XIOS  1.0
Xml I/O Server
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Définitions de type Énumérations Valeurs énumérées Amis Macros
cxios.cpp
Aller à la documentation de ce fichier.
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 
14 namespace 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  string CXios::serverPrmFile="./xios_server1";
21  string CXios::serverSndFile="./xios_server2";
22 
23  bool CXios::xiosStack = true;
24  bool CXios::systemStack = false;
25 
26  bool CXios::isClient ;
27  bool CXios::isServer ;
28  MPI_Comm CXios::globalComm ;
29  bool CXios::usingOasis ;
30  bool CXios::usingServer = false;
31  bool CXios::usingServer2 = false;
32  int CXios::ratioServer2 = 50;
33  int CXios::nbPoolsServer2 = 1;
34  double CXios::bufferSizeFactor = 1.0;
35  const double CXios::defaultBufferSizeFactor = 1.0;
36  StdSize CXios::minBufferSize = 1024 * sizeof(double);
37  StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ;
39  bool CXios::isOptPerformance = true;
40  CRegistry* CXios::globalRegistry = 0;
41  double CXios::recvFieldTimeout = 300.0;
42  bool CXios::checkEventSync=false ;
43 
46  {
47  set_new_handler(noMemory);
50  }
51 
57  {
58  usingOasis=getin<bool>("using_oasis",false) ;
59  usingServer=getin<bool>("using_server",false) ;
60  usingServer2=getin<bool>("using_server2",false) ;
61  ratioServer2=getin<int>("ratio_server2",50);
62  nbPoolsServer2=getin<int>("number_pools_server2",0);
63  info.setLevel(getin<int>("info_level",0)) ;
64  report.setLevel(getin<int>("info_level",50));
65  printLogs2Files=getin<bool>("print_file",false);
66 
67  xiosStack=getin<bool>("xios_stack",true) ;
68  systemStack=getin<bool>("system_stack",false) ;
69  if (xiosStack && systemStack)
70  {
71  xiosStack = false;
72  }
73 
74  StdString bufMemory("memory");
75  StdString bufPerformance("performance");
76  StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance);
77  std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower);
78  if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false;
79  else if (0 != bufOpt.compare(bufPerformance))
80  {
81  ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl );
82  }
83 
84  bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor);
85  minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double));
86  maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max());
87  recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout);
88  if (recvFieldTimeout < 0.0)
89  ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative.");
90 
91  checkEventSync = getin<bool>("check_event_sync", checkEventSync);
92 
93  globalComm=MPI_COMM_WORLD ;
94  }
95 
102  void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
103  TRY
104  {
105  initialize() ;
106 
107  isClient = true;
108 
109  CClient::initialize(codeId,localComm,returnComm) ;
110  if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ;
111 
112  // If there are no server processes then we are in attached mode
113  // and the clients are also servers
114  isServer = !usingServer;
115 
116  if (printLogs2Files)
117  {
118  CClient::openInfoStream(clientFile);
119  CClient::openErrorStream(clientFile);
120  }
121  else
122  {
125  }
126  }
127  CATCH
128 
130  {
132  if (CClient::getRank()==0)
133  {
134  info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
135  globalRegistry->toFile("xios_registry.bin") ;
136  delete globalRegistry ;
137  }
138 
139 #ifdef XIOS_MEMTRACK
140 
141 #ifdef XIOS_MEMTRACK_LIGHT
142  report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
143  report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
144 #endif
145 
146 #ifdef XIOS_MEMTRACK_FULL
147  MemTrack::TrackListMemoryUsage() ;
148  MemTrack::TrackDumpBlocks();
149 #endif
150 
152 
153 #endif
154  }
155 
158  {
159  set_new_handler(noMemory);
160  std::set<StdString> parseList;
161  parseList.insert("xios");
163  parseXiosConfig();
164  }
165 
168  {
169  initServer();
170  isClient = false;
171  isServer = true;
172 
173  // Initialize all aspects MPI
176 
177  if (printLogs2Files)
178  {
179  if (CServer::serverLevel == 0)
180  {
183  }
184  else if (CServer::serverLevel == 1)
185  {
188  }
189  else
190  {
193  }
194  }
195  else
196  {
199  }
200 
201  // Enter the loop to listen message from Client
203 
204  // Finalize
205  if (CServer::serverLevel == 0)
206  {
207  if (CServer::getRank()==0)
208  {
209  info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ;
210  globalRegistry->toFile("xios_registry.bin") ;
211  delete globalRegistry ;
212  }
213  }
214  else
215  {
216  // If using two server levels:
217  // (1) merge registries on each pool
218  // (2) send merged registries to the first pool
219  // (3) merge received registries on the first pool
220  if (CServer::serverLevel == 2)
221  {
222  vector<int>& secondaryServerGlobalRanks = CServer::getSecondaryServerGlobalRanks();
223  int firstPoolGlobalRank = secondaryServerGlobalRanks[0];
224  int rankGlobal;
225  MPI_Comm_rank(globalComm, &rankGlobal);
226 
227  // Merge registries defined on each pools
228  CRegistry globalRegistrySndServers (CServer::intraComm);
229 
230  // All pools (except the first): send globalRegistry to the first pool
231  for (int i=1; i<secondaryServerGlobalRanks.size(); i++)
232  {
233  if (rankGlobal == secondaryServerGlobalRanks[i])
234  {
235  globalRegistrySndServers.mergeRegistry(*globalRegistry) ;
236  int registrySize = globalRegistrySndServers.size();
237  MPI_Send(&registrySize,1,MPI_LONG,firstPoolGlobalRank,15,CXios::globalComm) ;
238  CBufferOut buffer(registrySize) ;
239  globalRegistrySndServers.toBuffer(buffer) ;
240  MPI_Send(buffer.start(),registrySize,MPI_CHAR,firstPoolGlobalRank,15,CXios::globalComm) ;
241  }
242  }
243 
244  // First pool: receive globalRegistry of all secondary server pools, merge and write the resultant registry
245  if (rankGlobal == firstPoolGlobalRank)
246  {
247  MPI_Status status;
248  char* recvBuff;
249 
250  globalRegistrySndServers.mergeRegistry(*globalRegistry) ;
251 
252  for (int i=1; i< secondaryServerGlobalRanks.size(); i++)
253  {
254  int rank = secondaryServerGlobalRanks[i];
255  int registrySize = 0;
256  MPI_Recv(&registrySize, 1, MPI_LONG, rank, 15, CXios::globalComm, &status);
257  recvBuff = new char[registrySize];
258  MPI_Recv(recvBuff, registrySize, MPI_CHAR, rank, 15, CXios::globalComm, &status);
259  CBufferIn buffer(recvBuff, registrySize) ;
260  CRegistry recvRegistry;
261  recvRegistry.fromBuffer(buffer) ;
262  globalRegistrySndServers.mergeRegistry(recvRegistry) ;
263  delete[] recvBuff;
264  }
265 
266  info(80)<<"Write data base Registry"<<endl<<globalRegistrySndServers.toString()<<endl ;
267  globalRegistrySndServers.toFile("xios_registry.bin") ;
268 
269  }
270  }
271  delete globalRegistry;
272  }
274 
275 #ifdef XIOS_MEMTRACK
276 
277 #ifdef XIOS_MEMTRACK_LIGHT
278  report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
279  report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
280 #endif
281 
282 #ifdef XIOS_MEMTRACK_FULL
283  MemTrack::TrackListMemoryUsage() ;
284  MemTrack::TrackDumpBlocks();
285 #endif
286 #endif
288  }
289 
291  void CXios::parseFile(const string& filename)
292  {
293  xml::CXMLParser::ParseFile(filename);
294  }
295 
298  {
299  usingServer = true;
300  }
301 
304  {
305  usingServer = false;
306  }
307 }
CLog report("report")
Definition: log.hpp:56
static bool isClient
Check if xios is client.
Definition: cxios.hpp:39
bool toBuffer(CBufferOut &buffer) const
Definition: registry.cpp:76
static void initServer()
Initialize server (if any)
Definition: cxios.cpp:157
void noMemory(void)
Definition: memory.cpp:7
static void initialize(const string &codeId, MPI_Comm &localComm, MPI_Comm &returnComm)
Definition: client.cpp:37
static bool checkEventSync
For debuuging, check if event are coherent and synchrone on client side.
Definition: cxios.hpp:57
static const double defaultBufferSizeFactor
Default factor value.
Definition: cxios.hpp:51
CLog info("info")
Definition: log.hpp:55
static void openErrorStream()
Write the error log to standard error output.
Definition: client.cpp:392
#define TRY
Definition: exception.hpp:154
size_t size(void) const
Definition: registry.cpp:88
static double bufferSizeFactor
Factor used to tune the buffer size.
Definition: cxios.hpp:50
static int nbPoolsServer2
Number of pools created on the secondary server.
Definition: cxios.hpp:49
static bool isOptPerformance
Check if buffer size is for performance (as large as possible)
Definition: cxios.hpp:54
static string rootFile
Configuration filename.
Definition: cxios.hpp:29
string toString(void) const
Definition: registry.cpp:103
static bool systemStack
Exception handling.
Definition: cxios.hpp:37
static bool usingServer2
Using secondary server (server mode).
Definition: cxios.hpp:47
static void parseFile(const string &filename)
Parse configuration file.
Definition: cxios.cpp:291
static void ParseFile(const StdString &filename, const std::set< StdString > &parseList=std::set< StdString >())
////////////////////// Définitions ////////////////////// ///
Definition: xml_parser.cpp:15
static double recvFieldTimeout
Time to wait for data before issuing an error when receiving a field.
Definition: cxios.hpp:56
static int serverLevel
Definition: server.hpp:35
void setLevel(int l)
Definition: log.hpp:27
std::string StdString
Definition: xios_spl.hpp:48
#define xios(arg)
static CRegistry * globalRegistry
global registry which is wrote by the root process of the servers
Definition: cxios.hpp:55
void * start(void)
Definition: buffer_out.hpp:46
static bool usingServer
Using server (server mode)
Definition: cxios.hpp:46
static MPI_Comm globalComm
Global communicator.
Definition: cxios.hpp:42
void mergeRegistry(const CRegistry &inRegistry)
Definition: registry.cpp:170
static bool xiosStack
Exception handling.
Definition: cxios.hpp:36
static void initialize(void)
Creates intraComm for each possible type of servers (classical, primary or secondary).
Definition: server.cpp:47
static int getRank()
Get rank of the current process in the intraComm.
Definition: server.cpp:861
static string clientFile
Filename template for client.
Definition: cxios.hpp:31
static void setNotUsingServer()
Setting xios NOT to use server mode.
Definition: cxios.cpp:303
void toFile(const string &filename)
Definition: registry.cpp:143
This class is a registry database which store key with an associated value.
Definition: registry.hpp:20
CATCH CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar *scalarDestination, CScalar *scalarSource, CReduceScalarToScalar *algo ERROR)("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",<< "Operation must be defined."<< "Scalar source "<< scalarSource->getId()<< std::endl<< "Scalar destination "<< scalarDestination->getId())
static void closeInfoStream()
Close the info logs file if it opens.
Definition: server.cpp:925
static void initServerSide(void)
Initialize server then put it into listening state.
Definition: cxios.cpp:167
static void openErrorStream()
Write the error log to standard error output.
Definition: server.cpp:945
static void initialize(void)
Parse configuration file and create some objects from it.
Definition: cxios.cpp:45
static StdSize minBufferSize
Minimum buffer size.
Definition: cxios.hpp:52
static string xiosCodeId
Identity for XIOS.
Definition: cxios.hpp:30
std::size_t StdSize
Definition: xios_spl.hpp:49
static void openInfoStream()
Write the info logs to standard output.
Definition: client.cpp:365
static void eventLoop(void)
Definition: server.cpp:436
static bool printLogs2Files
Printing out logs into files.
Definition: cxios.hpp:44
static int getRank()
Get global rank without oasis and current rank in model intraComm in case of oasis.
Definition: client.cpp:308
static void initClientSide(const string &codeId, MPI_Comm &localComm, MPI_Comm &returnComm)
Initialize client.
Definition: cxios.cpp:102
static bool isServer
Check if xios is server.
Definition: cxios.hpp:40
static bool usingOasis
Using Oasis.
Definition: cxios.hpp:45
static MPI_Comm intraComm
Definition: server.hpp:28
static void finalize(void)
Definition: client.cpp:263
static void parseXiosConfig()
Parse only Xios part of configuration file.
Definition: cxios.cpp:56
static StdSize maxBufferSize
Maximum buffer size.
Definition: cxios.hpp:53
static string serverFile
Filename template for server.
Definition: cxios.hpp:32
static void setUsingServer()
Setting xios to use server mode.
Definition: cxios.cpp:297
static void clientFinalize(void)
Definition: cxios.cpp:129
static void openInfoStream()
Write the info logs to standard output.
Definition: server.cpp:918
static int ratioServer2
Percentage of server processors dedicated to secondary server.
Definition: cxios.hpp:48
bool fromBuffer(CBufferIn &buffer)
Definition: registry.cpp:116
static void closeInfoStream()
Close the info logs file if it opens.
Definition: client.cpp:372
static string serverPrmFile
Filename template for primary server in case of two server levels.
Definition: cxios.hpp:33
#define CATCH
Definition: exception.hpp:155
static string serverSndFile
Filename template for secondary server in case of two server levels.
Definition: cxios.hpp:34
static void finalize(void)
Definition: server.cpp:402
static vector< int > & getSecondaryServerGlobalRanks()
Definition: server.cpp:868