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
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    #pragma omp critical
38    {
39      parseFile(rootFile); 
40    }
41    parseXiosConfig();
42  }
43
44  /*!
45  \brief Parse xios part of configuration file (.iodef.xml)
46   Both client and server need information returned from this function
47  */
48  void CXios::parseXiosConfig()
49  {
50
51    usingOasis=getin<bool>("using_oasis",false) ;
52    usingServer=getin<bool>("using_server",false) ;
53    info.setLevel(getin<int>("info_level",0)) ;
54    report.setLevel(getin<int>("info_level",50));
55    printLogs2Files=getin<bool>("print_file",false);
56
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
67    bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
68    minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
69 
70    int num_ep;
71    if(isClient) 
72    { 
73      num_ep = omp_get_num_threads();
74      printf("Client %d: num_ep = %d\n", omp_get_thread_num(), num_ep);
75    }
76   
77    if(isServer) 
78    { 
79      num_ep = omp_get_num_threads();
80      printf("Server %d: num_ep = %d\n", omp_get_thread_num(), num_ep); 
81    }
82   
83    MPI_Info info;
84    #pragma omp master
85    {
86      MPI_Comm *ep_comm;
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
92     
93    globalComm = passage[omp_get_thread_num()];
94
95    int tmp_rank;
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);
99   
100  }
101
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  */
108  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
109  {
110    isClient = true;
111   
112    initialize() ;
113
114
115
116    CClient::initialize(codeId,localComm,returnComm) ;
117
118
119    if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
120
121    // If there are no server processes then we are in attached mode
122    // and the clients are also servers
123    isServer = !usingServer;
124   
125    printf("CXios::initClientSide OK, printLogs2Files = %d\n", printLogs2Files);
126   
127    if (printLogs2Files)
128    {
129      CClient::openInfoStream(clientFile);
130      CClient::openErrorStream(clientFile);
131    }
132    else
133    {
134      CClient::openInfoStream();
135      CClient::openErrorStream();
136    }
137   
138   
139  }
140
141  void CXios::clientFinalize(void)
142  {
143     CClient::finalize() ;
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     }
150     CClient::closeInfoStream();
151 
152
153#ifdef XIOS_MEMTRACK
154     MemTrack::TrackListMemoryUsage() ;
155     MemTrack::TrackDumpBlocks();
156#endif
157  }
158
159  //! Init server by parsing only xios part of config file
160  void CXios::initServer()
161  {
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     
173    set_new_handler(noMemory);
174    std::set<StdString> parseList;
175    parseList.insert("xios");
176    xml::CXMLParser::ParseFile(rootFile, parseList);
177    parseXiosConfig();
178  }
179
180  //! Initialize server then put it into listening state
181  void CXios::initServerSide(void)
182  {
183   
184    isClient = false;
185    isServer = true;
186   
187    initServer();
188   
189   
190    // Initialize all aspects MPI
191    CServer::initialize();
192   
193    if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ;
194   
195     
196    if (printLogs2Files)
197    {
198      CServer::openInfoStream(serverFile);
199      CServer::openErrorStream(serverFile);
200    }
201    else
202    {
203      CServer::openInfoStream();
204      CServer::openErrorStream();
205    }
206   
207    // Enter the loop to listen message from Client
208    CServer::eventLoop();
209   
210    // Finalize
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     }
217     
218     
219    CServer::finalize();
220       
221    CServer::closeInfoStream();
222  }
223
224  //! Parse configuration file
225  void CXios::parseFile(const string& filename)
226  {
227    xml::CXMLParser::ParseFile(filename);
228  }
229
230  //! Set using server
231  void CXios::setUsingServer()
232  {
233    usingServer = true;
234  }
235
236  //! Unset using server
237  void CXios::setNotUsingServer()
238  {
239    usingServer = false;
240  }
241}
Note: See TracBrowser for help on using the repository browser.