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

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

Using threads : modif for xios_initialize

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