source: XIOS3/trunk/src/server.cpp @ 2564

Last change on this file since 2564 was 2547, checked in by ymipsl, 10 months ago

Major update :

  • New method to lock and unlock one-sided windows (window_dynamic) to avoid network overhead
  • Introducing multithreading on server sided to manage more efficiently dead-lock occuring (similar to co-routine which will be available and implemented in futur c++ standard), based on c++ threads
  • Suprression of old "attached mode" which is replaced by online writer and reder filters

YM

  • 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: 25.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 "mem_checker.hpp"
16#include "event_scheduler.hpp"
17#include "string_tools.hpp"
18#include "ressources_manager.hpp"
19#include "services_manager.hpp"
20#include "contexts_manager.hpp"
21#include "servers_ressource.hpp"
22#include "services.hpp"
23#include "pool_node.hpp"
24#include <cstdio>
25#include "workflow_graph.hpp"
26#include "release_static_allocation.hpp"
27#include "thread_manager.hpp"
28#include <sys/stat.h>
29#include <unistd.h>
30
31
32
33namespace xios
34{
35    MPI_Comm CServer::intraComm_ ;
36    MPI_Comm CServer::serversComm_ ;
37    std::list<MPI_Comm> CServer::interCommLeft ;
38    std::list<MPI_Comm> CServer::interCommRight ;
39    std::list<MPI_Comm> CServer::contextInterComms;
40    std::list<MPI_Comm> CServer::contextIntraComms;
41    int CServer::serverLevel = 0 ;
42    int CServer::nbContexts = 0;
43    bool CServer::isRoot = false ;
44    int CServer::rank_ = INVALID_RANK;
45    StdOFStream CServer::m_infoStream;
46    StdOFStream CServer::m_errorStream;
47    map<string,CContext*> CServer::contextList ;
48    vector<int> CServer::sndServerGlobalRanks;
49    bool CServer::finished=false ;
50    bool CServer::is_MPI_Initialized ;
51    CEventScheduler* CServer::eventScheduler = 0;
52    CServersRessource* CServer::serversRessource_=nullptr ;
53    CThirdPartyDriver* CServer::driver_ =nullptr ;
54
55       
56    void CServer::initialize(void)
57    {
58     
59      MPI_Comm serverComm ;
60      int initialized ;
61      MPI_Initialized(&initialized) ;
62      if (initialized) is_MPI_Initialized=true ;
63      else is_MPI_Initialized=false ;
64      MPI_Comm globalComm=CXios::getGlobalComm() ;
65      /////////////////////////////////////////
66      ///////////// PART 1 ////////////////////
67      /////////////////////////////////////////
68      // don't use OASIS
69      if (!CXios::usingOasis)
70      {
71        if (!is_MPI_Initialized) 
72        {
73          int required = MPI_THREAD_SERIALIZED ;
74          int provided ;
75          MPI_Init_thread(NULL,NULL, required, &provided) ;
76        }
77       
78        // split the global communicator
79        // get hash from all model to attribute a unique color (int) and then split to get client communicator
80        // every mpi process of globalComm (MPI_COMM_WORLD) must participate
81         
82        int commRank, commSize ;
83        MPI_Comm_rank(globalComm,&commRank) ;
84        MPI_Comm_size(globalComm,&commSize) ;
85
86        std::hash<string> hashString ;
87        size_t hashServer=hashString(CXios::xiosCodeId) ;
88         
89        size_t* hashAll = new size_t[commSize] ;
90        MPI_Allgather(&hashServer,1,MPI_SIZE_T,hashAll,1,MPI_SIZE_T,globalComm) ;
91         
92        int color=0 ;
93        map<size_t,int> listHash ;
94        for(int i=0 ; i<=commSize ; i++) 
95          if (listHash.count(hashAll[i])==0) 
96          {
97            listHash[hashAll[i]]=color ;
98            color=color+1 ;
99          }
100        color=listHash[hashServer] ;
101        delete[] hashAll ;
102
103        MPI_Comm_split(globalComm, color, commRank, &serverComm) ;
104      }
105      else // using OASIS
106      {
107       
108        if (!is_MPI_Initialized) 
109        {
110          int required = MPI_THREAD_SERIALIZED ;
111          int provided ;
112          MPI_Init_thread(NULL,NULL, required, &provided) ;
113        }
114
115        driver_ = new CThirdPartyDriver();
116
117        driver_->getComponentCommunicator( serverComm );
118      }
119      MPI_Comm_dup(serverComm, &intraComm_);
120     
121      CTimer::get("XIOS").resume() ;
122      CTimer::get("XIOS server").resume() ;
123      CTimer::get("XIOS initialize").resume() ;
124 
125      /////////////////////////////////////////
126      ///////////// PART 2 ////////////////////
127      /////////////////////////////////////////
128     
129
130      // Create the XIOS communicator for every process which is related
131      // to XIOS, as well on client side as on server side
132      MPI_Comm xiosGlobalComm ;
133      string strIds=CXios::getin<string>("clients_code_id","") ;
134      vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ;
135      if (strIds.empty())
136      {
137        // no code Ids given, suppose XIOS initialisation is global           
138        int commRank, commGlobalRank, serverLeader, clientLeader,serverRemoteLeader,clientRemoteLeader ;
139        MPI_Comm splitComm,interComm ;
140        MPI_Comm_rank(globalComm,&commGlobalRank) ;
141        MPI_Comm_split(globalComm, 1, commGlobalRank, &splitComm) ;
142        MPI_Comm_rank(splitComm,&commRank) ;
143        if (commRank==0) serverLeader=commGlobalRank ;
144        else serverLeader=0 ;
145        clientLeader=0 ;
146        MPI_Allreduce(&clientLeader,&clientRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ;
147        MPI_Allreduce(&serverLeader,&serverRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ;
148        MPI_Intercomm_create(splitComm, 0, globalComm, clientRemoteLeader,1341,&interComm) ;
149        MPI_Intercomm_merge(interComm,false,&xiosGlobalComm) ;
150        CXios::setXiosComm(xiosGlobalComm) ;
151      }
152      else
153      {
154
155        xiosGlobalCommByFileExchange(serverComm) ;
156
157      }
158     
159      /////////////////////////////////////////
160      ///////////// PART 4 ////////////////////
161      //  create servers intra communicator  //
162      /////////////////////////////////////////
163     
164      int commRank ;
165      MPI_Comm_rank(CXios::getXiosComm(), &commRank) ;
166      MPI_Comm_split(CXios::getXiosComm(),true,commRank,&serversComm_) ;
167     
168      CXios::setUsingServer() ;
169
170      /////////////////////////////////////////
171      ///////////// PART 5 ////////////////////
172      //       redirect files output         //
173      /////////////////////////////////////////
174     
175      CServer::openInfoStream(CXios::serverFile);
176      CServer::openErrorStream(CXios::serverFile);
177
178      CMemChecker::logMem( "CServer::initialize" );
179
180      /////////////////////////////////////////
181      ///////////// PART 4 ////////////////////
182      /////////////////////////////////////////
183
184      CXios::launchDaemonsManager(true) ;
185     
186      /////////////////////////////////////////
187      ///////////// PART 5 ////////////////////
188      /////////////////////////////////////////
189
190      // create the services
191
192      auto ressourcesManager=CXios::getRessourcesManager() ;
193      auto servicesManager=CXios::getServicesManager() ;
194      auto contextsManager=CXios::getContextsManager() ;
195      auto daemonsManager=CXios::getDaemonsManager() ;
196      auto serversRessource=CServer::getServersRessource() ;
197
198      int rank;
199      MPI_Comm_rank(intraComm_, &rank) ;
200      if (rank==0) isRoot=true;
201      else isRoot=false;
202
203      if (serversRessource->isServerLeader())
204      {
205        // creating pool
206        CPoolNodeGroup::get("xios","pool_definition")->solveDescInheritance(true) ;
207        vector<CPoolNode*> pools = CPoolNodeGroup::get("xios","pool_definition")->getAllChildren();
208        for(auto& pool : pools) pool->allocateRessources() ;
209       
210        int nbRessources = ressourcesManager->getFreeRessourcesSize() ;
211        if (nbRessources>0)
212        {
213          if (!CXios::usingServer2)
214          {
215            ressourcesManager->createPool(CXios::defaultPoolId, nbRessources) ;
216            if (CThreadManager::isUsingThreads()) 
217              while(!ressourcesManager->hasPool(CXios::defaultPoolId)) 
218              {
219                daemonsManager->eventLoop() ;
220                CThreadManager::yield() ;
221              }
222            else ressourcesManager->waitPoolRegistration(CXios::defaultPoolId) ;
223         
224            servicesManager->createServices(CXios::defaultPoolId, CXios::defaultWriterId, CServicesManager::WRITER,nbRessources,1) ;
225            if (CThreadManager::isUsingThreads()) 
226              while(!servicesManager->hasService(CXios::defaultPoolId, CXios::defaultWriterId,0)) 
227              {
228                daemonsManager->eventLoop() ;
229                CThreadManager::yield() ;
230              }
231            else servicesManager->waitServiceRegistration(CXios::defaultPoolId, CXios::defaultWriterId) ;
232           
233            servicesManager->createServicesOnto(CXios::defaultPoolId, CXios::defaultReaderId, CServicesManager::READER, CXios::defaultWriterId) ;
234            if (CThreadManager::isUsingThreads()) 
235            {
236              daemonsManager->eventLoop() ;
237              while(!servicesManager->hasService(CXios::defaultPoolId, CXios::defaultReaderId, 0)) CThreadManager::yield() ;
238            }
239            else servicesManager->waitServiceRegistration(CXios::defaultPoolId, CXios::defaultReaderId) ;
240          }
241          else
242          {
243            int nprocsServer = nbRessources*CXios::ratioServer2/100.;
244            int nprocsGatherer = nbRessources - nprocsServer ;
245         
246            int nbPoolsServer2 = CXios::nbPoolsServer2 ;
247            if (nbPoolsServer2 == 0) nbPoolsServer2 = nprocsServer;
248            ressourcesManager->createPool(CXios::defaultPoolId, nbRessources) ;
249            if (CThreadManager::isUsingThreads()) 
250              while(!ressourcesManager->hasPool(CXios::defaultPoolId)) 
251              {
252                daemonsManager->eventLoop() ;
253                CThreadManager::yield() ;
254              }
255            else ressourcesManager->waitPoolRegistration(CXios::defaultPoolId) ;
256
257            servicesManager->createServices(CXios::defaultPoolId,  CXios::defaultGathererId, CServicesManager::GATHERER, nprocsGatherer, 1) ;
258            if (CThreadManager::isUsingThreads()) 
259              while(!servicesManager->hasService(CXios::defaultPoolId, CXios::defaultGathererId,0)) 
260              {
261                daemonsManager->eventLoop() ;
262                CThreadManager::yield() ;
263              }
264            else servicesManager->waitServiceRegistration(CXios::defaultPoolId, CXios::defaultGathererId) ;
265
266            servicesManager->createServicesOnto(CXios::defaultPoolId, CXios::defaultReaderId, CServicesManager::READER, CXios::defaultGathererId) ;
267            if (CThreadManager::isUsingThreads()) 
268              while(!servicesManager->hasService(CXios::defaultPoolId, CXios::defaultReaderId, 0)) 
269              {
270                daemonsManager->eventLoop() ;
271                CThreadManager::yield() ;
272              }
273            else servicesManager->waitServiceRegistration(CXios::defaultPoolId, CXios::defaultReaderId) ;
274           
275            servicesManager->createServices(CXios::defaultPoolId,  CXios::defaultWriterId, CServicesManager::WRITER, nprocsServer, nbPoolsServer2) ;
276            if (CThreadManager::isUsingThreads())
277              for(int i=0; i<nbPoolsServer2; i++)
278                while(!servicesManager->hasService(CXios::defaultPoolId, CXios::defaultWriterId,i)) 
279                {
280                  daemonsManager->eventLoop() ;
281                  CThreadManager::yield() ;
282                }
283            else servicesManager->waitServiceRegistration(CXios::defaultPoolId, CXios::defaultWriterId) ;
284          }
285        }
286//        servicesManager->createServices(CXios::defaultPoolId,  CXios::defaultServicesId, CServicesManager::ALL_SERVICES, nbRessources, 1) ;
287      }
288
289      MPI_Request req ;
290      MPI_Status status ;
291      MPI_Ibarrier(getServersRessource()->getCommunicator(),&req) ; // be sure that all services are created now, could be remove later if more asynchronisity
292      int ok=false ;
293      while (!ok)
294      {
295        daemonsManager->eventLoop() ;
296        if (CThreadManager::isUsingThreads()) CThreadManager::yield();
297        MPI_Test(&req,&ok,&status) ;
298      }
299
300
301      //testingEventScheduler() ;
302/*
303      MPI_Request req ;
304      MPI_Status status ;
305      MPI_Ibarrier(CXios::getXiosComm(),&req) ; // be sure that all services are created now, could be remove later if more asynchronisity
306      int ok=false ;
307      while (!ok)
308      {
309        daemonsManager->eventLoop() ;
310        MPI_Test(&req,&ok,&status) ;
311      }
312*/
313      CTimer::get("XIOS initialize").suspend() ;
314
315      /////////////////////////////////////////
316      ///////////// PART 5 ////////////////////
317      /////////////////////////////////////////
318      // loop on event loop
319
320      bool finished=false ;
321      CTimer::get("XIOS event loop").resume() ;
322
323      while (!finished)
324      {
325        finished=daemonsManager->eventLoop() ;
326        if (CThreadManager::isUsingThreads()) CThreadManager::yield() ;
327      }
328      CTimer::get("XIOS event loop").suspend() ;
329
330      // Delete CContext
331      //CObjectTemplate<CContext>::cleanStaticDataStructure();
332    }
333
334
335    void CServer::testingEventScheduler(void)
336    {
337      CXios::getPoolRessource()->getEventScheduler()->registerEvent(1,10) ;
338      CXios::getPoolRessource()->getEventScheduler()->registerEvent(2,10) ;
339      if (CXios::getPoolRessource()->hasService(CXios::defaultGathererId,0))
340      {
341        CXios::getPoolRessource()->getService(CXios::defaultGathererId,0)-> getEventScheduler()->registerEvent(1,100) ;
342        CXios::getPoolRessource()->getService(CXios::defaultGathererId,0)-> getEventScheduler()->registerEvent(2,100) ;
343        CXios::getPoolRessource()->getService(CXios::defaultGathererId,0)-> getEventScheduler()->registerEvent(3,100) ;
344      }
345      if (CXios::getPoolRessource()->hasService(CXios::defaultWriterId,0))
346      {
347        CXios::getPoolRessource()->getService(CXios::defaultWriterId,0)-> getEventScheduler()->registerEvent(1,1000) ;
348        CXios::getPoolRessource()->getService(CXios::defaultWriterId,0)-> getEventScheduler()->registerEvent(2,1000) ;
349      }
350      CXios::getPoolRessource()->getEventScheduler()->registerEvent(3,10) ;
351      CXios::getPoolRessource()->getEventScheduler()->registerEvent(4,10) ;
352     
353      if (CXios::getPoolRessource()->hasService(CXios::defaultGathererId,0))
354      {
355        CXios::getPoolRessource()->getService(CXios::defaultGathererId,0)-> getEventScheduler()->registerEvent(4,100) ;
356        CXios::getPoolRessource()->getService(CXios::defaultGathererId,0)-> getEventScheduler()->registerEvent(5,100) ;
357      }
358      if (CXios::getPoolRessource()->hasService(CXios::defaultWriterId,0))
359      {
360        CXios::getPoolRessource()->getService(CXios::defaultWriterId,0)-> getEventScheduler()->registerEvent(3,1000) ;
361        CXios::getPoolRessource()->getService(CXios::defaultWriterId,0)-> getEventScheduler()->registerEvent(4,1000) ;
362        CXios::getPoolRessource()->getService(CXios::defaultWriterId,0)-> getEventScheduler()->registerEvent(5,1000) ;
363      }
364      CXios::getPoolRessource()->getEventScheduler()->registerEvent(5,10) ;
365      CXios::getPoolRessource()->getEventScheduler()->registerEvent(6,10) ;
366     
367      int numEvents=0 ;
368      int poolEvent=1 ;
369      int gatherEvent=1 ;
370      int writerEvent=1 ;
371      do
372      {
373        if (CXios::getPoolRessource()->getEventScheduler()->queryEvent(poolEvent,10))
374        {
375          CXios::getPoolRessource()->getEventScheduler()->popEvent() ;
376          MPI_Barrier(CXios::getPoolRessource()->getCommunicator());
377          poolEvent++ ;
378          numEvents++;
379        }
380       
381        if (CXios::getPoolRessource()->getEventScheduler()->queryEvent(gatherEvent,100))
382        {
383          CXios::getPoolRessource()->getEventScheduler()->popEvent() ;
384          MPI_Barrier(CXios::getPoolRessource()->getService(CXios::defaultGathererId,0)->getCommunicator());
385          gatherEvent++ ;
386          numEvents++;
387        }
388
389        if (CXios::getPoolRessource()->getEventScheduler()->queryEvent(writerEvent,1000))
390        {
391          CXios::getPoolRessource()->getEventScheduler()->popEvent() ;
392          MPI_Barrier(CXios::getPoolRessource()->getService(CXios::defaultWriterId,0)->getCommunicator());
393          writerEvent++ ;
394          numEvents++;
395        }
396
397       
398      } while (numEvents!=11) ;
399
400    }
401
402
403    void  CServer::xiosGlobalCommByFileExchange(MPI_Comm serverComm)
404    {
405       
406      MPI_Comm globalComm=CXios::getGlobalComm() ;
407      MPI_Comm xiosGlobalComm ;
408     
409      string strIds=CXios::getin<string>("clients_code_id","") ;
410      vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ;
411     
412      int commRank, globalRank ;
413      MPI_Comm_rank(serverComm, &commRank) ;
414      MPI_Comm_rank(globalComm, &globalRank) ;
415      string serverFileName("__xios_publisher::"+CXios::xiosCodeId+"__to_remove__") ;
416
417      if (commRank==0) // if root process publish name
418      { 
419        std::ofstream ofs (serverFileName, std::ofstream::out);
420        ofs<<globalRank ;
421        ofs.close();
422      }
423       
424      vector<int> clientsRank(clientsCodeId.size()) ;
425      for(int i=0;i<clientsRank.size();i++)
426      {
427        std::ifstream ifs ;
428        string fileName=("__xios_publisher::"+clientsCodeId[i]+"__to_remove__") ;
429        struct stat buffer;
430        do {
431        } while( stat(fileName.c_str(), &buffer) != 0 );
432        sleep(1);
433        ifs.open(fileName, ifstream::in) ;
434        ifs>>clientsRank[i] ;
435        //cout <<  "\t\t read: " << clientsRank[i] << " in " << fileName << endl;
436        ifs.close() ; 
437      }
438
439      MPI_Comm intraComm ;
440      MPI_Comm_dup(serverComm,&intraComm) ;
441      MPI_Comm interComm ;
442      for(int i=0 ; i<clientsRank.size(); i++)
443      { 
444        MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[i], 3141, &interComm);
445        interCommLeft.push_back(interComm) ;
446        MPI_Comm_free(&intraComm) ;
447        MPI_Intercomm_merge(interComm,false, &intraComm ) ;
448      }
449      xiosGlobalComm=intraComm ; 
450      MPI_Barrier(xiosGlobalComm);
451      if (commRank==0) std::remove(serverFileName.c_str()) ;
452      MPI_Barrier(xiosGlobalComm);
453
454      CXios::setXiosComm(xiosGlobalComm) ;
455     
456    }
457
458
459    void  CServer::xiosGlobalCommByPublishing(MPI_Comm serverComm)
460    {
461        // untested, need to be tested on a true MPI-2 compliant library
462
463        // try to discover other client/server
464/*
465        // publish server name
466        char portName[MPI_MAX_PORT_NAME];
467        int ierr ;
468        int commRank ;
469        MPI_Comm_rank(serverComm, &commRank) ;
470       
471        if (commRank==0) // if root process publish name
472        { 
473          MPI_Open_port(MPI_INFO_NULL, portName);
474          MPI_Publish_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName);
475        }
476
477        MPI_Comm intraComm=serverComm ;
478        MPI_Comm interComm ;
479        for(int i=0 ; i<clientsCodeId.size(); i++)
480        { 
481          MPI_Comm_accept(portName, MPI_INFO_NULL, 0, intraComm, &interComm);
482          MPI_Intercomm_merge(interComm,false, &intraComm ) ;
483        }
484*/     
485    }
486
487   /*!
488    * Root process is listening for an order sent by client to call "oasis_enddef".
489    * The root client of a compound send the order (tag 5). It is probed and received.
490    * When the order has been received from each coumpound, the server root process ping the order to the root processes of the secondary levels of servers (if any).
491    * After, it also inform (asynchronous call) other processes of the communicator that the oasis_enddef call must be done
492    */
493   
494     void CServer::listenOasisEnddef(void)
495     {
496        int flag ;
497        MPI_Status status ;
498        list<MPI_Comm>::iterator it;
499        int msg ;
500        static int nbCompound=0 ;
501        int size ;
502        static bool sent=false ;
503        static MPI_Request* allRequests ;
504        static MPI_Status* allStatus ;
505
506
507        if (sent)
508        {
509          MPI_Comm_size(intraComm_,&size) ;
510          MPI_Testall(size,allRequests, &flag, allStatus) ;
511          if (flag==true)
512          {
513            delete [] allRequests ;
514            delete [] allStatus ;
515            sent=false ;
516          }
517        }
518       
519
520        for(it=interCommLeft.begin();it!=interCommLeft.end();it++)
521        {
522           MPI_Status status ;
523           traceOff() ;
524           MPI_Iprobe(0,5,*it,&flag,&status) ;  // tags oasis_endded = 5
525           traceOn() ;
526           if (flag==true)
527           {
528              MPI_Recv(&msg,1,MPI_INT,0,5,*it,&status) ; // tags oasis_endded = 5
529              nbCompound++ ;
530              if (nbCompound==interCommLeft.size())
531              {
532                MPI_Comm_size(intraComm_,&size) ;
533                allRequests= new MPI_Request[size] ;
534                allStatus= new MPI_Status[size] ;
535                for(int i=0;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,5,intraComm_,&allRequests[i]) ; // tags oasis_endded = 5
536                sent=true ;
537              }
538           }
539        }
540}
541     
542   /*!
543    * Processes probes message from root process if oasis_enddef call must be done.
544    * When the order is received it is scheduled to be treated in a synchronized way by all server processes of the communicator
545    */
546     void CServer::listenRootOasisEnddef(void)
547     {
548       int flag ;
549       MPI_Status status ;
550       const int root=0 ;
551       int msg ;
552       static bool eventSent=false ;
553
554       if (eventSent)
555       {
556         boost::hash<string> hashString;
557         size_t hashId = hashString("oasis_enddef");
558
559         if (CXios::getPoolRessource()->getEventScheduler()->queryEvent(0,hashId))
560         {
561           CXios::getPoolRessource()->getEventScheduler()->popEvent() ;
562           driver_->endSynchronizedDefinition() ;
563           eventSent=false ;
564         }
565       }
566         
567       traceOff() ;
568       MPI_Iprobe(root,5,intraComm_, &flag, &status) ;
569       traceOn() ;
570       if (flag==true)
571       {
572           MPI_Recv(&msg,1,MPI_INT,root,5,intraComm_,&status) ; // tags oasis_endded = 5
573           boost::hash<string> hashString;
574           size_t hashId = hashString("oasis_enddef");
575           CXios::getPoolRessource()->getEventScheduler()->registerEvent(0,hashId);
576           eventSent=true ;
577       }
578     }
579
580    void CServer::finalize(void)
581    {
582      CTimer::get("XIOS").suspend() ;
583      CTimer::get("XIOS server").suspend() ;
584      delete eventScheduler ;
585
586      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
587        MPI_Comm_free(&(*it));
588
589      for (std::list<MPI_Comm>::iterator it = contextIntraComms.begin(); it != contextIntraComms.end(); it++)
590        MPI_Comm_free(&(*it));
591
592        for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++)
593          MPI_Comm_free(&(*it));
594
595//      MPI_Comm_free(&intraComm);
596      CXios::finalizeDaemonsManager();
597      finalizeServersRessource();
598     
599      CContext::removeAllContexts() ; // free memory for related context
600         
601      CXios::getMpiGarbageCollector().release() ; // release unfree MPI ressources
602
603      CMemChecker::logMem( "CServer::finalize", true );
604      if (!is_MPI_Initialized)
605      {
606        if (CXios::usingOasis) delete driver_;
607        else MPI_Finalize() ;
608      }
609      report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl  ;
610      report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl  ;
611      report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl  ;
612      report(100)<<CTimer::getAllCumulatedTime()<<endl ;
613      if (CXios::reportMemory)
614      {
615        report(100)<<CMemChecker::getAllCumulatedMem()<<endl ;
616      }
617     
618      CWorkflowGraph::drawWorkFlowGraph_server();
619      xios::releaseStaticAllocation() ; // free memory from static allocation
620    }
621
622    /*!
623    * Open a file specified by a suffix and an extension and use it for the given file buffer.
624    * The file name will be suffix+rank+extension.
625    *
626    * \param fileName[in] protype file name
627    * \param ext [in] extension of the file
628    * \param fb [in/out] the file buffer
629    */
630    void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
631    {
632      StdStringStream fileNameServer;
633      int numDigit = 0;
634      int commSize = 0;
635      int commRank ;
636      int id;
637     
638      MPI_Comm_size(CXios::getGlobalComm(), &commSize);
639      MPI_Comm_rank(CXios::getGlobalComm(), &commRank);
640
641      while (commSize)
642      {
643        commSize /= 10;
644        ++numDigit;
645      }
646      id = commRank;
647
648      fileNameServer << fileName << "_" << std::setfill('0') << std::setw(numDigit) << id << ext;
649      fb->open(fileNameServer.str().c_str(), std::ios::out);
650      if (!fb->is_open())
651        ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
652              << std::endl << "Can not open <" << fileNameServer.str() << "> file to write the server log(s).");
653    }
654
655    /*!
656    * \brief Open a file stream to write the info logs
657    * Open a file stream with a specific file name suffix+rank
658    * to write the info logs.
659    * \param fileName [in] protype file name
660    */
661    void CServer::openInfoStream(const StdString& fileName)
662    {
663      std::filebuf* fb = m_infoStream.rdbuf();
664      openStream(fileName, ".out", fb);
665
666      info.write2File(fb);
667      report.write2File(fb);
668    }
669
670    //! Write the info logs to standard output
671    void CServer::openInfoStream()
672    {
673      info.write2StdOut();
674      report.write2StdOut();
675    }
676
677    //! Close the info logs file if it opens
678    void CServer::closeInfoStream()
679    {
680      if (m_infoStream.is_open()) m_infoStream.close();
681    }
682
683    /*!
684    * \brief Open a file stream to write the error log
685    * Open a file stream with a specific file name suffix+rank
686    * to write the error log.
687    * \param fileName [in] protype file name
688    */
689    void CServer::openErrorStream(const StdString& fileName)
690    {
691      std::filebuf* fb = m_errorStream.rdbuf();
692      openStream(fileName, ".err", fb);
693
694      error.write2File(fb);
695    }
696
697    //! Write the error log to standard error output
698    void CServer::openErrorStream()
699    {
700      error.write2StdErr();
701    }
702
703    //! Close the error log file if it opens
704    void CServer::closeErrorStream()
705    {
706      if (m_errorStream.is_open()) m_errorStream.close();
707    }
708
709    void CServer::launchServersRessource(MPI_Comm serverComm)
710    {
711      serversRessource_ = new CServersRessource(serverComm) ;
712    }
713
714    void  CServer::finalizeServersRessource(void) 
715    { 
716      delete serversRessource_; serversRessource_=nullptr ;
717    }
718}
Note: See TracBrowser for help on using the repository browser.