source: XIOS/dev/dev_ym/XIOS_SERVICES/src/cxios.cpp @ 1761

Last change on this file since 1761 was 1761, checked in by ymipsl, 5 years ago

implementing first guess for service functionnalities.

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