source: XIOS/dev/branch_yushan/src/server.cpp @ 1085

Last change on this file since 1085 was 1085, checked in by yushan, 7 years ago

save modif for local compilation

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