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

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

Minor modifications for secondary server:

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