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

Last change on this file since 1190 was 1135, checked in by ymipsl, 7 years ago
  • Add new timer for better profiling. The full timer output will be provided only for info_level=100
  • Add new file attribute : convention_str (string) : this string will overide the default value wrote in the file (CF-1.6 or UGRID)

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