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

Last change on this file since 1227 was 1227, checked in by oabramkina, 7 years ago

Minor modifications:

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