source: XIOS/trunk/src/cxios.cpp @ 2297

Last change on this file since 2297 was 1704, checked in by yushan, 5 years ago

Introducing the new graph functionality. Attribute build_workflow_graph=.TRUE. is used in the field definition section in the xml file to enable the workflow graph of the field and other fields referecing to it. A more detailed document will be available soon on the graph fuctionality.

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