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

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

Two server levels: correcting buffer allocations on client side of primary server. The buffer size is calculated according to the number of files sent to each secondary server.

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