source: XIOS/dev/branch_yushan_merged/src/cxios.cpp @ 1161

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

fix bad commit. Back to R1155 branch not merged

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