source: XIOS/dev/branch_openmp/src/client.cpp @ 1338

Last change on this file since 1338 was 1338, checked in by yushan, 6 years ago

dev_omp

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:eol-style set to native
File size: 11.3 KB
Line 
1#include "globalScopeData.hpp"
2#include "xios_spl.hpp"
3#include "cxios.hpp"
4#include "client.hpp"
5#include <boost/functional/hash.hpp>
6#include "type.hpp"
7#include "context.hpp"
8#include "context_client.hpp"
9#include "oasis_cinterface.hpp"
10#include "mpi.hpp"
11#include "timer.hpp"
12#include "buffer_client.hpp"
13using namespace ep_lib;
14
15namespace xios
16{
17
18    MPI_Comm CClient::intraComm ;
19    MPI_Comm CClient::interComm ;
20    //std::list<MPI_Comm> CClient::contextInterComms;
21    std::list<MPI_Comm> *CClient::contextInterComms_ptr = 0;
22    int CClient::serverLeader ;
23    bool CClient::is_MPI_Initialized ;
24    int CClient::rank = INVALID_RANK;
25    StdOFStream CClient::m_infoStream;
26    StdOFStream CClient::m_errorStream;
27
28    void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)
29    {
30      int initialized ;
31      MPI_Initialized(&initialized) ;
32      if (initialized) is_MPI_Initialized=true ;
33      else is_MPI_Initialized=false ;
34
35// don't use OASIS
36      if (!CXios::usingOasis)
37      {
38// localComm doesn't given
39        if (localComm == MPI_COMM_NULL)
40        {
41          if (!is_MPI_Initialized)
42          {
43            MPI_Init(NULL, NULL);
44          }
45          CTimer::get("XIOS").resume() ;
46          CTimer::get("XIOS init/finalize").resume() ;
47          boost::hash<string> hashString ;
48
49          unsigned long hashClient=hashString(codeId) ;
50          unsigned long hashServer=hashString(CXios::xiosCodeId) ;
51          unsigned long* hashAll ;
52          int size ;
53          int myColor ;
54          int i,c ;
55          MPI_Comm newComm ;
56
57          MPI_Comm_size(CXios::globalComm,&size) ;
58          MPI_Comm_rank(CXios::globalComm,&rank);
59
60          hashAll=new unsigned long[size] ;
61
62          MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ;
63
64          map<unsigned long, int> colors ;
65          map<unsigned long, int> leaders ;
66
67          for(i=0,c=0;i<size;i++)
68          {
69            if (colors.find(hashAll[i])==colors.end())
70            {
71              colors[hashAll[i]] =c ;
72              leaders[hashAll[i]]=i ;
73              c++ ;
74            }
75          }
76
77          // Verify whether we are on server mode or not
78          CXios::setNotUsingServer();
79          for (i=0; i < size; ++i)
80          {
81            if (hashServer == hashAll[i])
82            {
83              CXios::setUsingServer();
84              break;
85            }
86          }
87
88          myColor=colors[hashClient] ;
89
90          MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ;
91
92          if (CXios::usingServer)
93          {
94            int clientLeader=leaders[hashClient] ;
95            serverLeader=leaders[hashServer] ;
96
97            int intraCommSize, intraCommRank ;
98            MPI_Comm_size(intraComm,&intraCommSize) ;
99            MPI_Comm_rank(intraComm,&intraCommRank) ;
100            info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize
101                 <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
102            MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
103          }
104          else
105          {
106            MPI_Comm_dup(intraComm,&interComm) ;
107          }
108          delete [] hashAll ;
109        }
110        // localComm argument is given
111        else
112        {
113          if (CXios::usingServer)
114          {
115            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
116          }
117          else
118          {
119            MPI_Comm_dup(localComm,&intraComm) ;
120            MPI_Comm_dup(intraComm,&interComm) ;
121          }
122        }
123      }
124      // using OASIS
125/*      else
126      {
127        // localComm doesn't given
128        if (localComm == MPI_COMM_NULL)
129        {
130          if (!is_MPI_Initialized) oasis_init(codeId) ;
131          oasis_get_localcomm(localComm) ;
132        }
133        MPI_Comm_dup(localComm,&intraComm) ;
134
135        CTimer::get("XIOS").resume() ;
136        CTimer::get("XIOS init/finalize").resume() ;
137
138        if (CXios::usingServer)
139        {
140          MPI_Status status ;
141          MPI_Comm_rank(intraComm,&rank) ;
142
143          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
144          if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
145          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
146
147        }
148        else MPI_Comm_dup(intraComm,&interComm) ;
149      }
150*/
151      MPI_Comm_dup(intraComm,&returnComm) ;
152    }
153
154
155    void CClient::registerContext(const string& id,MPI_Comm contextComm)
156    {
157      CContext::setCurrent(id) ;
158      CContext* context=CContext::create(id);
159      StdString idServer(id);
160      idServer += "_server";
161
162      if (!CXios::isServer)
163      {
164        int size,rank,globalRank ;
165        size_t message_size ;
166        int leaderRank ;
167        MPI_Comm contextInterComm ;
168
169        MPI_Comm_size(contextComm,&size) ;
170        MPI_Comm_rank(contextComm,&rank) ;
171        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
172        if (rank!=0) globalRank=0 ;
173
174
175        CMessage msg ;
176        msg<<idServer<<size<<globalRank ;
177//        msg<<id<<size<<globalRank ;
178
179        int messageSize=msg.size() ;
180        char * buff = new char[messageSize] ;
181        CBufferOut buffer((void*)buff,messageSize) ;
182        buffer<<msg ;
183
184        MPI_Send((void*)buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
185        delete [] buff ;
186
187        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
188        #pragma omp critical (std_output)
189        {
190          //info(10)<<"Register new Context : "<<id<<endl ;
191        }
192
193        MPI_Comm inter ;
194        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
195        MPI_Barrier(inter) ;
196
197        context->initClient(contextComm,contextInterComm) ;
198
199        //contextInterComms.push_back(contextInterComm);
200        if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>;
201        contextInterComms_ptr->push_back(contextInterComm);
202        MPI_Comm_free(&inter);
203      }
204      else
205      {
206        MPI_Comm contextInterComm ;
207        MPI_Comm_dup(contextComm,&contextInterComm) ;
208        CContext* contextServer = CContext::create(idServer);
209
210        // Firstly, initialize context on client side
211        context->initClient(contextComm,contextInterComm, contextServer);
212
213        // Secondly, initialize context on server side
214        contextServer->initServer(contextComm,contextInterComm, context);
215
216        // Finally, we should return current context to context client
217        CContext::setCurrent(id);
218
219        //contextInterComms.push_back(contextInterComm);
220        if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>;
221        contextInterComms_ptr->push_back(contextInterComm);
222      }
223    }
224
225    void CClient::finalize(void)
226    {
227      int rank ;
228      int msg=0 ;
229
230      MPI_Comm_rank(intraComm,&rank) ;
231 
232      if (!CXios::isServer)
233      {
234        MPI_Comm_rank(intraComm,&rank) ;
235        if (rank==0)
236        {
237          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
238        }
239      }
240
241      //for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
242      for (std::list<MPI_Comm>::iterator it = contextInterComms_ptr->begin(); it != contextInterComms_ptr->end(); it++)
243        MPI_Comm_free(&(*it));
244      MPI_Comm_free(&interComm);
245      MPI_Comm_free(&intraComm);
246
247      CTimer::get("XIOS init/finalize").suspend() ;
248      CTimer::get("XIOS").suspend() ;
249
250      if (!is_MPI_Initialized)
251      {
252        //if (CXios::usingOasis) oasis_finalize();
253        //else
254        MPI_Finalize() ;
255      }
256     
257      //info(20) << "Client side context is finalized"<<endl ;
258      //report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ;
259      //report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
260      //report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
261      //report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ;
262      //report(0)<< " Performance report : This ratio must be close to zero. Otherwise it may be usefull to increase buffer size or numbers of server"<<endl ;
263//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
264      //report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
265      //report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ;
266      //report(100)<<CTimer::getAllCumulatedTime()<<endl ;
267   }
268
269   int CClient::getRank()
270   {
271     return rank;
272   }
273
274    /*!
275    * Open a file specified by a suffix and an extension and use it for the given file buffer.
276    * The file name will be suffix+rank+extension.
277    *
278    * \param fileName[in] protype file name
279    * \param ext [in] extension of the file
280    * \param fb [in/out] the file buffer
281    */
282    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
283    {
284      StdStringStream fileNameClient;
285      int numDigit = 0;
286      int size = 0;
287      MPI_Comm_size(CXios::globalComm, &size);
288      while (size)
289      {
290        size /= 10;
291        ++numDigit;
292      }
293
294      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
295      fb->open(fileNameClient.str().c_str(), std::ios::out);
296      if (!fb->is_open())
297        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
298              << std::endl << "Can not open <" << fileNameClient << "> file to write the client log(s).");
299    }
300
301    /*!
302    * \brief Open a file stream to write the info logs
303    * Open a file stream with a specific file name suffix+rank
304    * to write the info logs.
305    * \param fileName [in] protype file name
306    */
307    void CClient::openInfoStream(const StdString& fileName)
308    {
309      std::filebuf* fb = m_infoStream.rdbuf();
310      openStream(fileName, ".out", fb);
311
312      info.write2File(fb);
313      report.write2File(fb);
314    }
315
316    //! Write the info logs to standard output
317    void CClient::openInfoStream()
318    {
319      info.write2StdOut();
320      report.write2StdOut();
321    }
322
323    //! Close the info logs file if it opens
324    void CClient::closeInfoStream()
325    {
326      if (m_infoStream.is_open()) m_infoStream.close();
327    }
328
329    /*!
330    * \brief Open a file stream to write the error log
331    * Open a file stream with a specific file name suffix+rank
332    * to write the error log.
333    * \param fileName [in] protype file name
334    */
335    void CClient::openErrorStream(const StdString& fileName)
336    {
337      std::filebuf* fb = m_errorStream.rdbuf();
338      openStream(fileName, ".err", fb);
339
340      error.write2File(fb);
341    }
342
343    //! Write the error log to standard error output
344    void CClient::openErrorStream()
345    {
346      error.write2StdErr();
347    }
348
349    //! Close the error log file if it opens
350    void CClient::closeErrorStream()
351    {
352      if (m_errorStream.is_open()) m_errorStream.close();
353    }
354}
Note: See TracBrowser for help on using the repository browser.