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

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

test_complete OK with openmp. Missing : arithmetic filter

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