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

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

log OK with threads

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