source: XIOS2/trunk/src/cxios.cpp

Last change on this file was 2503, checked in by jderouillat, 14 months ago

Add an option called memory_report (in the xios context, default is true) to activate the memory reporting operated at each xios routine call from models

  • 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 <unistd.h>
10#include "memory.hpp"
11#include <new>
12#include "memtrack.hpp"
13#include "mem_checker.hpp"
14#include "registry.hpp"
15
16#include "graphviz.hpp"
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
27  bool CXios::xiosStack = true;
28  bool CXios::systemStack = false;
29
30  bool CXios::isClient ;
31  bool CXios::isServer ;
32  MPI_Comm CXios::globalComm ;
33  bool CXios::usingOasis ;
34  bool CXios::usingServer = false;
35  bool CXios::usingServer2 = false;
36  int CXios::ratioServer2 = 50;
37  int CXios::nbPoolsServer2 = 1;
38  double CXios::bufferSizeFactor = 1.0;
39  const double CXios::defaultBufferSizeFactor = 1.0;
40  StdSize CXios::minBufferSize = 1024 * sizeof(double);
41  StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ;
42  bool CXios::printLogs2Files;
43  bool CXios::isOptPerformance = true;
44  CRegistry* CXios::globalRegistry = 0;
45  double CXios::recvFieldTimeout = 300.0;
46  bool CXios::checkEventSync=false ;
47  bool CXios::checkSumRecv=false ;
48  bool CXios::checkSumSend=false ;
49  bool CXios::logMemory=false ;
50  bool CXios::reportMemory=true ;
51 
52  //! Parse configuration file and create some objects from it
53  void CXios::initialize()
54  {
55    set_new_handler(noMemory);
56    char startPath[256];
57    getcwd(startPath, sizeof(startPath));
58    if(const char* userPath = std::getenv("XIOS_IODEF_PATH"))
59    {
60      if ( chdir( userPath ) != 0)
61      {
62        ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl );
63      }
64    }
65    parseFile(rootFile);
66    parseXiosConfig();
67    chdir( startPath );
68  }
69
70  /*!
71  \brief Parse xios part of configuration file (.iodef.xml)
72   Both client and server need information returned from this function
73  */
74  void CXios::parseXiosConfig()
75  {
76    usingOasis=getin<bool>("using_oasis",false) ;
77    usingServer=getin<bool>("using_server",false) ;
78    usingServer2=getin<bool>("using_server2",false) ;
79    ratioServer2=getin<int>("ratio_server2",50);
80    nbPoolsServer2=getin<int>("number_pools_server2",0);
81    info.setLevel(getin<int>("info_level",0)) ;
82    report.setLevel(getin<int>("info_level",50));
83    printLogs2Files=getin<bool>("print_file",false);
84
85    xiosStack=getin<bool>("xios_stack",true) ;
86    systemStack=getin<bool>("system_stack",false) ;
87    if (xiosStack && systemStack)
88    {
89      xiosStack = false;
90    }
91
92    StdString bufMemory("memory");
93    StdString bufPerformance("performance");
94    StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance);
95    std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower);
96    if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false;
97    else if (0 != bufOpt.compare(bufPerformance))
98    {
99      ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl );
100    }
101
102    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
103    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
104    maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
105    recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
106    if (recvFieldTimeout < 0.0)
107      ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
108
109    checkEventSync = getin<bool>("check_event_sync", checkEventSync);
110
111    checkSumSend = getin<bool>("checksum_send_fields", false);
112    checkSumRecv = getin<bool>("checksum_recv_fields", false);
113
114    logMemory = getin<bool>("log_memory", false);
115    reportMemory = getin<bool>("memory_report", true);
116 
117    globalComm=MPI_COMM_WORLD ;
118  }
119
120  /*!
121  Initialize client
122  \param [in] codeId identity of context
123  \param [in] localComm local communicator
124  \param [in/out] returnComm communicator corresponding to group of client with same codeId
125  */
126  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
127  TRY
128  {
129    initialize() ;
130
131    isClient = true;
132
133    CClient::initialize(codeId,localComm,returnComm) ;
134    if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
135
136    // If there are no server processes then we are in attached mode
137    // and the clients are also servers
138    isServer = !usingServer;
139
140    if (printLogs2Files)
141    {
142      CClient::openInfoStream(clientFile);
143      CClient::openErrorStream(clientFile);
144    }
145    else
146    {
147      CClient::openInfoStream();
148      CClient::openErrorStream();
149    }
150    CMemChecker::logMem("CXios::initClientSide");
151  }
152  CATCH
153
154  void CXios::clientFinalize(void)
155  {
156     CMemChecker::logMem("CXios::clientFinalize", true);
157     CClient::finalize() ;
158     if (CClient::getRank()==0)
159     {
160       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
161       globalRegistry->toFile("xios_registry.bin") ;
162       delete globalRegistry ;
163       CGraphviz::buildWorkflowGraphVisjs_with_info();
164     }
165
166#ifdef XIOS_MEMTRACK
167
168#ifdef XIOS_MEMTRACK_LIGHT
169       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
170       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
171#endif
172
173#ifdef XIOS_MEMTRACK_FULL
174     MemTrack::TrackListMemoryUsage() ;
175     MemTrack::TrackDumpBlocks();
176#endif
177
178     CClient::closeInfoStream();
179
180#endif
181  }
182
183  //! Init server by parsing only xios part of config file
184  void CXios::initServer()
185  {
186    set_new_handler(noMemory);
187    std::set<StdString> parseList;
188    parseList.insert("xios");
189    char startPath[256];
190    getcwd(startPath, sizeof(startPath));
191    if(const char* userPath = std::getenv("XIOS_IODEF_PATH"))
192    {
193      if ( chdir( userPath ) != 0)
194      {
195        ERROR("CXios::initialize()", << "XIOS_IODEF_PATH not defined correctly : " << userPath << endl );
196      }
197    }
198    xml::CXMLParser::ParseFile(rootFile, parseList);
199    parseXiosConfig();
200    chdir( startPath );
201  }
202
203  //! Initialize server then put it into listening state
204  void CXios::initServerSide(void)
205  {
206    initServer();
207    isClient = false;
208    isServer = true;
209
210    // Initialize all aspects MPI
211    CServer::initialize();
212    if (CServer::getRank()==0 && CServer::serverLevel != 1) globalRegistry = new CRegistry(CServer::intraComm) ;
213   
214    if (printLogs2Files)
215    {
216      if (CServer::serverLevel == 0)
217      {
218        CServer::openInfoStream(serverFile);
219        CServer::openErrorStream(serverFile);
220      }
221      else if (CServer::serverLevel == 1)
222      {
223        CServer::openInfoStream(serverPrmFile);
224        CServer::openErrorStream(serverPrmFile);
225      }
226      else
227      {
228        CServer::openInfoStream(serverSndFile);
229        CServer::openErrorStream(serverSndFile);
230      }
231    }
232    else
233    {
234      CServer::openInfoStream();
235      CServer::openErrorStream();
236    }
237    CMemChecker::logMem( "CServer::initialize" );
238
239    // Enter the loop to listen message from Client
240    CServer::eventLoop();
241
242    // Finalize
243    if (CServer::serverLevel == 0)
244    {
245      if (CServer::getRank()==0)
246      {
247        info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
248        globalRegistry->toFile("xios_registry.bin") ;
249        delete globalRegistry ;
250      }
251    }
252    else
253    {
254      // If using two server levels:
255      // (1) merge registries on each pool
256      // (2) send merged registries to the first pool
257      // (3) merge received registries on the first pool
258      if (CServer::serverLevel == 2)
259      {
260        vector<int>& secondaryServerGlobalRanks = CServer::getSecondaryServerGlobalRanks();
261        int firstPoolGlobalRank = secondaryServerGlobalRanks[0];
262        int rankGlobal;
263        MPI_Comm_rank(globalComm, &rankGlobal);
264
265        // Merge registries defined on each pools
266        CRegistry globalRegistrySndServers (CServer::intraComm);
267
268        // All pools (except the first): send globalRegistry to the first pool
269        for (int i=1; i<secondaryServerGlobalRanks.size(); i++)
270        {
271          if (rankGlobal == secondaryServerGlobalRanks[i])
272          {
273            globalRegistrySndServers.mergeRegistry(*globalRegistry) ;
274            int registrySize = globalRegistrySndServers.size();
275            MPI_Send(&registrySize,1,MPI_LONG,firstPoolGlobalRank,15,CXios::globalComm) ;
276            CBufferOut buffer(registrySize) ;
277            globalRegistrySndServers.toBuffer(buffer) ;
278            MPI_Send(buffer.start(),registrySize,MPI_CHAR,firstPoolGlobalRank,15,CXios::globalComm) ;
279          }
280        }
281
282        // First pool: receive globalRegistry of all secondary server pools, merge and write the resultant registry
283        if (rankGlobal == firstPoolGlobalRank)
284        {
285          MPI_Status status;
286          char* recvBuff;
287
288          globalRegistrySndServers.mergeRegistry(*globalRegistry) ;
289
290          for (int i=1; i< secondaryServerGlobalRanks.size(); i++)
291          {
292            int rank = secondaryServerGlobalRanks[i];
293            int registrySize = 0;
294            MPI_Recv(&registrySize, 1, MPI_LONG, rank, 15, CXios::globalComm, &status);
295            recvBuff = new char[registrySize];
296            MPI_Recv(recvBuff, registrySize, MPI_CHAR, rank, 15, CXios::globalComm, &status);
297            CBufferIn buffer(recvBuff, registrySize) ;
298            CRegistry recvRegistry;
299            recvRegistry.fromBuffer(buffer) ;
300            globalRegistrySndServers.mergeRegistry(recvRegistry) ;
301            delete[] recvBuff;
302          }
303
304          info(80)<<"Write data base Registry"<<endl<<globalRegistrySndServers.toString()<<endl ;
305          globalRegistrySndServers.toFile("xios_registry.bin") ;
306
307        }
308      }
309      delete globalRegistry;
310    }
311    CServer::finalize();
312
313#ifdef XIOS_MEMTRACK
314
315#ifdef XIOS_MEMTRACK_LIGHT
316       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
317       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
318#endif
319
320#ifdef XIOS_MEMTRACK_FULL
321     MemTrack::TrackListMemoryUsage() ;
322     MemTrack::TrackDumpBlocks();
323#endif
324#endif
325    CServer::closeInfoStream();
326  }
327
328  //! Parse configuration file
329  void CXios::parseFile(const string& filename)
330  {
331    xml::CXMLParser::ParseFile(filename);
332  }
333
334  //! Set using server
335  void CXios::setUsingServer()
336  {
337    usingServer = true;
338  }
339
340  //! Unset using server
341  void CXios::setNotUsingServer()
342  {
343    usingServer = false;
344  }
345}
Note: See TracBrowser for help on using the repository browser.