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

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

dev_omp

  • 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.6 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, 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      CClient::openInfoStream(clientFile);
127      CClient::openErrorStream(clientFile);
128    }
129    else
130    {
131      CClient::openInfoStream();
132      CClient::openErrorStream();
133    }
134  }
135
136  void CXios::clientFinalize(void)
137  {
138     CClient::finalize() ;
139     if (CClient::getRank()==0)
140     {
141       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
142       globalRegistry->toFile("xios_registry.bin") ;
143       delete globalRegistry ;
144     }
145
146#ifdef XIOS_MEMTRACK
147
148#ifdef XIOS_MEMTRACK_LIGHT
149       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
150       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
151#endif
152
153#ifdef XIOS_MEMTRACK_FULL
154     MemTrack::TrackListMemoryUsage() ;
155     MemTrack::TrackDumpBlocks();
156#endif
157
158     CClient::closeInfoStream();
159
160#endif
161  }
162
163  //! Init server by parsing only xios part of config file
164  void CXios::initServer()
165  {
166    set_new_handler(noMemory);
167    std::set<StdString> parseList;
168    parseList.insert("xios");
169    xml::CXMLParser::ParseFile(rootFile, parseList);
170    parseXiosConfig();
171  }
172
173  //! Initialize server then put it into listening state
174  void CXios::initServerSide(void)
175  {
176   
177    isClient = false;
178    isServer = true;
179   
180    initServer();
181
182    // Initialize all aspects MPI
183    CServer::initialize();
184    if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ;
185   
186    if (printLogs2Files)
187    {
188      CServer::openInfoStream(serverFile);
189      CServer::openErrorStream(serverFile);
190    }
191    else
192    {
193      CServer::openInfoStream();
194      CServer::openErrorStream();
195    }
196
197    // Enter the loop to listen message from Client
198    CServer::eventLoop();
199
200    // Finalize
201     if (CServer::getRank()==0)
202     {
203       info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
204       globalRegistry->toFile("xios_registry.bin") ;
205       delete globalRegistry ;
206     }
207    CServer::finalize();
208
209#ifdef XIOS_MEMTRACK
210
211#ifdef XIOS_MEMTRACK_LIGHT
212       report(10) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
213       report(10) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
214#endif
215
216#ifdef XIOS_MEMTRACK_FULL
217     MemTrack::TrackListMemoryUsage() ;
218     MemTrack::TrackDumpBlocks();
219#endif
220#endif
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.