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

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

Preperation for merge from trunk

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