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

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

Two server levels: merging with trunk r1137.
There are bugs.

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