source: XIOS2/trunk/src/cxios.cpp @ 2428

Last change on this file since 2428 was 2428, checked in by jderouillat, 17 months ago

Backport the XIOS3 system to log the memory consumption (commit ID [2418-2420,2425-2426])

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