source: XIOS/dev/dev_trunk_omp/src/cxios.cpp @ 1733

Last change on this file since 1733 was 1681, checked in by yushan, 5 years ago

MARK: Dynamic workflow graph developement. Branch up to date with trunk @1676. Bug fixed

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