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

Last change on this file since 2535 was 2535, checked in by jderouillat, 11 months ago

Backport [2448,2503,2516], ocean component in generic_testcase, memory_report option and nvertex name

  • 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.7 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 <boost/functional/hash.hpp>
8#include "mpi.hpp"
9#include "memory.hpp"
10#include <new>
11#include "memtrack.hpp"
12#include "registry.hpp"
13#include "ressources_manager.hpp"
14#include "services_manager.hpp"
15#include "servers_ressource.hpp"
16#include "mem_checker.hpp"
17#include <unistd.h>
18
19namespace xios
20{
21  string CXios::rootFile="./iodef.xml" ;
22  string CXios::xiosCodeId="xios.x" ;
23  string CXios::clientFile="./xios_client";
24  string CXios::serverFile="./xios_server";
25  string CXios::serverPrmFile="./xios_server1";
26  string CXios::serverSndFile="./xios_server2";
27  const string CXios::defaultPoolId="default_pool_id" ;
28  const string CXios::defaultServerId="default_server_id" ;
29  const string CXios::defaultWriterId="default_writer_id" ;
30  const string CXios::defaultReaderId="default_reader_id" ;
31  const string CXios::defaultGathererId="default_gatherer_id" ;
32  const string CXios::defaultServicesId="default_services_id" ;
33 
34  bool CXios::xiosStack = true;
35  bool CXios::systemStack = false;
36
37  bool CXios::isClient ;
38  bool CXios::isServer ;
39 
40  MPI_Comm CXios::globalComm ;
41  MPI_Comm CXios::xiosComm ;
42
43  bool CXios::usingOasis ;
44  bool CXios::usingServer = false;
45  bool CXios::usingServer2 = false;
46  int CXios::ratioServer2 = 50;
47  int CXios::nbPoolsServer2 = 1;
48  double CXios::bufferSizeFactor = 1.0;
49  const double CXios::defaultBufferSizeFactor = 1.0;
50  StdSize CXios::minBufferSize = 64 * sizeof(double);
51  StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ;
52  bool CXios::printLogs2Files;
53  bool CXios::isOptPerformance = true;
54  CRegistry* CXios::globalRegistry = 0;
55  double CXios::recvFieldTimeout = 300.0;
56  bool CXios::checkEventSync=false ;
57  bool CXios::checkSumRecv=false ;
58  bool CXios::checkSumSend=false ;
59  bool CXios::logMemory=false ;
60  bool CXios::reportMemory=true ;
61
62  CDaemonsManager*    CXios::daemonsManager_=nullptr ;
63  CRessourcesManager* CXios::ressourcesManager_=nullptr ;
64  CServicesManager*   CXios::servicesManager_=nullptr ;
65  CContextsManager*   CXios::contextsManager_=nullptr ;
66  CCouplerManager*    CXios::couplerManager_=nullptr ;
67  CRegistryManager*   CXios::registryManager_=nullptr ;
68
69  CMpiGarbageCollector CXios::MpiGarbageCollector_  ;
70
71  //! Parse configuration file and create some objects from it
72  void CXios::initialize()
73  {
74    set_new_handler(noMemory);
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    }
84    parseFile(rootFile);
85    parseXiosConfig();
86    chdir( startPath );
87  }
88
89  /*!
90  \brief Parse xios part of configuration file (.iodef.xml)
91   Both client and server need information returned from this function
92  */
93  void CXios::parseXiosConfig()
94  {
95    usingOasis=getin<bool>("using_oasis",false) ;
96    usingServer=getin<bool>("using_server",false) ;
97    usingServer2=getin<bool>("using_server2",false) ;
98    ratioServer2=getin<int>("ratio_server2",50);
99    nbPoolsServer2=getin<int>("number_pools_server2",0);
100    info.setLevel(getin<int>("info_level",0)) ;
101    report.setLevel(getin<int>("info_level",50));
102    printLogs2Files=getin<bool>("print_file",false);
103
104    xiosStack=getin<bool>("xios_stack",true) ;
105    systemStack=getin<bool>("system_stack",false) ;
106    if (xiosStack && systemStack)
107    {
108      xiosStack = false;
109    }
110
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
121    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
122    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
123    maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
124    recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
125    if (recvFieldTimeout < 0.0)
126      ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
127
128    checkEventSync = getin<bool>("check_event_sync", checkEventSync);
129   
130    checkSumSend = getin<bool>("checksum_send_fields", false);
131    checkSumRecv = getin<bool>("checksum_recv_fields", false);
132
133    logMemory = getin<bool>("log_memory", false);
134    reportMemory = getin<bool>("memory_report", true);
135
136    globalComm=MPI_COMM_WORLD ;
137  }
138
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  */
145  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
146  TRY
147  {
148    initialize() ;
149
150    isClient = true;
151
152    //CClient::initialize(codeId,localComm,returnComm) ;
153    CClient::initialize(codeId,localComm,returnComm) ;
154   
155    // If there are no server processes then we are in attached mode
156    // and the clients are also servers
157    isServer = !usingServer;
158
159    if (printLogs2Files)
160    {
161      CClient::openInfoStream(clientFile);
162      CClient::openErrorStream(clientFile);
163    }
164    else
165    {
166      CClient::openInfoStream();
167      CClient::openErrorStream();
168    }
169    CMemChecker::logMem("CXios::initClientSide");
170  }
171  CATCH
172
173  void CXios::clientFinalize(void)
174  {
175     CMemChecker::logMem("CXios::clientFinalize", true);
176
177     CClient::finalize() ;
178         
179#ifdef XIOS_MEMTRACK
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
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();
201#endif
202
203     CClient::closeInfoStream();
204
205#endif
206  }
207
208  //! Init server by parsing only xios part of config file
209  void CXios::initServer()
210  {
211    set_new_handler(noMemory);
212    std::set<StdString> parseList;
213    parseList.insert("xios");
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    }
223    xml::CXMLParser::ParseFile(rootFile, parseList);
224    parseXiosConfig();
225    chdir( startPath );
226  }
227
228  //! Initialize server then put it into listening state
229  void CXios::initServerSide(void)
230  {
231    CMemChecker::get("xios").resume() ;
232    initServer();
233    isClient = false;
234    isServer = true;
235
236    // Initialize all aspects MPI
237    CServer::initialize();
238    CServer::finalize();
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
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() ;
261#endif
262#endif
263    CMemChecker::get("xios").suspend() ;
264    CServer::closeInfoStream();
265  }
266
267  //! Parse configuration file
268  void CXios::parseFile(const string& filename)
269  {
270    xml::CXMLParser::ParseFile(filename);
271  }
272
273  //! Set using server
274  void CXios::setUsingServer()
275  {
276    usingServer = true;
277  }
278
279  //! Unset using server
280  void CXios::setNotUsingServer()
281  {
282    usingServer = false;
283  }
284
285  void CXios::launchRegistryManager(bool isXiosServer)
286  {
287    registryManager_ = new CRegistryManager(isXiosServer) ;
288  }
289
290  void CXios::launchRessourcesManager(bool isXiosServer)
291  {
292    ressourcesManager_ = new CRessourcesManager(isXiosServer) ;
293  }
294
295  void CXios::launchCouplerManager(bool isXiosServer)
296  {
297    couplerManager_ = new CCouplerManager(isXiosServer) ;
298  }
299
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
315 
316  void CXios::finalizeRegistryManager()
317  {
318    delete registryManager_;
319  }
320
321  void CXios::finalizeRessourcesManager()
322  {
323    delete ressourcesManager_;
324  }
325
326  void CXios::finalizeCouplerManager()
327  {
328    delete couplerManager_;
329  }
330
331  void CXios::finalizeServicesManager()
332  {
333    delete servicesManager_  ;
334  }
335
336  void CXios::finalizeContextsManager()
337  {
338    delete contextsManager_  ;
339  }
340 
341  void CXios::finalizeDaemonsManager()
342  {
343    delete daemonsManager_  ;
344  }
345 
346  CPoolRessource* CXios::getPoolRessource(void)
347  {
348    if (isClient) return CClient::getPoolRessource() ;
349    else if (isServer) return CServer::getServersRessource()->getPoolRessource() ;
350   
351    MISSING_RETURN( "CPoolRessource* CXios::getPoolRessource()" );
352    return nullptr;
353  }
354}
355
Note: See TracBrowser for help on using the repository browser.