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

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

test_complete OK with openmp. Missing : arithmetic filter

  • 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.8 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() ;
[1110]283         #ifdef _usingEP
284         MPI_Iprobe(-1,1,CXios::globalComm, &flag, &status) ;
285         #else
[300]286         MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ;
[1110]287         #endif
[347]288         traceOn() ;
[1037]289         
[490]290         if (flag==true)
[300]291         {
[1037]292           #ifdef _usingMPI
[300]293           rank=status.MPI_SOURCE ;
[1037]294           #elif _usingEP
295           rank= status.ep_src ;
296           #endif
[300]297           MPI_Get_count(&status,MPI_CHAR,&count) ;
298           buffer=new char[count] ;
[1037]299           MPI_Irecv(buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ;
[490]300           recept=true ;
[300]301         }
[1037]302         
[300]303       }
304       else
305       {
[347]306         traceOff() ;
[300]307         MPI_Test(&request,&flag,&status) ;
[347]308         traceOn() ;
[300]309         if (flag==true)
310         {
[1037]311           #ifdef _usingMPI
[300]312           rank=status.MPI_SOURCE ;
[1037]313           #elif _usingEP
314           rank= status.ep_src ;
315           #endif
[300]316           MPI_Get_count(&status,MPI_CHAR,&count) ;
[1037]317           recvContextMessage(buffer,count) ;
318           
[300]319           delete [] buffer ;
[490]320           recept=false ;
[300]321         }
322       }
323     }
[490]324
[300]325     void CServer::recvContextMessage(void* buff,int count)
326     {
327       static map<string,contextMessage> recvContextId ;
328       map<string,contextMessage>::iterator it ;
[490]329
[300]330       CBufferIn buffer(buff,count) ;
331       string id ;
332       int clientLeader ;
333       int nbMessage ;
334
335       buffer>>id>>nbMessage>>clientLeader ;
[490]336
[300]337       it=recvContextId.find(id) ;
338       if (it==recvContextId.end())
[490]339       {
[300]340         contextMessage msg={0,0} ;
341         pair<map<string,contextMessage>::iterator,bool> ret ;
342         ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ;
343         it=ret.first ;
[490]344       }
[300]345       it->second.nbRecv+=1 ;
346       it->second.leaderRank+=clientLeader ;
[490]347
[300]348       if (it->second.nbRecv==nbMessage)
[490]349       {
[300]350         int size ;
351         MPI_Comm_size(intraComm,&size) ;
352         MPI_Request* requests= new MPI_Request[size-1] ;
353         MPI_Status* status= new MPI_Status[size-1] ;
[490]354
[300]355         for(int i=1;i<size;i++)
356         {
357            MPI_Isend(buff,count,MPI_CHAR,i,2,intraComm,&requests[i-1]) ;
358         }
359         MPI_Waitall(size-1,requests,status) ;
360         registerContext(buff,count,it->second.leaderRank) ;
361
362         recvContextId.erase(it) ;
363         delete [] requests ;
364         delete [] status ;
365
366       }
[490]367     }
368
[300]369     void CServer::listenRootContext(void)
370     {
[490]371
[300]372       MPI_Status status ;
373       int flag ;
[1037]374       static void* buffer ;
[1126]375       #pragma omp threadprivate(buffer)
[1072]376
[300]377       static MPI_Request request ;
[1126]378       #pragma omp threadprivate(request)
[1072]379
[300]380       static bool recept=false ;
[1126]381       #pragma omp threadprivate(recept)
[1072]382       
[300]383       int rank ;
384       int count ;
385       const int root=0 ;
[1037]386       
[490]387
[300]388       if (recept==false)
389       {
[347]390         traceOff() ;
[300]391         MPI_Iprobe(root,2,intraComm, &flag, &status) ;
[347]392         traceOn() ;
[490]393         if (flag==true)
[300]394         {
395           MPI_Get_count(&status,MPI_CHAR,&count) ;
396           buffer=new char[count] ;
[1037]397           MPI_Irecv(buffer,count,MPI_CHAR,root,2,intraComm,&request) ;
[490]398           recept=true ;
[300]399         }
400       }
401       else
402       {
403         MPI_Test(&request,&flag,&status) ;
404         if (flag==true)
405         {
406           MPI_Get_count(&status,MPI_CHAR,&count) ;
[1037]407           registerContext(buffer,count) ;
[300]408           delete [] buffer ;
[490]409           recept=false ;
[300]410         }
411       }
[490]412     }
413
[655]414     void CServer::registerContext(void* buff, int count, int leaderRank)
[300]415     {
416       string contextId;
[655]417       CBufferIn buffer(buff, count);
418       buffer >> contextId;
[300]419
[680]420       info(20) << "CServer : Register new Context : " << contextId << endl;
[490]421
[680]422       if (contextList.find(contextId) != contextList.end())
423         ERROR("void CServer::registerContext(void* buff, int count, int leaderRank)",
424               << "Context '" << contextId << "' has already been registred");
[490]425
[655]426       MPI_Comm contextIntercomm;
[1037]427       
[655]428       MPI_Intercomm_create(intraComm,0,CXios::globalComm,leaderRank,10+leaderRank,&contextIntercomm);
[490]429
[1067]430       MPI_Comm inter;
431       MPI_Intercomm_merge(contextIntercomm,1,&inter);
432       MPI_Barrier(inter);
433
434       info(20) << "CServer : MPI_Intercomm_merge and MPI_Barrier " << contextId << endl;
[1037]435       
[655]436
[1079]437       CContext* context = CContext::create(contextId);
[655]438       contextList[contextId]=context;
439       context->initServer(intraComm,contextIntercomm);
[1037]440       
441       
[655]442
443       contextInterComms.push_back(contextIntercomm);
[1037]444       
445       
[1067]446       MPI_Comm_free(&inter);
[1037]447       
[1067]448       info(20) << "CServer : Register new Context OKOK " << contextId << endl;
[490]449     }
450
[300]451     void CServer::contextEventLoop(void)
452     {
453       bool finished ;
454       map<string,CContext*>::iterator it ;
[1063]455       for(it=contextList.begin();it!=contextList.end();++it)
[300]456       {
[597]457         finished=it->second->checkBuffersAndListen();
[300]458         if (finished)
459         {
460           contextList.erase(it) ;
461           break ;
462         }
463       }
[490]464
[300]465     }
[490]466
467     //! Get rank of the current process
468     int CServer::getRank()
469     {
470       return rank;
471     }
472
[523]473    /*!
474    * Open a file specified by a suffix and an extension and use it for the given file buffer.
475    * The file name will be suffix+rank+extension.
476    *
477    * \param fileName[in] protype file name
478    * \param ext [in] extension of the file
479    * \param fb [in/out] the file buffer
480    */
481    void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
482    {
483      StdStringStream fileNameClient;
484      int numDigit = 0;
485      int size = 0;
486      MPI_Comm_size(CXios::globalComm, &size);
487      while (size)
488      {
489        size /= 10;
490        ++numDigit;
491      }
[497]492
[523]493      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
494      fb->open(fileNameClient.str().c_str(), std::ios::out);
495      if (!fb->is_open())
496        ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
497              << std::endl << "Can not open <" << fileNameClient << "> file to write the server log(s).");
498    }
[490]499
[523]500    /*!
501    * \brief Open a file stream to write the info logs
502    * Open a file stream with a specific file name suffix+rank
503    * to write the info logs.
504    * \param fileName [in] protype file name
505    */
506    void CServer::openInfoStream(const StdString& fileName)
507    {
508      std::filebuf* fb = m_infoStream.rdbuf();
509      openStream(fileName, ".out", fb);
[490]510
[523]511      info.write2File(fb);
512      report.write2File(fb);
513    }
[490]514
[523]515    //! Write the info logs to standard output
516    void CServer::openInfoStream()
517    {
518      info.write2StdOut();
519      report.write2StdOut();
520    }
[490]521
[523]522    //! Close the info logs file if it opens
523    void CServer::closeInfoStream()
524    {
525      if (m_infoStream.is_open()) m_infoStream.close();
526    }
527
528    /*!
529    * \brief Open a file stream to write the error log
530    * Open a file stream with a specific file name suffix+rank
531    * to write the error log.
532    * \param fileName [in] protype file name
533    */
534    void CServer::openErrorStream(const StdString& fileName)
535    {
536      std::filebuf* fb = m_errorStream.rdbuf();
537      openStream(fileName, ".err", fb);
538
539      error.write2File(fb);
540    }
541
542    //! Write the error log to standard error output
543    void CServer::openErrorStream()
544    {
545      error.write2StdErr();
546    }
547
548    //! Close the error log file if it opens
549    void CServer::closeErrorStream()
550    {
551      if (m_errorStream.is_open()) m_errorStream.close();
552    }
[300]553}
Note: See TracBrowser for help on using the repository browser.