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

Last change on this file since 778 was 697, checked in by ymipsl, 9 years ago

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