source: XIOS/dev/dev_olga/src/server.cpp @ 1152

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

Two servel levels: correcting initialization in case of non-contiguous server ranks.
Tests on Curie: test_complete.

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