source: XIOS/dev/branch_openmp/src/cxios.cpp @ 1355

Last change on this file since 1355 was 1355, checked in by yushan, 3 years ago

unify MPI_Comm type

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