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

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

backup modification

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