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

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

Clean Up

  • 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.7 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    // int tmp_size;
90    // MPI_Comm_size(globalComm, &tmp_size);
91    // if(isClient) printf("Client : globalcomm size = %d\n", tmp_size);
92    // if(isServer) printf("Server : globalcomm size = %d\n", tmp_size);
93
94   
95  }
96
97  /*!
98  Initialize client
99  \param [in] codeId identity of context
100  \param [in] localComm local communicator
101  \param [in/out] returnComm communicator corresponding to group of client with same codeId
102  */
103  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
104  {
105    isClient = true;
106   
107    initialize() ;
108
109   
110    CClient::initialize(codeId,localComm,returnComm) ;
111    if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
112
113    // If there are no server processes then we are in attached mode
114    // and the clients are also servers
115    isServer = !usingServer;
116   
117   
118    if (printLogs2Files)
119    {
120      CClient::openInfoStream(clientFile);
121      CClient::openErrorStream(clientFile);
122    }
123    else
124    {
125      CClient::openInfoStream();
126      CClient::openErrorStream();
127    }
128   
129   
130  }
131
132  void CXios::clientFinalize(void)
133  {
134     CClient::finalize() ;
135     if (CClient::getRank()==0)
136     {
137       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
138       globalRegistry->toFile("xios_registry.bin") ;
139       delete globalRegistry ;
140     }
141     CClient::closeInfoStream();
142 
143
144#ifdef XIOS_MEMTRACK
145     MemTrack::TrackListMemoryUsage() ;
146     MemTrack::TrackDumpBlocks();
147#endif
148  }
149
150  //! Init server by parsing only xios part of config file
151  void CXios::initServer()
152  {
153    int initialized;
154    MPI_Initialized(&initialized);
155    if (initialized) CServer::is_MPI_Initialized=true ;
156    else CServer::is_MPI_Initialized=false ;
157     
158 
159    if(!CServer::is_MPI_Initialized)
160    {
161      MPI_Init(NULL, NULL);
162    }
163     
164    set_new_handler(noMemory);
165    std::set<StdString> parseList;
166    parseList.insert("xios");
167    xml::CXMLParser::ParseFile(rootFile, parseList);
168    parseXiosConfig();
169  }
170
171  //! Initialize server then put it into listening state
172  void CXios::initServerSide(void)
173  {
174   
175    isClient = false;
176    isServer = true;
177   
178    initServer();
179   
180   
181    // Initialize all aspects MPI
182    CServer::initialize();
183   
184    if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ;
185   
186     
187    if (printLogs2Files)
188    {
189      CServer::openInfoStream(serverFile);
190      CServer::openErrorStream(serverFile);
191    }
192    else
193    {
194      CServer::openInfoStream();
195      CServer::openErrorStream();
196    }
197   
198    // Enter the loop to listen message from Client
199    CServer::eventLoop();
200   
201    // Finalize
202     if (CServer::getRank()==0)
203     {
204       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
205       globalRegistry->toFile("xios_registry.bin") ;
206       delete globalRegistry ;
207     }
208     
209     
210    CServer::finalize();
211       
212    CServer::closeInfoStream();
213  }
214
215  //! Parse configuration file
216  void CXios::parseFile(const string& filename)
217  {
218    xml::CXMLParser::ParseFile(filename);
219  }
220
221  //! Set using server
222  void CXios::setUsingServer()
223  {
224    usingServer = true;
225  }
226
227  //! Unset using server
228  void CXios::setNotUsingServer()
229  {
230    usingServer = false;
231  }
232}
Note: See TracBrowser for help on using the repository browser.