source: XIOS/dev/XIOS_DEV_CMIP6/src/server.cpp @ 1226

Last change on this file since 1226 was 1226, checked in by oabramkina, 7 years ago

Minor correction of calculations the number of processes dedicated to secondary pool in case of oasis.

  • 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: 23.4 KB
RevLine 
[490]1#include "globalScopeData.hpp"
[591]2#include "xios_spl.hpp"
[300]3#include "cxios.hpp"
[342]4#include "server.hpp"
[983]5#include "client.hpp"
[300]6#include "type.hpp"
7#include "context.hpp"
[352]8#include "object_template.hpp"
[300]9#include "oasis_cinterface.hpp"
10#include <boost/functional/hash.hpp>
11#include <boost/algorithm/string.hpp>
[382]12#include "mpi.hpp"
[347]13#include "tracer.hpp"
14#include "timer.hpp"
[492]15#include "event_scheduler.hpp"
[300]16
[335]17namespace xios
[490]18{
[300]19    MPI_Comm CServer::intraComm ;
[1180]20    std::list<MPI_Comm> CServer::interCommLeft ;
21    std::list<MPI_Comm> CServer::interCommRight ;
[655]22    std::list<MPI_Comm> CServer::contextInterComms;
[1071]23    std::list<MPI_Comm> CServer::contextIntraComms;
[1021]24    int CServer::serverLevel = 0 ;
[1148]25    int CServer::nbContexts = 0;
[983]26    bool CServer::isRoot = false ;
[1077]27    int CServer::rank_ = INVALID_RANK;
[490]28    StdOFStream CServer::m_infoStream;
[523]29    StdOFStream CServer::m_errorStream;
[490]30    map<string,CContext*> CServer::contextList ;
[1152]31    vector<int> CServer::sndServerGlobalRanks;
[300]32    bool CServer::finished=false ;
33    bool CServer::is_MPI_Initialized ;
[597]34    CEventScheduler* CServer::eventScheduler = 0;
[983]35
36//---------------------------------------------------------------
37/*!
38 * \fn void CServer::initialize(void)
[1054]39 * Creates intraComm for each possible type of servers (classical, primary or secondary).
40 * In case of secondary servers intraComm is created for each secondary server pool.
41 * (For now the assumption is that there is one proc per pool.)
[1148]42 * Creates interComm and stores them into the following lists:
[1054]43 *   classical server -- interCommLeft
44 *   primary server -- interCommLeft and interCommRight
[1148]45 *   secondary server -- interCommLeft for each pool.
[983]46 */
[300]47    void CServer::initialize(void)
48    {
49      int initialized ;
50      MPI_Initialized(&initialized) ;
51      if (initialized) is_MPI_Initialized=true ;
52      else is_MPI_Initialized=false ;
[1152]53      int rank ;
[490]54
[300]55      // Not using OASIS
56      if (!CXios::usingOasis)
57      {
[490]58
59        if (!is_MPI_Initialized)
[300]60        {
[925]61          MPI_Init(NULL, NULL);
[300]62        }
[359]63        CTimer::get("XIOS").resume() ;
[490]64
65        boost::hash<string> hashString ;
[1021]66        unsigned long hashServer = hashString(CXios::xiosCodeId);
[490]67
[300]68        unsigned long* hashAll ;
[1152]69        unsigned long* srvLevelAll ;
[490]70
[300]71        int size ;
72        int myColor ;
73        int i,c ;
[1152]74        MPI_Comm newComm;
[490]75
[983]76        MPI_Comm_size(CXios::globalComm, &size) ;
[1077]77        MPI_Comm_rank(CXios::globalComm, &rank_);
[1009]78
[300]79        hashAll=new unsigned long[size] ;
[983]80        MPI_Allgather(&hashServer, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ;
[490]81
[1021]82        map<unsigned long, int> colors ;
[300]83        map<unsigned long, int> leaders ;
84        map<unsigned long, int>::iterator it ;
[490]85
[1152]86        // (1) Establish client leaders, distribute processes between two server levels
[1180]87        vector<int> srvRanks;
[300]88        for(i=0,c=0;i<size;i++)
89        {
90          if (colors.find(hashAll[i])==colors.end())
91          {
92            colors[hashAll[i]]=c ;
93            leaders[hashAll[i]]=i ;
94            c++ ;
95          }
[1152]96          if (CXios::usingServer2)
[1180]97            if (hashAll[i] == hashServer)
98              srvRanks.push_back(i);
99        }
100        for (i=0; i<srvRanks.size(); i++)
101        {
[1214]102          if (i >= srvRanks.size()*(100.-CXios::ratioServer2)/100.)
[1152]103          {
[1180]104            sndServerGlobalRanks.push_back(srvRanks[i]);
105            if (rank_ == srvRanks[i]) serverLevel=2;
[1152]106          }
[1180]107          else
108          {
109            if (rank_ == srvRanks[i]) serverLevel=1;
110          }
[300]111        }
[490]112
[1152]113        // (2) Create intraComm
114        myColor = (serverLevel == 2) ? rank_ : colors[hashServer];
115        MPI_Comm_split(CXios::globalComm, myColor, rank_, &intraComm) ;
116
117        // (3) Create interComm
[1021]118        if (serverLevel == 0)
[983]119        {
120          int clientLeader;
121          for(it=leaders.begin();it!=leaders.end();it++)
122          {
123            if (it->first!=hashServer)
124            {
125              clientLeader=it->second ;
126              int intraCommSize, intraCommRank ;
127              MPI_Comm_size(intraComm,&intraCommSize) ;
128              MPI_Comm_rank(intraComm,&intraCommRank) ;
[1142]129              info(50)<<"intercommCreate::server (classical mode) "<<rank_<<" intraCommSize : "<<intraCommSize
[983]130                       <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
[490]131
[1009]132              MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ;
[1180]133              interCommLeft.push_back(newComm) ;
[983]134            }
135          }
136        }
[1021]137        else if (serverLevel == 1)
[983]138        {
[1054]139          int clientLeader, srvSndLeader;
140          int srvPrmLeader ;
[1152]141
[1021]142          for (it=leaders.begin();it!=leaders.end();it++)
[983]143          {
[1021]144            if (it->first != hashServer)
[983]145            {
[1021]146              clientLeader=it->second ;
147              int intraCommSize, intraCommRank ;
148              MPI_Comm_size(intraComm, &intraCommSize) ;
149              MPI_Comm_rank(intraComm, &intraCommRank) ;
[1142]150              info(50)<<"intercommCreate::server (server level 1) "<<rank_<<" intraCommSize : "<<intraCommSize
[1021]151                       <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
152              MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ;
153              interCommLeft.push_back(newComm) ;
[983]154            }
[1021]155          }
[1009]156
[1152]157          for (int i = 0; i < sndServerGlobalRanks.size(); ++i)
[983]158          {
[1054]159            int intraCommSize, intraCommRank ;
160            MPI_Comm_size(intraComm, &intraCommSize) ;
161            MPI_Comm_rank(intraComm, &intraCommRank) ;
[1142]162            info(50)<<"intercommCreate::client (server level 1) "<<rank_<<" intraCommSize : "<<intraCommSize
[1152]163                <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< sndServerGlobalRanks[i]<<endl ;
164            MPI_Intercomm_create(intraComm, 0, CXios::globalComm, sndServerGlobalRanks[i], 1, &newComm) ;
[1054]165            interCommRight.push_back(newComm) ;
[1021]166          }
[1168]167        }
[1021]168        else
169        {
170          int clientLeader;
171          clientLeader = leaders[hashString(CXios::xiosCodeId)];
172          int intraCommSize, intraCommRank ;
173          MPI_Comm_size(intraComm, &intraCommSize) ;
174          MPI_Comm_rank(intraComm, &intraCommRank) ;
[1142]175          info(50)<<"intercommCreate::server (server level 2) "<<rank_<<" intraCommSize : "<<intraCommSize
[1021]176                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
[983]177
[1142]178          MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 1, &newComm) ;
[1021]179          interCommLeft.push_back(newComm) ;
[1168]180        }
[983]181
182        delete [] hashAll ;
183
[300]184      }
185      // using OASIS
186      else
187      {
[1152]188        int size;
[1130]189        int myColor;
[1180]190        unsigned long* srvGlobalRanks;
[490]191        if (!is_MPI_Initialized) oasis_init(CXios::xiosCodeId);
192
[359]193        CTimer::get("XIOS").resume() ;
[655]194        MPI_Comm localComm;
195        oasis_get_localcomm(localComm);
196
[1130]197        // Create server intraComm
198        if (!CXios::usingServer2)
[1167]199        {
[1130]200          MPI_Comm_dup(localComm, &intraComm);
[1167]201          MPI_Comm_rank(localComm,&rank_) ;
202        }
[1130]203        else
204        {
[1180]205          int globalRank;
[1152]206          MPI_Comm_rank(localComm,&rank_) ;
207          MPI_Comm_size(localComm,&size) ;
[1180]208          MPI_Comm_rank(CXios::globalComm,&globalRank) ;
209          srvGlobalRanks = new unsigned long[size] ;
210          MPI_Allgather(&globalRank, 1, MPI_LONG, srvGlobalRanks, 1, MPI_LONG, localComm) ;
[1152]211
[1226]212          for (int i=size*(100.-CXios::ratioServer2)/100.; i<size; i++)
[1180]213            sndServerGlobalRanks.push_back(srvGlobalRanks[i]);
[1152]214
215          if ( rank_ < (size - sndServerGlobalRanks.size()) )
[1130]216          {
217            serverLevel = 1;
218            myColor = 0;
219          }
220          else
221          {
222            serverLevel = 2;
[1152]223            myColor = rank_;
[1130]224          }
[1152]225          MPI_Comm_split(localComm, myColor, rank_, &intraComm) ;
[1130]226
227        }
[300]228        MPI_Comm_size(intraComm,&size) ;
229        string codesId=CXios::getin<string>("oasis_codes_id") ;
[490]230
[300]231        vector<string> splitted ;
[483]232        boost::split( splitted, codesId, boost::is_any_of(","), boost::token_compress_on ) ;
[300]233        vector<string>::iterator it ;
234
235        MPI_Comm newComm ;
236        int globalRank ;
237        MPI_Comm_rank(CXios::globalComm,&globalRank);
[490]238
[1180]239//      (1) Create interComms with models
[300]240        for(it=splitted.begin();it!=splitted.end();it++)
241        {
242          oasis_get_intercomm(newComm,*it) ;
[1180]243          if ( !CXios::usingServer2 || serverLevel == 1)
[1150]244          {
[1130]245            interCommLeft.push_back(newComm) ;
[1150]246            if (rank_==0) MPI_Send(&globalRank,1,MPI_INT,0,0,newComm) ;
247          }
[1180]248        }
249
250//      (2) Create interComms between primary and secondary servers
251        if (serverLevel == 1)
252        {
253          for (int i = 0; i < sndServerGlobalRanks.size(); ++i)
[1130]254          {
[1180]255            int srvSndLeader = sndServerGlobalRanks[i];
256            info(50)<<"intercommCreate::client (server level 1) "<<globalRank<<" intraCommSize : "<<size
257                <<" intraCommRank :"<<rank_<<"  clientLeader "<< srvSndLeader<<endl ;
258            MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvSndLeader, 0, &newComm) ;
259            interCommRight.push_back(newComm) ;
[1130]260          }
[300]261        }
[1180]262        else if (serverLevel == 2)
263        {
264          info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<size
265                   <<" intraCommRank :"<<rank_<<"  clientLeader "<< srvGlobalRanks[0] <<endl ;
266          MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvGlobalRanks[0], 0, &newComm) ;
267          interCommLeft.push_back(newComm) ;
268        }
[1184]269        if (CXios::usingServer2) delete [] srvGlobalRanks ;
270        oasis_enddef() ;
[300]271      }
[490]272
[1184]273
[1152]274      MPI_Comm_rank(intraComm, &rank) ;
275      if (rank==0) isRoot=true;
[490]276      else isRoot=false;
[492]277     
278      eventScheduler = new CEventScheduler(intraComm) ;
[300]279    }
[490]280
[300]281    void CServer::finalize(void)
282    {
[361]283      CTimer::get("XIOS").suspend() ;
[697]284     
[492]285      delete eventScheduler ;
[655]286
287      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
288        MPI_Comm_free(&(*it));
[983]289
[1071]290      for (std::list<MPI_Comm>::iterator it = contextIntraComms.begin(); it != contextIntraComms.end(); it++)
291        MPI_Comm_free(&(*it));
292
[992]293//      for (std::list<MPI_Comm>::iterator it = interComm.begin(); it != interComm.end(); it++)
294//        MPI_Comm_free(&(*it));
295
[1077]296//        for (std::list<MPI_Comm>::iterator it = interCommLeft.begin(); it != interCommLeft.end(); it++)
297//          MPI_Comm_free(&(*it));
[983]298
[1054]299        for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++)
300          MPI_Comm_free(&(*it));
[992]301
[655]302      MPI_Comm_free(&intraComm);
303
[300]304      if (!is_MPI_Initialized)
[490]305      {
[300]306        if (CXios::usingOasis) oasis_finalize();
307        else MPI_Finalize() ;
308      }
[347]309      report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl  ;
310      report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl  ;
311      report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl  ;
[1158]312      report(100)<<CTimer::getAllCumulatedTime()<<endl ;
[300]313    }
[490]314
[300]315     void CServer::eventLoop(void)
316     {
317       bool stop=false ;
[490]318
[347]319       CTimer::get("XIOS server").resume() ;
[300]320       while(!stop)
321       {
322         if (isRoot)
323         {
324           listenContext();
[1148]325           listenRootContext();
[300]326           if (!finished) listenFinalize() ;
327         }
328         else
329         {
330           listenRootContext();
331           if (!finished) listenRootFinalize() ;
332         }
[490]333
[300]334         contextEventLoop() ;
335         if (finished && contextList.empty()) stop=true ;
[956]336         eventScheduler->checkEvent() ;
[300]337       }
[347]338       CTimer::get("XIOS server").suspend() ;
[300]339     }
[490]340
[300]341     void CServer::listenFinalize(void)
342     {
[992]343        list<MPI_Comm>::iterator it, itr;
[300]344        int msg ;
345        int flag ;
[490]346
[992]347        for(it=interCommLeft.begin();it!=interCommLeft.end();it++)
[300]348        {
349           MPI_Status status ;
[347]350           traceOff() ;
[300]351           MPI_Iprobe(0,0,*it,&flag,&status) ;
[347]352           traceOn() ;
[300]353           if (flag==true)
354           {
[1054]355              MPI_Recv(&msg,1,MPI_INT,0,0,*it,&status) ;
356              info(20)<<" CServer : Receive client finalize"<<endl ;
357              // Sending server finalize message to secondary servers (if any)
358              for(itr=interCommRight.begin();itr!=interCommRight.end();itr++)
359              {
360                MPI_Send(&msg,1,MPI_INT,0,0,*itr) ;
361              }
[655]362              MPI_Comm_free(&(*it));
[992]363              interCommLeft.erase(it) ;
[300]364              break ;
365            }
366         }
[490]367
[1054]368         if (interCommLeft.empty())
[300]369         {
370           int i,size ;
371           MPI_Comm_size(intraComm,&size) ;
372           MPI_Request* requests= new MPI_Request[size-1] ;
373           MPI_Status* status= new MPI_Status[size-1] ;
[490]374
[300]375           for(int i=1;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,4,intraComm,&requests[i-1]) ;
376           MPI_Waitall(size-1,requests,status) ;
377
378           finished=true ;
379           delete [] requests ;
380           delete [] status ;
381         }
382     }
[490]383
384
[300]385     void CServer::listenRootFinalize()
386     {
387        int flag ;
388        MPI_Status status ;
389        int msg ;
[490]390
[347]391        traceOff() ;
[300]392        MPI_Iprobe(0,4,intraComm, &flag, &status) ;
[347]393        traceOn() ;
[300]394        if (flag==true)
395        {
396           MPI_Recv(&msg,1,MPI_INT,0,4,intraComm,&status) ;
397           finished=true ;
398        }
399      }
[490]400
[300]401     void CServer::listenContext(void)
402     {
[490]403
[300]404       MPI_Status status ;
405       int flag ;
[1158]406       static char* buffer ;
[300]407       static MPI_Request request ;
408       static bool recept=false ;
409       int rank ;
410       int count ;
[490]411
[300]412       if (recept==false)
413       {
[347]414         traceOff() ;
[300]415         MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ;
[347]416         traceOn() ;
[490]417         if (flag==true)
[300]418         {
419           rank=status.MPI_SOURCE ;
420           MPI_Get_count(&status,MPI_CHAR,&count) ;
421           buffer=new char[count] ;
[1158]422           MPI_Irecv((void*)buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ;
[490]423           recept=true ;
[300]424         }
425       }
426       else
427       {
[347]428         traceOff() ;
[300]429         MPI_Test(&request,&flag,&status) ;
[347]430         traceOn() ;
[300]431         if (flag==true)
432         {
433           rank=status.MPI_SOURCE ;
434           MPI_Get_count(&status,MPI_CHAR,&count) ;
[1158]435           recvContextMessage((void*)buffer,count) ;
436           delete [] buffer ;
[490]437           recept=false ;
[300]438         }
439       }
440     }
[490]441
[300]442     void CServer::recvContextMessage(void* buff,int count)
443     {
[983]444       static map<string,contextMessage> recvContextId;
[300]445       map<string,contextMessage>::iterator it ;
446       CBufferIn buffer(buff,count) ;
447       string id ;
448       int clientLeader ;
449       int nbMessage ;
450
451       buffer>>id>>nbMessage>>clientLeader ;
[490]452
[300]453       it=recvContextId.find(id) ;
454       if (it==recvContextId.end())
[490]455       {
[300]456         contextMessage msg={0,0} ;
457         pair<map<string,contextMessage>::iterator,bool> ret ;
458         ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ;
459         it=ret.first ;
[490]460       }
[300]461       it->second.nbRecv+=1 ;
462       it->second.leaderRank+=clientLeader ;
[490]463
[300]464       if (it->second.nbRecv==nbMessage)
[490]465       {
[300]466         int size ;
467         MPI_Comm_size(intraComm,&size) ;
[1148]468//         MPI_Request* requests= new MPI_Request[size-1] ;
469//         MPI_Status* status= new MPI_Status[size-1] ;
470         MPI_Request* requests= new MPI_Request[size] ;
471         MPI_Status* status= new MPI_Status[size] ;
[490]472
[1148]473         CMessage msg ;
474         msg<<id<<it->second.leaderRank;
475         int messageSize=msg.size() ;
476         void * sendBuff = new char[messageSize] ;
477         CBufferOut sendBuffer(sendBuff,messageSize) ;
478         sendBuffer<<msg ;
479
480         // Include root itself in order not to have a divergence
481         for(int i=0; i<size; i++)
[300]482         {
[1148]483           MPI_Isend(sendBuff,count,MPI_CHAR,i,2,intraComm,&requests[i]) ;
[300]484         }
485
486         recvContextId.erase(it) ;
487         delete [] requests ;
488         delete [] status ;
489
490       }
[490]491     }
492
[300]493     void CServer::listenRootContext(void)
494     {
495       MPI_Status status ;
496       int flag ;
497       static void* buffer ;
498       static MPI_Request request ;
499       static bool recept=false ;
500       int rank ;
[1148]501//       int count ;
502       static int count ;
[300]503       const int root=0 ;
[1148]504       boost::hash<string> hashString;
505       size_t hashId = hashString("RegisterContext");
[490]506
[1148]507       // (1) Receive context id from the root
[300]508       if (recept==false)
509       {
[347]510         traceOff() ;
[300]511         MPI_Iprobe(root,2,intraComm, &flag, &status) ;
[347]512         traceOn() ;
[490]513         if (flag==true)
[300]514         {
515           MPI_Get_count(&status,MPI_CHAR,&count) ;
516           buffer=new char[count] ;
[1158]517           MPI_Irecv((void*)buffer,count,MPI_CHAR,root,2,intraComm,&request) ;
[490]518           recept=true ;
[300]519         }
520       }
[1148]521       // (2) If context id is received, save it into a buffer and register an event
[300]522       else
523       {
524         MPI_Test(&request,&flag,&status) ;
525         if (flag==true)
526         {
527           MPI_Get_count(&status,MPI_CHAR,&count) ;
[1148]528           eventScheduler->registerEvent(nbContexts,hashId);
[1158]529//           registerContext((void*)buffer,count) ;
[1148]530//           delete [] buffer ;
[490]531           recept=false ;
[300]532         }
533       }
[1148]534       // (3) If event has been scheduled, call register context
535       if (eventScheduler->queryEvent(nbContexts,hashId))
536       {
537         registerContext(buffer,count) ;
538         ++nbContexts;
539         delete [] buffer ;
540       }
[490]541     }
542
[655]543     void CServer::registerContext(void* buff, int count, int leaderRank)
[300]544     {
545       string contextId;
[655]546       CBufferIn buffer(buff, count);
[1148]547//       buffer >> contextId;
548       buffer >> contextId>>leaderRank;
[983]549       CContext* context;
[300]550
[680]551       info(20) << "CServer : Register new Context : " << contextId << endl;
[490]552
[680]553       if (contextList.find(contextId) != contextList.end())
554         ERROR("void CServer::registerContext(void* buff, int count, int leaderRank)",
555               << "Context '" << contextId << "' has already been registred");
[490]556
[983]557       context=CContext::create(contextId);
[655]558       contextList[contextId]=context;
559
[1148]560       // Primary or classical server: create communication channel with a client
561       // (1) create interComm (with a client)
562       // (2) initialize client and server (contextClient and contextServer)
[1071]563       MPI_Comm inter;
[1054]564       if (serverLevel < 2)
565       {
566         MPI_Comm contextInterComm;
567         MPI_Intercomm_create(intraComm, 0, CXios::globalComm, leaderRank, 10+leaderRank, &contextInterComm);
568         MPI_Intercomm_merge(contextInterComm,1,&inter);
569         MPI_Barrier(inter);
570         MPI_Comm_free(&inter);
571         context->initServer(intraComm,contextInterComm);
572         contextInterComms.push_back(contextInterComm);
[1071]573
[1054]574       }
[1148]575       // Secondary server: create communication channel with a primary server
576       // (1) duplicate interComm with a primary server
577       // (2) initialize client and server (contextClient and contextServer)
578       // Remark: in the case of the secondary server there is no need to create an interComm calling MPI_Intercomm_create,
579       //         because interComm of CContext is defined on the same processes as the interComm of CServer.
580       //         So just duplicate it.
[1054]581       else if (serverLevel == 2)
582       {
[1071]583         MPI_Comm_dup(interCommLeft.front(), &inter);
584         contextInterComms.push_back(inter);
585         context->initServer(intraComm, contextInterComms.back());
[1054]586       }
587
[1148]588       // Primary server:
589       // (1) send create context message to secondary servers
590       // (2) initialize communication channels with secondary servers (create contextClient and contextServer)
[1021]591       if (serverLevel == 1)
[983]592       {
[1054]593         int i = 0, size;
594         CMessage msg;
595         int messageSize;
596         MPI_Comm_size(intraComm, &size) ;
597         for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++, ++i)
598         {
599           StdString str = contextId +"_server_" + boost::lexical_cast<string>(i);
[1077]600           msg<<str<<size<<rank_ ;
[1054]601           messageSize = msg.size() ;
602           buff = new char[messageSize] ;
603           CBufferOut buffer(buff,messageSize) ;
604           buffer<<msg ;
[1152]605           MPI_Send(buff, buffer.count(), MPI_CHAR, sndServerGlobalRanks[i], 1, CXios::globalComm) ;
[1071]606           MPI_Comm_dup(*it, &inter);
607           contextInterComms.push_back(inter);
608           MPI_Comm_dup(intraComm, &inter);
609           contextIntraComms.push_back(inter);
610           context->initClient(contextIntraComms.back(), contextInterComms.back()) ;
[1054]611           delete [] buff ;
612         }
[983]613       }
[490]614     }
615
[300]616     void CServer::contextEventLoop(void)
617     {
[1130]618       bool isFinalized ;
[300]619       map<string,CContext*>::iterator it ;
[983]620
[490]621       for(it=contextList.begin();it!=contextList.end();it++)
[300]622       {
[1130]623         isFinalized=it->second->isFinalized();
624         if (isFinalized)
[300]625         {
626           contextList.erase(it) ;
627           break ;
628         }
[1054]629         else
[1139]630           it->second->checkBuffersAndListen();
[300]631       }
632     }
[490]633
[1148]634     //! Get rank of the current process in the intraComm
[490]635     int CServer::getRank()
636     {
[1167]637       int rank;
638       MPI_Comm_rank(intraComm,&rank);
639       return rank;
[490]640     }
641
[1168]642     vector<int>& CServer::getSecondaryServerGlobalRanks()
643     {
644       return sndServerGlobalRanks;
645     }
646
[523]647    /*!
648    * Open a file specified by a suffix and an extension and use it for the given file buffer.
649    * The file name will be suffix+rank+extension.
650    *
651    * \param fileName[in] protype file name
652    * \param ext [in] extension of the file
653    * \param fb [in/out] the file buffer
654    */
655    void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
656    {
657      StdStringStream fileNameClient;
658      int numDigit = 0;
659      int size = 0;
[1021]660      int id;
[523]661      MPI_Comm_size(CXios::globalComm, &size);
662      while (size)
663      {
664        size /= 10;
665        ++numDigit;
666      }
[1167]667      id = rank_; //getRank();
[497]668
[1021]669      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << id << ext;
[523]670      fb->open(fileNameClient.str().c_str(), std::ios::out);
671      if (!fb->is_open())
672        ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
673              << std::endl << "Can not open <" << fileNameClient << "> file to write the server log(s).");
674    }
[490]675
[523]676    /*!
677    * \brief Open a file stream to write the info logs
678    * Open a file stream with a specific file name suffix+rank
679    * to write the info logs.
680    * \param fileName [in] protype file name
681    */
682    void CServer::openInfoStream(const StdString& fileName)
683    {
684      std::filebuf* fb = m_infoStream.rdbuf();
685      openStream(fileName, ".out", fb);
[490]686
[523]687      info.write2File(fb);
688      report.write2File(fb);
689    }
[490]690
[523]691    //! Write the info logs to standard output
692    void CServer::openInfoStream()
693    {
694      info.write2StdOut();
695      report.write2StdOut();
696    }
[490]697
[523]698    //! Close the info logs file if it opens
699    void CServer::closeInfoStream()
700    {
701      if (m_infoStream.is_open()) m_infoStream.close();
702    }
703
704    /*!
705    * \brief Open a file stream to write the error log
706    * Open a file stream with a specific file name suffix+rank
707    * to write the error log.
708    * \param fileName [in] protype file name
709    */
710    void CServer::openErrorStream(const StdString& fileName)
711    {
712      std::filebuf* fb = m_errorStream.rdbuf();
713      openStream(fileName, ".err", fb);
714
715      error.write2File(fb);
716    }
717
718    //! Write the error log to standard error output
719    void CServer::openErrorStream()
720    {
721      error.write2StdErr();
722    }
723
724    //! Close the error log file if it opens
725    void CServer::closeErrorStream()
726    {
727      if (m_errorStream.is_open()) m_errorStream.close();
728    }
[300]729}
Note: See TracBrowser for help on using the repository browser.