source: XIOS/dev/branch_yushan_merged/src/server.cpp @ 1156

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

branch merged with trunk @1155

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