source: XIOS3/trunk/src/cxios.cpp

Last change on this file was 2629, checked in by jderouillat, 2 months ago

Delete boost dependencies, the few features used are replaced by functions stored in extern/boost_extraction

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