source: XIOS/dev/branch_openmp/src/cxios.cpp @ 1520

Last change on this file since 1520 was 1520, checked in by yushan, 6 years ago

save dev. TO DO : test with xios

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