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
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"
[2439]9#include <unistd.h>
[400]10#include "memory.hpp"
11#include <new>
[401]12#include "memtrack.hpp"
[2428]13#include "mem_checker.hpp"
[697]14#include "registry.hpp"
[300]15
[1704]16#include "graphviz.hpp"
17
[335]18namespace xios
[300]19{
20  string CXios::rootFile="./iodef.xml" ;
21  string CXios::xiosCodeId="xios.x" ;
[499]22  string CXios::clientFile="./xios_client";
23  string CXios::serverFile="./xios_server";
[1021]24  string CXios::serverPrmFile="./xios_server1";
25  string CXios::serverSndFile="./xios_server2";
[490]26
[1622]27  bool CXios::xiosStack = true;
28  bool CXios::systemStack = false;
29
[300]30  bool CXios::isClient ;
31  bool CXios::isServer ;
[1639]32  MPI_Comm CXios::globalComm ;
[300]33  bool CXios::usingOasis ;
[491]34  bool CXios::usingServer = false;
[1021]35  bool CXios::usingServer2 = false;
36  int CXios::ratioServer2 = 50;
[1243]37  int CXios::nbPoolsServer2 = 1;
[718]38  double CXios::bufferSizeFactor = 1.0;
39  const double CXios::defaultBufferSizeFactor = 1.0;
[719]40  StdSize CXios::minBufferSize = 1024 * sizeof(double);
[1227]41  StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ;
[523]42  bool CXios::printLogs2Files;
[511]43  bool CXios::isOptPerformance = true;
[697]44  CRegistry* CXios::globalRegistry = 0;
[1375]45  double CXios::recvFieldTimeout = 300.0;
[1377]46  bool CXios::checkEventSync=false ;
[2330]47  bool CXios::checkSumRecv=false ;
48  bool CXios::checkSumSend=false ;
[2428]49  bool CXios::logMemory=false ;
[2503]50  bool CXios::reportMemory=true ;
[1376]51 
[512]52  //! Parse configuration file and create some objects from it
[300]53  void CXios::initialize()
54  {
[400]55    set_new_handler(noMemory);
[2439]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    }
[346]65    parseFile(rootFile);
[511]66    parseXiosConfig();
[2439]67    chdir( startPath );
[511]68  }
69
[512]70  /*!
71  \brief Parse xios part of configuration file (.iodef.xml)
72   Both client and server need information returned from this function
73  */
[511]74  void CXios::parseXiosConfig()
75  {
[311]76    usingOasis=getin<bool>("using_oasis",false) ;
[506]77    usingServer=getin<bool>("using_server",false) ;
[1021]78    usingServer2=getin<bool>("using_server2",false) ;
79    ratioServer2=getin<int>("ratio_server2",50);
[1519]80    nbPoolsServer2=getin<int>("number_pools_server2",0);
[311]81    info.setLevel(getin<int>("info_level",0)) ;
[512]82    report.setLevel(getin<int>("info_level",50));
[523]83    printLogs2Files=getin<bool>("print_file",false);
[512]84
[1622]85    xiosStack=getin<bool>("xios_stack",true) ;
86    systemStack=getin<bool>("system_stack",false) ;
87    if (xiosStack && systemStack)
88    {
89      xiosStack = false;
90    }
91
[511]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
[718]102    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
[719]103    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
[1227]104    maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
[1376]105    recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
[1158]106    if (recvFieldTimeout < 0.0)
107      ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
[718]108
[1377]109    checkEventSync = getin<bool>("check_event_sync", checkEventSync);
[1639]110
[2330]111    checkSumSend = getin<bool>("checksum_send_fields", false);
112    checkSumRecv = getin<bool>("checksum_recv_fields", false);
[2428]113
114    logMemory = getin<bool>("log_memory", false);
[2503]115    reportMemory = getin<bool>("memory_report", true);
[2330]116 
[300]117    globalComm=MPI_COMM_WORLD ;
118  }
119
[512]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  */
[1639]126  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
[1622]127  TRY
[300]128  {
129    initialize() ;
[490]130
[1639]131    isClient = true;
[491]132
133    CClient::initialize(codeId,localComm,returnComm) ;
[697]134    if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
[491]135
[956]136    // If there are no server processes then we are in attached mode
137    // and the clients are also servers
138    isServer = !usingServer;
[490]139
[523]140    if (printLogs2Files)
141    {
[499]142      CClient::openInfoStream(clientFile);
[523]143      CClient::openErrorStream(clientFile);
144    }
[490]145    else
[523]146    {
[490]147      CClient::openInfoStream();
[523]148      CClient::openErrorStream();
149    }
[2428]150    CMemChecker::logMem("CXios::initClientSide");
[490]151  }
[1622]152  CATCH
[300]153
154  void CXios::clientFinalize(void)
155  {
[2428]156     CMemChecker::logMem("CXios::clientFinalize", true);
[490]157     CClient::finalize() ;
[697]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 ;
[1704]163       CGraphviz::buildWorkflowGraphVisjs_with_info();
[697]164     }
[490]165
[401]166#ifdef XIOS_MEMTRACK
[1158]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
[401]174     MemTrack::TrackListMemoryUsage() ;
[490]175     MemTrack::TrackDumpBlocks();
[401]176#endif
[1158]177
178     CClient::closeInfoStream();
179
180#endif
[490]181  }
182
[512]183  //! Init server by parsing only xios part of config file
[509]184  void CXios::initServer()
185  {
186    set_new_handler(noMemory);
187    std::set<StdString> parseList;
188    parseList.insert("xios");
[2439]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    }
[509]198    xml::CXMLParser::ParseFile(rootFile, parseList);
[511]199    parseXiosConfig();
[2439]200    chdir( startPath );
[509]201  }
202
[512]203  //! Initialize server then put it into listening state
[1054]204  void CXios::initServerSide(void)
[300]205  {
[1639]206    initServer();
[1638]207    isClient = false;
208    isServer = true;
209
[1021]210    // Initialize all aspects MPI
211    CServer::initialize();
[1330]212    if (CServer::getRank()==0 && CServer::serverLevel != 1) globalRegistry = new CRegistry(CServer::intraComm) ;
[697]213   
[523]214    if (printLogs2Files)
215    {
[1021]216      if (CServer::serverLevel == 0)
[983]217      {
218        CServer::openInfoStream(serverFile);
219        CServer::openErrorStream(serverFile);
220      }
[1021]221      else if (CServer::serverLevel == 1)
[983]222      {
[1021]223        CServer::openInfoStream(serverPrmFile);
224        CServer::openErrorStream(serverPrmFile);
[983]225      }
226      else
227      {
[1021]228        CServer::openInfoStream(serverSndFile);
229        CServer::openErrorStream(serverSndFile);
[983]230      }
[523]231    }
[490]232    else
[523]233    {
[490]234      CServer::openInfoStream();
[523]235      CServer::openErrorStream();
236    }
[2428]237    CMemChecker::logMem( "CServer::initialize" );
[490]238
[491]239    // Enter the loop to listen message from Client
[490]240    CServer::eventLoop();
241
[491]242    // Finalize
[1234]243    if (CServer::serverLevel == 0)
[1168]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    {
[1243]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
[1168]258      if (CServer::serverLevel == 2)
259      {
260        vector<int>& secondaryServerGlobalRanks = CServer::getSecondaryServerGlobalRanks();
261        int firstPoolGlobalRank = secondaryServerGlobalRanks[0];
262        int rankGlobal;
[1639]263        MPI_Comm_rank(globalComm, &rankGlobal);
[1168]264
[1243]265        // Merge registries defined on each pools
266        CRegistry globalRegistrySndServers (CServer::intraComm);
267
[1168]268        // All pools (except the first): send globalRegistry to the first pool
[1243]269        for (int i=1; i<secondaryServerGlobalRanks.size(); i++)
[1168]270        {
[1243]271          if (rankGlobal == secondaryServerGlobalRanks[i])
272          {
273            globalRegistrySndServers.mergeRegistry(*globalRegistry) ;
274            int registrySize = globalRegistrySndServers.size();
[1639]275            MPI_Send(&registrySize,1,MPI_LONG,firstPoolGlobalRank,15,CXios::globalComm) ;
[1243]276            CBufferOut buffer(registrySize) ;
277            globalRegistrySndServers.toBuffer(buffer) ;
[1639]278            MPI_Send(buffer.start(),registrySize,MPI_CHAR,firstPoolGlobalRank,15,CXios::globalComm) ;
[1243]279          }
[1168]280        }
281
282        // First pool: receive globalRegistry of all secondary server pools, merge and write the resultant registry
[1243]283        if (rankGlobal == firstPoolGlobalRank)
[1168]284        {
[1639]285          MPI_Status status;
[1168]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;
[1639]294            MPI_Recv(&registrySize, 1, MPI_LONG, rank, 15, CXios::globalComm, &status);
[1168]295            recvBuff = new char[registrySize];
[1639]296            MPI_Recv(recvBuff, registrySize, MPI_CHAR, rank, 15, CXios::globalComm, &status);
[1168]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    }
[490]311    CServer::finalize();
[1158]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
[490]325    CServer::closeInfoStream();
326  }
327
[512]328  //! Parse configuration file
[346]329  void CXios::parseFile(const string& filename)
330  {
[490]331    xml::CXMLParser::ParseFile(filename);
[346]332  }
[491]333
[512]334  //! Set using server
[491]335  void CXios::setUsingServer()
336  {
337    usingServer = true;
338  }
339
[512]340  //! Unset using server
[491]341  void CXios::setNotUsingServer()
342  {
343    usingServer = false;
344  }
[300]345}
Note: See TracBrowser for help on using the repository browser.