source: XIOS3/trunk/src/cxios.cpp @ 2547

Last change on this file since 2547 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: 10.9 KB
RevLine 
[300]1
[591]2#include "xios_spl.hpp"
[300]3#include "cxios.hpp"
[342]4#include "client.hpp"
5#include "server.hpp"
[346]6#include "xml_parser.hpp"
[300]7#include <boost/functional/hash.hpp>
[382]8#include "mpi.hpp"
[400]9#include "memory.hpp"
10#include <new>
[401]11#include "memtrack.hpp"
[697]12#include "registry.hpp"
[1761]13#include "ressources_manager.hpp"
14#include "services_manager.hpp"
15#include "servers_ressource.hpp"
[2212]16#include "mem_checker.hpp"
[2438]17#include <unistd.h>
[300]18
[335]19namespace xios
[300]20{
21  string CXios::rootFile="./iodef.xml" ;
22  string CXios::xiosCodeId="xios.x" ;
[499]23  string CXios::clientFile="./xios_client";
24  string CXios::serverFile="./xios_server";
[1021]25  string CXios::serverPrmFile="./xios_server1";
26  string CXios::serverSndFile="./xios_server2";
[1761]27  const string CXios::defaultPoolId="default_pool_id" ;
28  const string CXios::defaultServerId="default_server_id" ;
[2407]29  const string CXios::defaultWriterId="default_writer_id" ;
30  const string CXios::defaultReaderId="default_reader_id" ;
[1761]31  const string CXios::defaultGathererId="default_gatherer_id" ;
[2335]32  const string CXios::defaultServicesId="default_services_id" ;
[1761]33 
[1622]34  bool CXios::xiosStack = true;
35  bool CXios::systemStack = false;
36
[300]37  bool CXios::isClient ;
38  bool CXios::isServer ;
[1761]39 
[1639]40  MPI_Comm CXios::globalComm ;
[1761]41  MPI_Comm CXios::xiosComm ;
42
[300]43  bool CXios::usingOasis ;
[491]44  bool CXios::usingServer = false;
[1021]45  bool CXios::usingServer2 = false;
46  int CXios::ratioServer2 = 50;
[1243]47  int CXios::nbPoolsServer2 = 1;
[718]48  double CXios::bufferSizeFactor = 1.0;
49  const double CXios::defaultBufferSizeFactor = 1.0;
[2176]50  StdSize CXios::minBufferSize = 64 * sizeof(double);
[1227]51  StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ;
[523]52  bool CXios::printLogs2Files;
[511]53  bool CXios::isOptPerformance = true;
[697]54  CRegistry* CXios::globalRegistry = 0;
[1375]55  double CXios::recvFieldTimeout = 300.0;
[1377]56  bool CXios::checkEventSync=false ;
[2329]57  bool CXios::checkSumRecv=false ;
58  bool CXios::checkSumSend=false ;
[2420]59  bool CXios::logMemory=false ;
[2535]60  bool CXios::reportMemory=true ;
[1761]61
62  CDaemonsManager*    CXios::daemonsManager_=nullptr ;
63  CRessourcesManager* CXios::ressourcesManager_=nullptr ;
64  CServicesManager*   CXios::servicesManager_=nullptr ;
65  CContextsManager*   CXios::contextsManager_=nullptr ;
[1878]66  CCouplerManager*    CXios::couplerManager_=nullptr ;
[2209]67  CRegistryManager*   CXios::registryManager_=nullptr ;
[1761]68
[2310]69  CMpiGarbageCollector CXios::MpiGarbageCollector_  ;
70
[512]71  //! Parse configuration file and create some objects from it
[300]72  void CXios::initialize()
73  {
[400]74    set_new_handler(noMemory);
[2438]75    char startPath[256];
76    getcwd(startPath, sizeof(startPath));
77    if(const char* userPath = std::getenv("XIOS_IODEF_PATH"))
78    {
79      if ( chdir( userPath ) != 0)
80      {
81        ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl );
82      }
83    }
[346]84    parseFile(rootFile);
[511]85    parseXiosConfig();
[2438]86    chdir( startPath );
[511]87  }
88
[512]89  /*!
90  \brief Parse xios part of configuration file (.iodef.xml)
91   Both client and server need information returned from this function
92  */
[511]93  void CXios::parseXiosConfig()
94  {
[311]95    usingOasis=getin<bool>("using_oasis",false) ;
[506]96    usingServer=getin<bool>("using_server",false) ;
[1021]97    usingServer2=getin<bool>("using_server2",false) ;
98    ratioServer2=getin<int>("ratio_server2",50);
[1519]99    nbPoolsServer2=getin<int>("number_pools_server2",0);
[311]100    info.setLevel(getin<int>("info_level",0)) ;
[512]101    report.setLevel(getin<int>("info_level",50));
[523]102    printLogs2Files=getin<bool>("print_file",false);
[512]103
[1622]104    xiosStack=getin<bool>("xios_stack",true) ;
105    systemStack=getin<bool>("system_stack",false) ;
106    if (xiosStack && systemStack)
107    {
108      xiosStack = false;
109    }
110
[511]111    StdString bufMemory("memory");
112    StdString bufPerformance("performance");
113    StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance);
114    std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower);
115    if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false;
116    else if (0 != bufOpt.compare(bufPerformance))
117    {
118      ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl );
119    }
120
[718]121    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
[719]122    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
[1227]123    maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
[1376]124    recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
[1158]125    if (recvFieldTimeout < 0.0)
126      ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
[718]127
[1377]128    checkEventSync = getin<bool>("check_event_sync", checkEventSync);
[2329]129   
130    checkSumSend = getin<bool>("checksum_send_fields", false);
131    checkSumRecv = getin<bool>("checksum_recv_fields", false);
[2535]132
[2420]133    logMemory = getin<bool>("log_memory", false);
[2535]134    reportMemory = getin<bool>("memory_report", true);
[1639]135
[300]136    globalComm=MPI_COMM_WORLD ;
137  }
138
[512]139  /*!
140  Initialize client
141  \param [in] codeId identity of context
142  \param [in] localComm local communicator
143  \param [in/out] returnComm communicator corresponding to group of client with same codeId
144  */
[1639]145  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
[1622]146  TRY
[300]147  {
148    initialize() ;
[490]149
[1639]150    isClient = true;
[491]151
[1761]152    //CClient::initialize(codeId,localComm,returnComm) ;
[491]153    CClient::initialize(codeId,localComm,returnComm) ;
[1761]154   
[956]155    // If there are no server processes then we are in attached mode
156    // and the clients are also servers
157    isServer = !usingServer;
[490]158
[523]159    if (printLogs2Files)
160    {
[499]161      CClient::openInfoStream(clientFile);
[523]162      CClient::openErrorStream(clientFile);
163    }
[490]164    else
[523]165    {
[490]166      CClient::openInfoStream();
[523]167      CClient::openErrorStream();
168    }
[2418]169    CMemChecker::logMem("CXios::initClientSide");
[490]170  }
[1622]171  CATCH
[300]172
173  void CXios::clientFinalize(void)
174  {
[2420]175     CMemChecker::logMem("CXios::clientFinalize", true);
[2418]176
[490]177     CClient::finalize() ;
[1761]178         
[401]179#ifdef XIOS_MEMTRACK
[1158]180
181#ifdef XIOS_MEMTRACK_LIGHT
182       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
183       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
184#endif
185
186#ifdef XIOS_MEMTRACK_FULL
[2212]187      report(0) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
188      report(0) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
189     
190      ofstream memReport ;
191      std::filebuf* fb = memReport.rdbuf();
192      CClient::openStream(clientFile, ".mem", fb);
193     
194      MemTrack::TrackListMemoryUsage() ;
195      size_t memtrack_blocks=0 ;
196      memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ;
197      size_t memtrack_size=0 ;
198      memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ;
199      MemTrack::TrackDumpBlocks(memReport, memtrack_blocks,memtrack_size);
200      memReport.close();
[401]201#endif
[1158]202
203     CClient::closeInfoStream();
204
205#endif
[490]206  }
207
[512]208  //! Init server by parsing only xios part of config file
[509]209  void CXios::initServer()
210  {
211    set_new_handler(noMemory);
212    std::set<StdString> parseList;
213    parseList.insert("xios");
[2438]214    char startPath[256];
215    getcwd(startPath, sizeof(startPath));
216    if(const char* userPath = std::getenv("XIOS_IODEF_PATH"))
217    {
218      if ( chdir( userPath ) != 0)
219      {
220        ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl );
221      }
222    }
[509]223    xml::CXMLParser::ParseFile(rootFile, parseList);
[511]224    parseXiosConfig();
[2438]225    chdir( startPath );
[509]226  }
227
[512]228  //! Initialize server then put it into listening state
[1054]229  void CXios::initServerSide(void)
[300]230  {
[2212]231    CMemChecker::get("xios").resume() ;
[1639]232    initServer();
[1638]233    isClient = false;
234    isServer = true;
235
[1021]236    // Initialize all aspects MPI
237    CServer::initialize();
[490]238    CServer::finalize();
[1158]239
240#ifdef XIOS_MEMTRACK
241
242#ifdef XIOS_MEMTRACK_LIGHT
243       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
244       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
245#endif
246
247#ifdef XIOS_MEMTRACK_FULL
[2212]248      report(0) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
249      report(0) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
250      ofstream memReport ;
251      std::filebuf* fb = memReport.rdbuf();
252      CClient::openStream(serverFile, ".mem", fb);
253     
254      MemTrack::TrackListMemoryUsage() ;
255      size_t memtrack_blocks=0 ;
256      memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ;
257      size_t memtrack_size=0 ;
258      memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ;
259      MemTrack::TrackDumpBlocks(memReport,memtrack_blocks,memtrack_size);
260      memReport.close() ;
[1158]261#endif
262#endif
[2212]263    CMemChecker::get("xios").suspend() ;
[490]264    CServer::closeInfoStream();
265  }
266
[512]267  //! Parse configuration file
[346]268  void CXios::parseFile(const string& filename)
269  {
[490]270    xml::CXMLParser::ParseFile(filename);
[346]271  }
[491]272
[512]273  //! Set using server
[491]274  void CXios::setUsingServer()
275  {
276    usingServer = true;
277  }
278
[512]279  //! Unset using server
[491]280  void CXios::setNotUsingServer()
281  {
282    usingServer = false;
283  }
[1761]284
[2209]285  void CXios::launchRegistryManager(bool isXiosServer)
286  {
287    registryManager_ = new CRegistryManager(isXiosServer) ;
288  }
289
[1761]290  void CXios::launchRessourcesManager(bool isXiosServer)
291  {
292    ressourcesManager_ = new CRessourcesManager(isXiosServer) ;
293  }
294
[1878]295  void CXios::launchCouplerManager(bool isXiosServer)
296  {
297    couplerManager_ = new CCouplerManager(isXiosServer) ;
298  }
299
[1761]300  void CXios::launchServicesManager(bool isXiosServer)
301  {
302    servicesManager_ = new CServicesManager(isXiosServer) ;
303  }
304
305  void CXios::launchContextsManager(bool isXiosServer)
306  {
307    contextsManager_ = new CContextsManager(isXiosServer) ;
308  }
309 
310  void CXios::launchDaemonsManager(bool isXiosServer)
311  {
312    daemonsManager_ = new CDaemonsManager(isXiosServer) ;
313  }
314
[2547]315  void CXios::launchThreadManager(bool isXiosServer)
316  {
317    CThreadManager::initialize(isXiosServer) ;
318  }
319
[2209]320  void CXios::finalizeRegistryManager()
321  {
322    delete registryManager_;
323  }
324
[1764]325  void CXios::finalizeRessourcesManager()
326  {
327    delete ressourcesManager_;
328  }
329
[1878]330  void CXios::finalizeCouplerManager()
331  {
332    delete couplerManager_;
333  }
334
[1764]335  void CXios::finalizeServicesManager()
336  {
337    delete servicesManager_  ;
338  }
339
340  void CXios::finalizeContextsManager()
341  {
342    delete contextsManager_  ;
343  }
344 
[2547]345  void CXios::finalizeThreadManager()
346  {
347    CThreadManager::finalize()  ;
348  }
349
[1764]350  void CXios::finalizeDaemonsManager()
351  {
352    delete daemonsManager_  ;
353  }
354 
[1761]355  CPoolRessource* CXios::getPoolRessource(void)
356  {
357    if (isClient) return CClient::getPoolRessource() ;
358    else if (isServer) return CServer::getServersRessource()->getPoolRessource() ;
[2282]359   
360    MISSING_RETURN( "CPoolRessource* CXios::getPoolRessource()" );
361    return nullptr;
[1761]362  }
[300]363}
[1761]364
Note: See TracBrowser for help on using the repository browser.