source: XIOS/dev/branch_yushan/src/cxios.cpp @ 1074

Last change on this file since 1074 was 1074, checked in by yushan, 7 years ago

save modif

  • 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: 6.0 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"
[300]13
[335]14namespace xios
[300]15{
16  string CXios::rootFile="./iodef.xml" ;
17  string CXios::xiosCodeId="xios.x" ;
[499]18  string CXios::clientFile="./xios_client";
19  string CXios::serverFile="./xios_server";
[490]20
[300]21  bool CXios::isClient ;
22  bool CXios::isServer ;
23  MPI_Comm CXios::globalComm ;
24  bool CXios::usingOasis ;
[491]25  bool CXios::usingServer = false;
[718]26  double CXios::bufferSizeFactor = 1.0;
27  const double CXios::defaultBufferSizeFactor = 1.0;
[719]28  StdSize CXios::minBufferSize = 1024 * sizeof(double);
[523]29  bool CXios::printLogs2Files;
[511]30  bool CXios::isOptPerformance = true;
[697]31  CRegistry* CXios::globalRegistry = 0;
[490]32
[512]33  //! Parse configuration file and create some objects from it
[300]34  void CXios::initialize()
[1060]35  {   
[400]36    set_new_handler(noMemory);
[1074]37    #pragma omp critical
[1072]38    {
39      parseFile(rootFile); 
40    }
[511]41    parseXiosConfig();
42  }
43
[512]44  /*!
45  \brief Parse xios part of configuration file (.iodef.xml)
46   Both client and server need information returned from this function
47  */
[511]48  void CXios::parseXiosConfig()
49  {
[1072]50
[311]51    usingOasis=getin<bool>("using_oasis",false) ;
[506]52    usingServer=getin<bool>("using_server",false) ;
[311]53    info.setLevel(getin<int>("info_level",0)) ;
[512]54    report.setLevel(getin<int>("info_level",50));
[523]55    printLogs2Files=getin<bool>("print_file",false);
[512]56
[511]57    StdString bufMemory("memory");
58    StdString bufPerformance("performance");
59    StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance);
60    std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower);
61    if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false;
62    else if (0 != bufOpt.compare(bufPerformance))
63    {
64      ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl );
65    }
66
[718]67    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
[719]68    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
[1072]69 
[1037]70    int num_ep;
[1072]71    if(isClient) 
[1037]72    { 
[1072]73      num_ep = omp_get_num_threads();
74      printf("Client %d: num_ep = %d\n", omp_get_thread_num(), num_ep);
[1037]75    }
76   
77    if(isServer) 
78    { 
[1072]79      num_ep = omp_get_num_threads();
80      printf("Server %d: num_ep = %d\n", omp_get_thread_num(), num_ep); 
[1037]81    }
82   
83    MPI_Info info;
[1072]84    #pragma omp master
[1068]85    {
[1072]86      MPI_Comm *ep_comm;
[1068]87      MPI_Comm_create_endpoints(MPI_COMM_WORLD, num_ep, info, ep_comm);  // servers should reach here too.
88      passage = ep_comm; 
89    }
90   
91    #pragma omp barrier
[1037]92     
[1068]93    globalComm = passage[omp_get_thread_num()];
[1072]94
95    int tmp_rank;
[1073]96    MPI_Comm_rank(globalComm, &tmp_rank);
97    if(isClient) printf("client thread %d/%d\n", omp_get_thread_num(), tmp_rank);
98    if(isServer) printf("server thread %d/%d\n", omp_get_thread_num(), tmp_rank);
[1037]99   
[300]100  }
101
[512]102  /*!
103  Initialize client
104  \param [in] codeId identity of context
105  \param [in] localComm local communicator
106  \param [in/out] returnComm communicator corresponding to group of client with same codeId
107  */
[300]108  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
109  {
[1037]110    isClient = true;
111   
[300]112    initialize() ;
[490]113
[1073]114
115
[491]116    CClient::initialize(codeId,localComm,returnComm) ;
[1073]117
118
[697]119    if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
[491]120
[956]121    // If there are no server processes then we are in attached mode
122    // and the clients are also servers
123    isServer = !usingServer;
[1037]124   
[1073]125    printf("CXios::initClientSide OK, printLogs2Files = %d\n", printLogs2Files);
[1037]126   
[523]127    if (printLogs2Files)
128    {
[499]129      CClient::openInfoStream(clientFile);
[523]130      CClient::openErrorStream(clientFile);
131    }
[490]132    else
[523]133    {
[490]134      CClient::openInfoStream();
[523]135      CClient::openErrorStream();
136    }
[1037]137   
138   
[490]139  }
[300]140
141  void CXios::clientFinalize(void)
142  {
[490]143     CClient::finalize() ;
[697]144     if (CClient::getRank()==0)
145     {
146       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
147       globalRegistry->toFile("xios_registry.bin") ;
148       delete globalRegistry ;
149     }
[698]150     CClient::closeInfoStream();
151 
[490]152
[401]153#ifdef XIOS_MEMTRACK
154     MemTrack::TrackListMemoryUsage() ;
[490]155     MemTrack::TrackDumpBlocks();
[401]156#endif
[490]157  }
158
[512]159  //! Init server by parsing only xios part of config file
[509]160  void CXios::initServer()
161  {
[1037]162    int initialized;
163    MPI_Initialized(&initialized);
164    if (initialized) CServer::is_MPI_Initialized=true ;
165    else CServer::is_MPI_Initialized=false ;
166     
167 
168    if(!CServer::is_MPI_Initialized)
169    {
170      MPI_Init(NULL, NULL);
171    }
172     
[509]173    set_new_handler(noMemory);
174    std::set<StdString> parseList;
175    parseList.insert("xios");
176    xml::CXMLParser::ParseFile(rootFile, parseList);
[511]177    parseXiosConfig();
[509]178  }
179
[512]180  //! Initialize server then put it into listening state
[300]181  void CXios::initServerSide(void)
182  {
[1037]183   
[956]184    isClient = false;
185    isServer = true;
[1037]186   
187    initServer();
188   
189   
[491]190    // Initialize all aspects MPI
[490]191    CServer::initialize();
[1060]192   
[697]193    if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ;
194   
[1037]195     
[523]196    if (printLogs2Files)
197    {
[499]198      CServer::openInfoStream(serverFile);
[523]199      CServer::openErrorStream(serverFile);
200    }
[490]201    else
[523]202    {
[490]203      CServer::openInfoStream();
[523]204      CServer::openErrorStream();
205    }
[1037]206   
[491]207    // Enter the loop to listen message from Client
[490]208    CServer::eventLoop();
[1037]209   
[491]210    // Finalize
[697]211     if (CServer::getRank()==0)
212     {
213       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
214       globalRegistry->toFile("xios_registry.bin") ;
215       delete globalRegistry ;
216     }
[1037]217     
218     
[490]219    CServer::finalize();
[1069]220       
[490]221    CServer::closeInfoStream();
222  }
223
[512]224  //! Parse configuration file
[346]225  void CXios::parseFile(const string& filename)
226  {
[490]227    xml::CXMLParser::ParseFile(filename);
[346]228  }
[491]229
[512]230  //! Set using server
[491]231  void CXios::setUsingServer()
232  {
233    usingServer = true;
234  }
235
[512]236  //! Unset using server
[491]237  void CXios::setNotUsingServer()
238  {
239    usingServer = false;
240  }
[300]241}
Note: See TracBrowser for help on using the repository browser.