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

Last change on this file since 2257 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
RevLine 
[300]1
[591]2#include "xios_spl.hpp"
[300]3#include "cxios.hpp"
[342]4#include "client.hpp"
5#include "server.hpp"
[346]6#include "xml_parser.hpp"
[300]7#include <boost/functional/hash.hpp>
[382]8#include "mpi.hpp"
[400]9#include "memory.hpp"
10#include <new>
[401]11#include "memtrack.hpp"
[697]12#include "registry.hpp"
[1646]13#include "timer.hpp"
14#ifdef _usingEP
[1601]15using namespace ep_lib;
[1646]16#endif
[1668]17#include "graphviz.hpp"
[335]18namespace xios
[300]19{
[1601]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";
[490]26
[1646]27  bool CXios::xiosStack = true;
28  bool CXios::systemStack = false;
29
[300]30  bool CXios::isClient ;
31  bool CXios::isServer ;
32  MPI_Comm CXios::globalComm ;
33  bool CXios::usingOasis ;
[491]34  bool CXios::usingServer = false;
[1021]35  bool CXios::usingServer2 = false;
36  int CXios::ratioServer2 = 50;
[1243]37  int CXios::nbPoolsServer2 = 1;
[718]38  double CXios::bufferSizeFactor = 1.0;
39  const double CXios::defaultBufferSizeFactor = 1.0;
[719]40  StdSize CXios::minBufferSize = 1024 * sizeof(double);
[1227]41  StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ;
[523]42  bool CXios::printLogs2Files;
[511]43  bool CXios::isOptPerformance = true;
[697]44  CRegistry* CXios::globalRegistry = 0;
[1375]45  double CXios::recvFieldTimeout = 300.0;
[1377]46  bool CXios::checkEventSync=false ;
[1376]47 
[512]48  //! Parse configuration file and create some objects from it
[300]49  void CXios::initialize()
50  {
[400]51    set_new_handler(noMemory);
[1601]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
[511]60    parseXiosConfig();
61  }
62
[512]63  /*!
64  \brief Parse xios part of configuration file (.iodef.xml)
65   Both client and server need information returned from this function
66  */
[511]67  void CXios::parseXiosConfig()
68  {
[311]69    usingOasis=getin<bool>("using_oasis",false) ;
[506]70    usingServer=getin<bool>("using_server",false) ;
[1021]71    usingServer2=getin<bool>("using_server2",false) ;
72    ratioServer2=getin<int>("ratio_server2",50);
[1519]73    nbPoolsServer2=getin<int>("number_pools_server2",0);
[311]74    info.setLevel(getin<int>("info_level",0)) ;
[512]75    report.setLevel(getin<int>("info_level",50));
[523]76    printLogs2Files=getin<bool>("print_file",false);
[512]77
[1646]78    xiosStack=getin<bool>("xios_stack",true) ;
79    systemStack=getin<bool>("system_stack",false) ;
80    if (xiosStack && systemStack)
81    {
82      xiosStack = false;
83    }
84
[511]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
[718]95    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
[719]96    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
[1227]97    maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
[1376]98    recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
[1158]99    if (recvFieldTimeout < 0.0)
100      ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
[718]101
[1377]102    checkEventSync = getin<bool>("check_event_sync", checkEventSync);
103
[1665]104   
105    #ifdef _usingEP
[1601]106    int num_ep;
[1646]107
108    if(isClient)  num_ep = omp_get_num_threads();
109    if(isServer) num_ep = 1;
[1601]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()];
[1665]121
122    #else
123    globalComm=MPI_COMM_WORLD ;
124
[1646]125    #endif
[300]126  }
127
[512]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  */
[300]134  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
[1646]135  TRY
[300]136  {
[1601]137    isClient = true;
138    isServer = false;
139
[300]140    initialize() ;
[490]141
[491]142    CClient::initialize(codeId,localComm,returnComm) ;
[697]143    if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
[491]144
[956]145    // If there are no server processes then we are in attached mode
146    // and the clients are also servers
147    isServer = !usingServer;
[490]148
[523]149    if (printLogs2Files)
150    {
[1601]151      #pragma omp critical
[499]152      CClient::openInfoStream(clientFile);
[523]153      CClient::openErrorStream(clientFile);
154    }
[490]155    else
[523]156    {
[490]157      CClient::openInfoStream();
[523]158      CClient::openErrorStream();
159    }
[490]160  }
[1646]161  CATCH
[300]162
163  void CXios::clientFinalize(void)
164  {
[490]165     CClient::finalize() ;
[697]166     if (CClient::getRank()==0)
167     {
[1601]168       #pragma omp critical (_output)
[1646]169       {
170        info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
171       }
[697]172       globalRegistry->toFile("xios_registry.bin") ;
173       delete globalRegistry ;
[1668]174       
[1679]175       CGraphviz::buildWorkflowGraphVisjs_with_info();
[1677]176
177
178 
[697]179     }
[490]180
[1669]181
[401]182#ifdef XIOS_MEMTRACK
[1158]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
[401]190     MemTrack::TrackListMemoryUsage() ;
[490]191     MemTrack::TrackDumpBlocks();
[401]192#endif
[1158]193
194     CClient::closeInfoStream();
195
196#endif
[490]197  }
198
[512]199  //! Init server by parsing only xios part of config file
[509]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);
[511]206    parseXiosConfig();
[509]207  }
208
[512]209  //! Initialize server then put it into listening state
[1054]210  void CXios::initServerSide(void)
[300]211  {
[1601]212
[1158]213    isClient = false;
214    isServer = true;
[1021]215
[1601]216    initServer();
217
[1021]218    // Initialize all aspects MPI
219    CServer::initialize();
[1330]220    if (CServer::getRank()==0 && CServer::serverLevel != 1) globalRegistry = new CRegistry(CServer::intraComm) ;
[697]221   
[523]222    if (printLogs2Files)
223    {
[1021]224      if (CServer::serverLevel == 0)
[983]225      {
226        CServer::openInfoStream(serverFile);
227        CServer::openErrorStream(serverFile);
228      }
[1021]229      else if (CServer::serverLevel == 1)
[983]230      {
[1021]231        CServer::openInfoStream(serverPrmFile);
232        CServer::openErrorStream(serverPrmFile);
[983]233      }
234      else
235      {
[1021]236        CServer::openInfoStream(serverSndFile);
237        CServer::openErrorStream(serverSndFile);
[983]238      }
[523]239    }
[490]240    else
[523]241    {
[490]242      CServer::openInfoStream();
[523]243      CServer::openErrorStream();
244    }
[490]245
[491]246    // Enter the loop to listen message from Client
[490]247    CServer::eventLoop();
248
[491]249    // Finalize
[1234]250    if (CServer::serverLevel == 0)
[1168]251    {
252      if (CServer::getRank()==0)
253      {
[1646]254        #pragma omp critical (_output)
255        {
256          info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
257        }
[1168]258        globalRegistry->toFile("xios_registry.bin") ;
259        delete globalRegistry ;
260      }
261    }
262    else
263    {
[1243]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
[1168]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
[1243]275        // Merge registries defined on each pools
276        CRegistry globalRegistrySndServers (CServer::intraComm);
277
[1168]278        // All pools (except the first): send globalRegistry to the first pool
[1243]279        for (int i=1; i<secondaryServerGlobalRanks.size(); i++)
[1168]280        {
[1243]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          }
[1168]290        }
291
292        // First pool: receive globalRegistry of all secondary server pools, merge and write the resultant registry
[1243]293        if (rankGlobal == firstPoolGlobalRank)
[1168]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
[1646]314          #pragma omp critical (_output)
315          {
316            info(80)<<"Write data base Registry"<<endl<<globalRegistrySndServers.toString()<<endl ;
317          }
[1168]318          globalRegistrySndServers.toFile("xios_registry.bin") ;
319
320        }
321      }
322      delete globalRegistry;
323    }
[1646]324    CTimer::get("XIOS").suspend() ;
[490]325    CServer::finalize();
[1158]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
[490]339    CServer::closeInfoStream();
340  }
341
[512]342  //! Parse configuration file
[346]343  void CXios::parseFile(const string& filename)
344  {
[490]345    xml::CXMLParser::ParseFile(filename);
[346]346  }
[491]347
[512]348  //! Set using server
[491]349  void CXios::setUsingServer()
350  {
351    usingServer = true;
352  }
353
[512]354  //! Unset using server
[491]355  void CXios::setNotUsingServer()
356  {
357    usingServer = false;
358  }
[300]359}
Note: See TracBrowser for help on using the repository browser.