source: XIOS/dev/XIOS_DEV_CMIP6/src/cxios.cpp @ 1376

Last change on this file since 1376 was 1376, checked in by ymipsl, 6 years ago

Set the default timeout in reception at 300s instead 10s

YM

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