source: XIOS/dev/dev_olga/src/cxios.cpp @ 1168

Last change on this file since 1168 was 1168, checked in by oabramkina, 4 years ago

Two server levels: merging registries of secondary server pools into one and then writing the merged one.

Tested on curie with toy_cmip6.

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