source: XIOS/trunk/src/server.cpp @ 1205

Last change on this file since 1205 was 1135, checked in by ymipsl, 7 years ago
  • Add new timer for better profiling. The full timer output will be provided only for info_level=100
  • Add new file attribute : convention_str (string) : this string will overide the default value wrote in the file (CF-1.6 or UGRID)

YM

  • 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: 14.0 KB
Line 
1#include "globalScopeData.hpp"
2#include "xios_spl.hpp"
3#include "cxios.hpp"
4#include "server.hpp"
5#include "type.hpp"
6#include "context.hpp"
7#include "object_template.hpp"
8#include "oasis_cinterface.hpp"
9#include <boost/functional/hash.hpp>
10#include <boost/algorithm/string.hpp>
11#include "mpi.hpp"
12#include "tracer.hpp"
13#include "timer.hpp"
14#include "event_scheduler.hpp"
15
16namespace xios
17{
18    MPI_Comm CServer::intraComm ;
19    list<MPI_Comm> CServer::interComm ;
20    std::list<MPI_Comm> CServer::contextInterComms;
21    bool CServer::isRoot ;
22    int CServer::rank = INVALID_RANK;
23    StdOFStream CServer::m_infoStream;
24    StdOFStream CServer::m_errorStream;
25    map<string,CContext*> CServer::contextList ;
26    bool CServer::finished=false ;
27    bool CServer::is_MPI_Initialized ;
28    CEventScheduler* CServer::eventScheduler = 0;
29   
30    void CServer::initialize(void)
31    {
32      int initialized ;
33      MPI_Initialized(&initialized) ;
34      if (initialized) is_MPI_Initialized=true ;
35      else is_MPI_Initialized=false ;
36
37      // Not using OASIS
38      if (!CXios::usingOasis)
39      {
40
41        if (!is_MPI_Initialized)
42        {
43          MPI_Init(NULL, NULL);
44        }
45        CTimer::get("XIOS").resume() ;
46
47        boost::hash<string> hashString ;
48
49        unsigned long hashServer=hashString(CXios::xiosCodeId) ;
50        unsigned long* hashAll ;
51
52//        int rank ;
53        int size ;
54        int myColor ;
55        int i,c ;
56        MPI_Comm newComm ;
57
58        MPI_Comm_size(CXios::globalComm,&size) ;
59        MPI_Comm_rank(CXios::globalComm,&rank);
60        hashAll=new unsigned long[size] ;
61
62        MPI_Allgather(&hashServer,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ;
63
64        map<unsigned long, int> colors ;
65        map<unsigned long, int> leaders ;
66        map<unsigned long, int>::iterator it ;
67
68        for(i=0,c=0;i<size;i++)
69        {
70          if (colors.find(hashAll[i])==colors.end())
71          {
72            colors[hashAll[i]]=c ;
73            leaders[hashAll[i]]=i ;
74            c++ ;
75          }
76        }
77
78        myColor=colors[hashServer] ;
79        MPI_Comm_split(MPI_COMM_WORLD,myColor,rank,&intraComm) ;
80
81        int serverLeader=leaders[hashServer] ;
82        int clientLeader;
83
84         serverLeader=leaders[hashServer] ;
85         for(it=leaders.begin();it!=leaders.end();it++)
86         {
87           if (it->first!=hashServer)
88           {
89             clientLeader=it->second ;
90             int intraCommSize, intraCommRank ;
91             MPI_Comm_size(intraComm,&intraCommSize) ;
92             MPI_Comm_rank(intraComm,&intraCommRank) ;
93             info(50)<<"intercommCreate::server "<<rank<<" intraCommSize : "<<intraCommSize
94                     <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
95
96             MPI_Intercomm_create(intraComm,0,CXios::globalComm,clientLeader,0,&newComm) ;
97             interComm.push_back(newComm) ;
98           }
99         }
100
101         delete [] hashAll ;
102      }
103      // using OASIS
104      else
105      {
106//        int rank ,size;
107        int size;
108        if (!is_MPI_Initialized) oasis_init(CXios::xiosCodeId);
109
110        CTimer::get("XIOS").resume() ;
111        MPI_Comm localComm;
112        oasis_get_localcomm(localComm);
113        MPI_Comm_dup(localComm, &intraComm);
114
115        MPI_Comm_rank(intraComm,&rank) ;
116        MPI_Comm_size(intraComm,&size) ;
117        string codesId=CXios::getin<string>("oasis_codes_id") ;
118
119        vector<string> splitted ;
120        boost::split( splitted, codesId, boost::is_any_of(","), boost::token_compress_on ) ;
121        vector<string>::iterator it ;
122
123        MPI_Comm newComm ;
124        int globalRank ;
125        MPI_Comm_rank(CXios::globalComm,&globalRank);
126
127        for(it=splitted.begin();it!=splitted.end();it++)
128        {
129          oasis_get_intercomm(newComm,*it) ;
130          if (rank==0) MPI_Send(&globalRank,1,MPI_INT,0,0,newComm) ;
131          MPI_Comm_remote_size(newComm,&size);
132          interComm.push_back(newComm) ;
133        }
134              oasis_enddef() ;
135      }
136
137//      int rank;
138      MPI_Comm_rank(intraComm,&rank) ;
139      if (rank==0) isRoot=true;
140      else isRoot=false;
141     
142      eventScheduler = new CEventScheduler(intraComm) ;
143    }
144
145    void CServer::finalize(void)
146    {
147      CTimer::get("XIOS").suspend() ;
148     
149      delete eventScheduler ;
150
151      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
152        MPI_Comm_free(&(*it));
153      for (std::list<MPI_Comm>::iterator it = interComm.begin(); it != interComm.end(); it++)
154        MPI_Comm_free(&(*it));
155      MPI_Comm_free(&intraComm);
156
157      if (!is_MPI_Initialized)
158      {
159        if (CXios::usingOasis) oasis_finalize();
160        else MPI_Finalize() ;
161      }
162      report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl  ;
163      report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl  ;
164      report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl  ;
165      report(100)<<CTimer::getAllCumulatedTime()<<endl ;
166    }
167
168     void CServer::eventLoop(void)
169     {
170       bool stop=false ;
171
172       CTimer::get("XIOS server").resume() ;
173       while(!stop)
174       {
175         if (isRoot)
176         {
177           listenContext();
178           if (!finished) listenFinalize() ;
179         }
180         else
181         {
182           listenRootContext();
183           if (!finished) listenRootFinalize() ;
184         }
185
186         contextEventLoop() ;
187         if (finished && contextList.empty()) stop=true ;
188         eventScheduler->checkEvent() ;
189       }
190       CTimer::get("XIOS server").suspend() ;
191     }
192
193     void CServer::listenFinalize(void)
194     {
195        list<MPI_Comm>::iterator it;
196        int msg ;
197        int flag ;
198
199        for(it=interComm.begin();it!=interComm.end();it++)
200        {
201           MPI_Status status ;
202           traceOff() ;
203           MPI_Iprobe(0,0,*it,&flag,&status) ;
204           traceOn() ;
205           if (flag==true)
206           {
207              MPI_Recv(&msg,1,MPI_INT,0,0,*it,&status) ;
208              info(20)<<" CServer : Receive client finalize"<<endl ;
209              MPI_Comm_free(&(*it));
210              interComm.erase(it) ;
211              break ;
212            }
213         }
214
215         if (interComm.empty())
216         {
217           int i,size ;
218           MPI_Comm_size(intraComm,&size) ;
219           MPI_Request* requests= new MPI_Request[size-1] ;
220           MPI_Status* status= new MPI_Status[size-1] ;
221
222           for(int i=1;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,4,intraComm,&requests[i-1]) ;
223           MPI_Waitall(size-1,requests,status) ;
224
225           finished=true ;
226           delete [] requests ;
227           delete [] status ;
228         }
229     }
230
231
232     void CServer::listenRootFinalize()
233     {
234        int flag ;
235        MPI_Status status ;
236        int msg ;
237
238        traceOff() ;
239        MPI_Iprobe(0,4,intraComm, &flag, &status) ;
240        traceOn() ;
241        if (flag==true)
242        {
243           MPI_Recv(&msg,1,MPI_INT,0,4,intraComm,&status) ;
244           finished=true ;
245        }
246      }
247
248     void CServer::listenContext(void)
249     {
250
251       MPI_Status status ;
252       int flag ;
253       static char* buffer ;
254       static MPI_Request request ;
255       static bool recept=false ;
256       int rank ;
257       int count ;
258
259       if (recept==false)
260       {
261         traceOff() ;
262         MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ;
263         traceOn() ;
264         if (flag==true)
265         {
266           rank=status.MPI_SOURCE ;
267           MPI_Get_count(&status,MPI_CHAR,&count) ;
268           buffer=new char[count] ;
269           MPI_Irecv((void*)buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ;
270           recept=true ;
271         }
272       }
273       else
274       {
275         traceOff() ;
276         MPI_Test(&request,&flag,&status) ;
277         traceOn() ;
278         if (flag==true)
279         {
280           rank=status.MPI_SOURCE ;
281           MPI_Get_count(&status,MPI_CHAR,&count) ;
282           recvContextMessage((void*)buffer,count) ;
283           delete [] buffer ;
284           recept=false ;
285         }
286       }
287     }
288
289     void CServer::recvContextMessage(void* buff,int count)
290     {
291       static map<string,contextMessage> recvContextId ;
292       map<string,contextMessage>::iterator it ;
293
294       CBufferIn buffer(buff,count) ;
295       string id ;
296       int clientLeader ;
297       int nbMessage ;
298
299       buffer>>id>>nbMessage>>clientLeader ;
300
301       it=recvContextId.find(id) ;
302       if (it==recvContextId.end())
303       {
304         contextMessage msg={0,0} ;
305         pair<map<string,contextMessage>::iterator,bool> ret ;
306         ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ;
307         it=ret.first ;
308       }
309       it->second.nbRecv+=1 ;
310       it->second.leaderRank+=clientLeader ;
311
312       if (it->second.nbRecv==nbMessage)
313       {
314         int size ;
315         MPI_Comm_size(intraComm,&size) ;
316         MPI_Request* requests= new MPI_Request[size-1] ;
317         MPI_Status* status= new MPI_Status[size-1] ;
318
319         for(int i=1;i<size;i++)
320         {
321            MPI_Isend(buff,count,MPI_CHAR,i,2,intraComm,&requests[i-1]) ;
322         }
323         MPI_Waitall(size-1,requests,status) ;
324         registerContext(buff,count,it->second.leaderRank) ;
325
326         recvContextId.erase(it) ;
327         delete [] requests ;
328         delete [] status ;
329
330       }
331     }
332
333     void CServer::listenRootContext(void)
334     {
335
336       MPI_Status status ;
337       int flag ;
338       static char* buffer ;
339       static MPI_Request request ;
340       static bool recept=false ;
341       int rank ;
342       int count ;
343       const int root=0 ;
344
345       if (recept==false)
346       {
347         traceOff() ;
348         MPI_Iprobe(root,2,intraComm, &flag, &status) ;
349         traceOn() ;
350         if (flag==true)
351         {
352           MPI_Get_count(&status,MPI_CHAR,&count) ;
353           buffer=new char[count] ;
354           MPI_Irecv((void*)buffer,count,MPI_CHAR,root,2,intraComm,&request) ;
355           recept=true ;
356         }
357       }
358       else
359       {
360         MPI_Test(&request,&flag,&status) ;
361         if (flag==true)
362         {
363           MPI_Get_count(&status,MPI_CHAR,&count) ;
364           registerContext((void*)buffer,count) ;
365           delete [] buffer ;
366           recept=false ;
367         }
368       }
369     }
370
371     void CServer::registerContext(void* buff, int count, int leaderRank)
372     {
373       string contextId;
374       CBufferIn buffer(buff, count);
375       buffer >> contextId;
376
377       info(20) << "CServer : Register new Context : " << contextId << endl;
378
379       if (contextList.find(contextId) != contextList.end())
380         ERROR("void CServer::registerContext(void* buff, int count, int leaderRank)",
381               << "Context '" << contextId << "' has already been registred");
382
383       MPI_Comm contextIntercomm;
384       MPI_Intercomm_create(intraComm,0,CXios::globalComm,leaderRank,10+leaderRank,&contextIntercomm);
385
386       MPI_Comm inter;
387       MPI_Intercomm_merge(contextIntercomm,1,&inter);
388       MPI_Barrier(inter);
389
390       CContext* context=CContext::create(contextId);
391       contextList[contextId]=context;
392       context->initServer(intraComm,contextIntercomm);
393
394       contextInterComms.push_back(contextIntercomm);
395       MPI_Comm_free(&inter);
396     }
397
398     void CServer::contextEventLoop(void)
399     {
400       bool finished ;
401       map<string,CContext*>::iterator it ;
402       for(it=contextList.begin();it!=contextList.end();it++)
403       {
404         finished=it->second->checkBuffersAndListen();
405         if (finished)
406         {
407           contextList.erase(it) ;
408           break ;
409         }
410       }
411
412     }
413
414     //! Get rank of the current process
415     int CServer::getRank()
416     {
417       return rank;
418     }
419
420    /*!
421    * Open a file specified by a suffix and an extension and use it for the given file buffer.
422    * The file name will be suffix+rank+extension.
423    *
424    * \param fileName[in] protype file name
425    * \param ext [in] extension of the file
426    * \param fb [in/out] the file buffer
427    */
428    void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
429    {
430      StdStringStream fileNameClient;
431      int numDigit = 0;
432      int size = 0;
433      MPI_Comm_size(CXios::globalComm, &size);
434      while (size)
435      {
436        size /= 10;
437        ++numDigit;
438      }
439
440      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
441      fb->open(fileNameClient.str().c_str(), std::ios::out);
442      if (!fb->is_open())
443        ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
444              << std::endl << "Can not open <" << fileNameClient << "> file to write the server log(s).");
445    }
446
447    /*!
448    * \brief Open a file stream to write the info logs
449    * Open a file stream with a specific file name suffix+rank
450    * to write the info logs.
451    * \param fileName [in] protype file name
452    */
453    void CServer::openInfoStream(const StdString& fileName)
454    {
455      std::filebuf* fb = m_infoStream.rdbuf();
456      openStream(fileName, ".out", fb);
457
458      info.write2File(fb);
459      report.write2File(fb);
460    }
461
462    //! Write the info logs to standard output
463    void CServer::openInfoStream()
464    {
465      info.write2StdOut();
466      report.write2StdOut();
467    }
468
469    //! Close the info logs file if it opens
470    void CServer::closeInfoStream()
471    {
472      if (m_infoStream.is_open()) m_infoStream.close();
473    }
474
475    /*!
476    * \brief Open a file stream to write the error log
477    * Open a file stream with a specific file name suffix+rank
478    * to write the error log.
479    * \param fileName [in] protype file name
480    */
481    void CServer::openErrorStream(const StdString& fileName)
482    {
483      std::filebuf* fb = m_errorStream.rdbuf();
484      openStream(fileName, ".err", fb);
485
486      error.write2File(fb);
487    }
488
489    //! Write the error log to standard error output
490    void CServer::openErrorStream()
491    {
492      error.write2StdErr();
493    }
494
495    //! Close the error log file if it opens
496    void CServer::closeErrorStream()
497    {
498      if (m_errorStream.is_open()) m_errorStream.close();
499    }
500}
Note: See TracBrowser for help on using the repository browser.