1 | #include "globalScopeData.hpp" |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "cxios.hpp" |
---|
4 | #include "client.hpp" |
---|
5 | #include <boost/functional/hash.hpp> |
---|
6 | #include "type.hpp" |
---|
7 | #include "context.hpp" |
---|
8 | #include "context_client.hpp" |
---|
9 | #include "oasis_cinterface.hpp" |
---|
10 | #include "mpi.hpp" |
---|
11 | #include "timer.hpp" |
---|
12 | #include "buffer_client.hpp" |
---|
13 | #include "string_tools.hpp" |
---|
14 | #include "ressources_manager.hpp" |
---|
15 | #include "services_manager.hpp" |
---|
16 | #include <functional> |
---|
17 | #include <cstdio> |
---|
18 | #include "workflow_graph.hpp" |
---|
19 | #include "release_static_allocation.hpp" |
---|
20 | #include "mem_checker.hpp" |
---|
21 | |
---|
22 | namespace xios |
---|
23 | { |
---|
24 | |
---|
25 | const double serverPublishDefaultTimeout=10; |
---|
26 | |
---|
27 | MPI_Comm CClient::intraComm_ ; |
---|
28 | MPI_Comm CClient::interComm_ ; |
---|
29 | MPI_Comm CClient::clientsComm_ ; |
---|
30 | |
---|
31 | std::list<MPI_Comm> CClient::contextInterComms; |
---|
32 | int CClient::serverLeader ; |
---|
33 | bool CClient::is_MPI_Initialized ; |
---|
34 | int CClient::rank_ = INVALID_RANK; |
---|
35 | StdOFStream CClient::m_infoStream; |
---|
36 | StdOFStream CClient::m_errorStream; |
---|
37 | CPoolRessource* CClient::poolRessource_=nullptr ; |
---|
38 | |
---|
39 | MPI_Comm& CClient::getInterComm(void) { return (interComm_); } |
---|
40 | |
---|
41 | ///--------------------------------------------------------------- |
---|
42 | /*! |
---|
43 | * \fn void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
44 | * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups. |
---|
45 | * \param [in] codeId identity of context. |
---|
46 | * \param [in/out] localComm local communicator. |
---|
47 | * \param [in/out] returnComm (intra)communicator of client group. |
---|
48 | */ |
---|
49 | |
---|
50 | void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
51 | { |
---|
52 | |
---|
53 | MPI_Comm clientComm ; |
---|
54 | // initialize MPI if not initialized |
---|
55 | int initialized ; |
---|
56 | MPI_Initialized(&initialized) ; |
---|
57 | if (initialized) is_MPI_Initialized=true ; |
---|
58 | else is_MPI_Initialized=false ; |
---|
59 | |
---|
60 | MPI_Comm globalComm=CXios::getGlobalComm() ; |
---|
61 | |
---|
62 | ///////////////////////////////////////// |
---|
63 | ///////////// PART 1 //////////////////// |
---|
64 | ///////////////////////////////////////// |
---|
65 | |
---|
66 | |
---|
67 | // localComm isn't given |
---|
68 | if (localComm == MPI_COMM_NULL) |
---|
69 | { |
---|
70 | |
---|
71 | // don't use OASIS |
---|
72 | if (!CXios::usingOasis) |
---|
73 | { |
---|
74 | |
---|
75 | if (!is_MPI_Initialized) |
---|
76 | { |
---|
77 | MPI_Init(NULL, NULL); |
---|
78 | } |
---|
79 | CTimer::get("XIOS").resume() ; |
---|
80 | CTimer::get("XIOS init/finalize",false).resume() ; |
---|
81 | |
---|
82 | // split the global communicator |
---|
83 | // get hash from all model to attribute a unique color (int) and then split to get client communicator |
---|
84 | // every mpi process of globalComm (MPI_COMM_WORLD) must participate |
---|
85 | |
---|
86 | int commRank, commSize ; |
---|
87 | MPI_Comm_rank(globalComm,&commRank) ; |
---|
88 | MPI_Comm_size(globalComm,&commSize) ; |
---|
89 | |
---|
90 | std::hash<string> hashString ; |
---|
91 | size_t hashClient=hashString(codeId) ; |
---|
92 | |
---|
93 | size_t* hashAll = new size_t[commSize] ; |
---|
94 | MPI_Allgather(&hashClient,1,MPI_SIZE_T,hashAll,1,MPI_SIZE_T,globalComm) ; |
---|
95 | |
---|
96 | int color=0 ; |
---|
97 | map<size_t,int> listHash ; |
---|
98 | for(int i=0 ; i<=commSize ; i++) |
---|
99 | if (listHash.count(hashAll[i])==0) |
---|
100 | { |
---|
101 | listHash[hashAll[i]]=color ; |
---|
102 | color=color+1 ; |
---|
103 | } |
---|
104 | color=listHash[hashClient] ; |
---|
105 | delete[] hashAll ; |
---|
106 | |
---|
107 | MPI_Comm_split(globalComm, color, commRank, &clientComm) ; |
---|
108 | } |
---|
109 | else |
---|
110 | { |
---|
111 | ERROR("void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)", <<"OASIS usage is set. In these conditions, XIOS initialization needs the local_comm created by OASIS."<<endl) ; |
---|
112 | } |
---|
113 | } |
---|
114 | else // localComm is given |
---|
115 | { |
---|
116 | MPI_Comm_dup(localComm,&clientComm) ; |
---|
117 | MPI_Comm_dup(localComm,&intraComm_) ; |
---|
118 | |
---|
119 | if (CXios::usingServer) |
---|
120 | { |
---|
121 | MPI_Comm_rank(intraComm_,&rank_) ; |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | ///////////////////////////////////////// |
---|
128 | ///////////// PART 2 //////////////////// |
---|
129 | ///////////////////////////////////////// |
---|
130 | |
---|
131 | |
---|
132 | // Create the XIOS communicator for every process which is related |
---|
133 | // to XIOS, as well on client side as on server side |
---|
134 | |
---|
135 | MPI_Comm xiosGlobalComm ; |
---|
136 | string strIds=CXios::getin<string>("clients_code_id","") ; |
---|
137 | vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ; |
---|
138 | if (strIds.empty()) |
---|
139 | { |
---|
140 | // no code Ids given, suppose XIOS initialisation is global |
---|
141 | int commRank, commGlobalRank, serverLeader, clientLeader,serverRemoteLeader,clientRemoteLeader ; |
---|
142 | MPI_Comm splitComm,interComm ; |
---|
143 | MPI_Comm_rank(globalComm,&commGlobalRank) ; |
---|
144 | MPI_Comm_split(globalComm, 0, commGlobalRank, &splitComm) ; |
---|
145 | int splitCommSize, globalCommSize ; |
---|
146 | |
---|
147 | MPI_Comm_size(splitComm,&splitCommSize) ; |
---|
148 | MPI_Comm_size(globalComm,&globalCommSize) ; |
---|
149 | if (splitCommSize==globalCommSize) // no server |
---|
150 | { |
---|
151 | MPI_Comm_dup(globalComm,&xiosGlobalComm) ; |
---|
152 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | MPI_Comm_rank(splitComm,&commRank) ; |
---|
157 | if (commRank==0) clientLeader=commGlobalRank ; |
---|
158 | else clientLeader=0 ; |
---|
159 | serverLeader=0 ; |
---|
160 | MPI_Allreduce(&clientLeader,&clientRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ; |
---|
161 | MPI_Allreduce(&serverLeader,&serverRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ; |
---|
162 | MPI_Intercomm_create(splitComm, 0, globalComm, serverRemoteLeader,1341,&interComm) ; |
---|
163 | MPI_Intercomm_merge(interComm,true,&xiosGlobalComm) ; |
---|
164 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
165 | } |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | |
---|
170 | xiosGlobalCommByFileExchange(clientComm, codeId) ; |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | int commRank ; |
---|
175 | MPI_Comm_rank(CXios::getXiosComm(), &commRank) ; |
---|
176 | MPI_Comm_split(CXios::getXiosComm(),false,commRank, &clientsComm_) ; |
---|
177 | |
---|
178 | // is using server or not ? |
---|
179 | int xiosCommSize, clientsCommSize ; |
---|
180 | MPI_Comm_size(CXios::getXiosComm(), &xiosCommSize) ; |
---|
181 | MPI_Comm_size(clientsComm_, &clientsCommSize) ; |
---|
182 | if (xiosCommSize==clientsCommSize) CXios::setUsingServer() ; |
---|
183 | else CXios::setNotUsingServer() ; |
---|
184 | |
---|
185 | ///////////////////////////////////////// |
---|
186 | ///////////// PART 3 //////////////////// |
---|
187 | ///////////////////////////////////////// |
---|
188 | |
---|
189 | CXios::launchDaemonsManager(false) ; |
---|
190 | poolRessource_ = new CPoolRessource(clientComm, codeId, false) ; |
---|
191 | |
---|
192 | ///////////////////////////////////////// |
---|
193 | ///////////// PART 4 //////////////////// |
---|
194 | ///////////////////////////////////////// |
---|
195 | /* |
---|
196 | MPI_Request req ; |
---|
197 | MPI_Status status ; |
---|
198 | MPI_Ibarrier(xiosGlobalComm,&req) ; // be sure that all services are created now, could be remove later if more asynchronisity |
---|
199 | int ok=false ; |
---|
200 | while (!ok) |
---|
201 | { |
---|
202 | CXios::getDaemonsManager()->eventLoop() ; |
---|
203 | MPI_Test(&req,&ok,&status) ; |
---|
204 | } |
---|
205 | */ |
---|
206 | returnComm = clientComm ; |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | void CClient::xiosGlobalCommByFileExchange(MPI_Comm clientComm, const string& codeId) |
---|
211 | { |
---|
212 | |
---|
213 | MPI_Comm globalComm=CXios::getGlobalComm() ; |
---|
214 | MPI_Comm xiosGlobalComm ; |
---|
215 | |
---|
216 | string strIds=CXios::getin<string>("clients_code_id","") ; |
---|
217 | vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ; |
---|
218 | |
---|
219 | int commRank, globalRank, clientRank, serverRank ; |
---|
220 | MPI_Comm_rank(clientComm, &commRank) ; |
---|
221 | MPI_Comm_rank(globalComm, &globalRank) ; |
---|
222 | string clientFileName("__xios_publisher::"+codeId+"__to_remove__") ; |
---|
223 | |
---|
224 | int error ; |
---|
225 | |
---|
226 | if (commRank==0) // if root process publish name |
---|
227 | { |
---|
228 | std::ofstream ofs (clientFileName, std::ofstream::out); |
---|
229 | ofs<<globalRank ; |
---|
230 | ofs.close(); |
---|
231 | |
---|
232 | // get server root rank |
---|
233 | |
---|
234 | std::ifstream ifs ; |
---|
235 | string fileName=("__xios_publisher::"+CXios::xiosCodeId+"__to_remove__") ; |
---|
236 | |
---|
237 | double timeout = CXios::getin<double>("server_puplish_timeout",serverPublishDefaultTimeout) ; |
---|
238 | double time ; |
---|
239 | |
---|
240 | do |
---|
241 | { |
---|
242 | CTimer::get("server_publish_timeout").resume() ; |
---|
243 | ifs.clear() ; |
---|
244 | ifs.open(fileName, std::ifstream::in) ; |
---|
245 | CTimer::get("server_publish_timeout").suspend() ; |
---|
246 | } while (ifs.fail() && CTimer::get("server_publish_timeout").getCumulatedTime()<timeout) ; |
---|
247 | |
---|
248 | if (CTimer::get("server_publish_timeout").getCumulatedTime()>=timeout || ifs.fail()) |
---|
249 | { |
---|
250 | ifs.clear() ; |
---|
251 | ifs.close() ; |
---|
252 | ifs.clear() ; |
---|
253 | error=true ; |
---|
254 | } |
---|
255 | else |
---|
256 | { |
---|
257 | ifs>>serverRank ; |
---|
258 | ifs.close() ; |
---|
259 | error=false ; |
---|
260 | } |
---|
261 | |
---|
262 | } |
---|
263 | MPI_Bcast(&error,1,MPI_INT,0,clientComm) ; |
---|
264 | |
---|
265 | if (error==false) // you have a server |
---|
266 | { |
---|
267 | MPI_Comm intraComm ; |
---|
268 | MPI_Comm_dup(clientComm,&intraComm) ; |
---|
269 | MPI_Comm interComm ; |
---|
270 | |
---|
271 | int pos=0 ; |
---|
272 | for(int i=0 ; codeId!=clientsCodeId[i]; i++) pos=pos+1 ; |
---|
273 | |
---|
274 | bool high=true ; |
---|
275 | for(int i=pos ; i<clientsCodeId.size(); i++) |
---|
276 | { |
---|
277 | MPI_Intercomm_create(intraComm, 0, globalComm, serverRank, 3141, &interComm); |
---|
278 | MPI_Comm_free(&intraComm) ; |
---|
279 | MPI_Intercomm_merge(interComm,high, &intraComm ) ; |
---|
280 | high=false ; |
---|
281 | if (i==pos) { |
---|
282 | interComm_=interComm ; |
---|
283 | } |
---|
284 | } |
---|
285 | xiosGlobalComm=intraComm ; |
---|
286 | } |
---|
287 | else // no server detected |
---|
288 | { |
---|
289 | vector<int> clientsRank(clientsCodeId.size()) ; |
---|
290 | |
---|
291 | if (commRank==0) |
---|
292 | { |
---|
293 | for(int i=0;i<clientsRank.size();i++) |
---|
294 | { |
---|
295 | std::ifstream ifs ; |
---|
296 | string fileName=("__xios_publisher::"+clientsCodeId[i]+"__to_remove__") ; |
---|
297 | do |
---|
298 | { |
---|
299 | ifs.clear() ; |
---|
300 | ifs.open(fileName, std::ifstream::in) ; |
---|
301 | } while (ifs.fail()) ; |
---|
302 | ifs>>clientsRank[i] ; |
---|
303 | ifs.close() ; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | int client ; |
---|
308 | MPI_Comm intraComm ; |
---|
309 | MPI_Comm_dup(clientComm,&intraComm) ; |
---|
310 | MPI_Comm interComm ; |
---|
311 | |
---|
312 | int pos=0 ; |
---|
313 | for(int i=0 ; codeId!=clientsCodeId[i]; i++) pos=pos+1 ; |
---|
314 | |
---|
315 | bool high=true ; |
---|
316 | for(int i=pos+1 ; i<clientsCodeId.size(); i++) |
---|
317 | { |
---|
318 | if (codeId==clientsCodeId[0]) // first model play the server rule |
---|
319 | { |
---|
320 | MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[i], 3141, &interComm); |
---|
321 | MPI_Intercomm_merge(interComm,false, &intraComm ) ; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[0], 3141, &interComm); |
---|
326 | MPI_Intercomm_merge(interComm,high, &intraComm ) ; |
---|
327 | high=false ; |
---|
328 | } |
---|
329 | if (i==pos) { |
---|
330 | interComm_=interComm ; // NOT TESTED ! |
---|
331 | } |
---|
332 | } |
---|
333 | xiosGlobalComm=intraComm ; |
---|
334 | } |
---|
335 | |
---|
336 | MPI_Barrier(xiosGlobalComm); |
---|
337 | if (commRank==0) std::remove(clientFileName.c_str()) ; |
---|
338 | MPI_Barrier(xiosGlobalComm); |
---|
339 | |
---|
340 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
341 | |
---|
342 | MPI_Comm commUnfree ; |
---|
343 | MPI_Comm_dup(clientComm, &commUnfree ) ; |
---|
344 | |
---|
345 | } |
---|
346 | |
---|
347 | // to check on other architecture |
---|
348 | void CClient::xiosGlobalCommByPublishing(MPI_Comm clientComm, const string& codeId) |
---|
349 | { |
---|
350 | |
---|
351 | // untested. need to be developped an a true MPI compliant library |
---|
352 | |
---|
353 | /* |
---|
354 | // try to discover other client/server |
---|
355 | // do you have a xios server ? |
---|
356 | char portName[MPI_MAX_PORT_NAME]; |
---|
357 | int ierr ; |
---|
358 | int commRank ; |
---|
359 | MPI_Comm_rank(clientComm,&commRank) ; |
---|
360 | |
---|
361 | MPI_Barrier(globalComm) ; |
---|
362 | if (commRank==0) |
---|
363 | { |
---|
364 | |
---|
365 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN ); |
---|
366 | const char* serviceName=CXios::xiosCodeId.c_str() ; |
---|
367 | ierr=MPI_Lookup_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
368 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL ); |
---|
369 | } |
---|
370 | ierr=MPI_SUCCESS ; |
---|
371 | MPI_Bcast(&ierr,1,MPI_INT,0,clientComm) ; |
---|
372 | |
---|
373 | if (ierr==MPI_SUCCESS) // you have a server |
---|
374 | { |
---|
375 | MPI_Comm intraComm=clientComm ; |
---|
376 | MPI_Comm interComm ; |
---|
377 | for(int i=0 ; i<clientsCodeId.size(); i++) |
---|
378 | { |
---|
379 | MPI_Comm_connect(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
380 | MPI_Intercomm_merge(interComm, true, &intraComm ) ; |
---|
381 | } |
---|
382 | xiosGlobalComm=intraComm ; |
---|
383 | } |
---|
384 | else // you don't have any server |
---|
385 | { |
---|
386 | if (codeId==clientsCodeId[0]) // first code will publish his name |
---|
387 | { |
---|
388 | |
---|
389 | if (commRank==0) // if root process publish name |
---|
390 | { |
---|
391 | MPI_Open_port(MPI_INFO_NULL, portName); |
---|
392 | MPI_Publish_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
393 | } |
---|
394 | |
---|
395 | MPI_Comm intraComm=clientComm ; |
---|
396 | MPI_Comm interComm ; |
---|
397 | for(int i=0 ; i<clientsCodeId.size()-1; i++) |
---|
398 | { |
---|
399 | MPI_Comm_accept(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
400 | MPI_Intercomm_merge(interComm,false, &intraComm ) ; |
---|
401 | } |
---|
402 | } |
---|
403 | else // other clients are connecting to the first one |
---|
404 | { |
---|
405 | if (commRank==0) |
---|
406 | { |
---|
407 | |
---|
408 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN ); |
---|
409 | ierr=MPI_Lookup_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
410 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL ); |
---|
411 | } |
---|
412 | |
---|
413 | MPI_Bcast(&ierr,1,MPI_INT,0,clientComm) ; |
---|
414 | |
---|
415 | if (ierr==MPI_SUCCESS) // you can connect |
---|
416 | { |
---|
417 | MPI_Comm intraComm=clientComm ; |
---|
418 | MPI_Comm interComm ; |
---|
419 | for(int i=0 ; i<clientsCodeId.size()-1; i++) |
---|
420 | { |
---|
421 | MPI_Comm_connect(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
422 | MPI_Intercomm_merge(interComm, true, &intraComm ) ; |
---|
423 | } |
---|
424 | xiosGlobalComm=intraComm ; |
---|
425 | } |
---|
426 | } |
---|
427 | } |
---|
428 | */ |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | ///--------------------------------------------------------------- |
---|
433 | /*! |
---|
434 | * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
435 | * \brief Sends a request to create a context to server. Creates client/server contexts. |
---|
436 | * \param [in] id id of context. |
---|
437 | * \param [in] contextComm. |
---|
438 | * Function is only called by client. |
---|
439 | */ |
---|
440 | void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
441 | { |
---|
442 | int commRank, commSize ; |
---|
443 | MPI_Comm_rank(contextComm,&commRank) ; |
---|
444 | MPI_Comm_size(contextComm,&commSize) ; |
---|
445 | |
---|
446 | getPoolRessource()->createService(contextComm, id, 0, CServicesManager::CLIENT, 1) ; |
---|
447 | getPoolRessource()->createService(contextComm, id+"_"+CXios::defaultWriterId, 0, CServicesManager::WRITER, 1) ; |
---|
448 | getPoolRessource()->createService(contextComm, id+"_"+CXios::defaultReaderId, 0, CServicesManager::READER, 1) ; |
---|
449 | |
---|
450 | if (commRank==0) while (!CXios::getServicesManager()->hasService(getPoolRessource()->getId(), id, 0)) { CXios::getDaemonsManager()->eventLoop();} |
---|
451 | |
---|
452 | if (commRank==0) CXios::getContextsManager()->createServerContext(getPoolRessource()->getId(), id, 0, id) ; |
---|
453 | int type=CServicesManager::CLIENT ; |
---|
454 | string name = CXios::getContextsManager()->getServerContextName(getPoolRessource()->getId(), id, 0, type, id) ; |
---|
455 | double time ; |
---|
456 | double lastTime=0 ; |
---|
457 | double latency=0 ; |
---|
458 | bool out=false ; |
---|
459 | while (!out) |
---|
460 | { |
---|
461 | time=MPI_Wtime() ; |
---|
462 | if (time-lastTime > latency) |
---|
463 | { |
---|
464 | out=CXios::getContextsManager()->hasContext(name, contextComm); |
---|
465 | lastTime=time ; |
---|
466 | } |
---|
467 | if (!out) CXios::getDaemonsManager()->eventLoop() ; |
---|
468 | } |
---|
469 | |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | |
---|
474 | /*! |
---|
475 | * \fn void CClient::callOasisEnddef(void) |
---|
476 | * \brief Send the order to the servers to call "oasis_enddef". It must be done by each compound of models before calling oasis_enddef on client side |
---|
477 | * Function is only called by client. |
---|
478 | */ |
---|
479 | void CClient::callOasisEnddef(void) |
---|
480 | { |
---|
481 | bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ; |
---|
482 | if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl |
---|
483 | <<"Variable <call_oasis_enddef> must be set to true"<<endl) ; |
---|
484 | if (!CXios::isClient) // != isServer (change recently ) |
---|
485 | // Attached mode |
---|
486 | { |
---|
487 | // nothing to do |
---|
488 | } |
---|
489 | else |
---|
490 | { |
---|
491 | int rank ; |
---|
492 | int msg=0 ; |
---|
493 | |
---|
494 | MPI_Comm_rank(intraComm_,&rank) ; |
---|
495 | if (rank==0) |
---|
496 | { |
---|
497 | MPI_Send(&msg,1,MPI_INT,0,5,interComm_) ; // tags oasis_endded = 5 |
---|
498 | } |
---|
499 | |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | void CClient::finalize(void) |
---|
504 | { |
---|
505 | |
---|
506 | MPI_Barrier(clientsComm_) ; |
---|
507 | int commRank ; |
---|
508 | MPI_Comm_rank(clientsComm_, &commRank) ; |
---|
509 | if (commRank==0) CXios::getRessourcesManager()->finalize() ; |
---|
510 | |
---|
511 | CTimer::get("XIOS init/finalize",false).suspend() ; |
---|
512 | CTimer::get("XIOS").suspend() ; |
---|
513 | CXios::finalizeDaemonsManager() ; |
---|
514 | finalizePoolRessource() ; |
---|
515 | CContext::removeAllContexts() ; // free memory for related context |
---|
516 | |
---|
517 | CXios::getMpiGarbageCollector().release() ; // release unfree MPI ressources |
---|
518 | if (!is_MPI_Initialized) |
---|
519 | { |
---|
520 | if (!CXios::usingOasis) MPI_Finalize() ; |
---|
521 | } |
---|
522 | |
---|
523 | info(20) << "Client side context is finalized"<<endl ; |
---|
524 | report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ; |
---|
525 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
526 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
527 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ; |
---|
528 | report(0)<< " Performance report : This ratio must be close to zero. Otherwise it may be usefull to increase buffer size or numbers of server"<<endl ; |
---|
529 | // report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
530 | report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ; |
---|
531 | report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ; |
---|
532 | report(100)<<CTimer::getAllCumulatedTime()<<endl ; |
---|
533 | report(100)<<CMemChecker::getAllCumulatedMem()<<endl ; |
---|
534 | CWorkflowGraph::drawWorkFlowGraph_client(); |
---|
535 | |
---|
536 | xios::releaseStaticAllocation() ; |
---|
537 | |
---|
538 | } |
---|
539 | |
---|
540 | void CClient::finalizePoolRessource() |
---|
541 | { |
---|
542 | delete poolRessource_ ; poolRessource_=nullptr ; |
---|
543 | } |
---|
544 | |
---|
545 | /*! |
---|
546 | * Return global rank without oasis and current rank in model intraComm in case of oasis |
---|
547 | */ |
---|
548 | int CClient::getRank() |
---|
549 | { |
---|
550 | return rank_; |
---|
551 | } |
---|
552 | |
---|
553 | /*! |
---|
554 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
555 | * The file name will be suffix+rank+extension. |
---|
556 | * |
---|
557 | * \param fileName[in] protype file name |
---|
558 | * \param ext [in] extension of the file |
---|
559 | * \param fb [in/out] the file buffer |
---|
560 | */ |
---|
561 | void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
562 | { |
---|
563 | StdStringStream fileNameClient; |
---|
564 | int numDigit = 0; |
---|
565 | int size = 0; |
---|
566 | int rank; |
---|
567 | MPI_Comm_size(CXios::getGlobalComm(), &size); |
---|
568 | MPI_Comm_rank(CXios::getGlobalComm(),&rank); |
---|
569 | while (size) |
---|
570 | { |
---|
571 | size /= 10; |
---|
572 | ++numDigit; |
---|
573 | } |
---|
574 | |
---|
575 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext; |
---|
576 | |
---|
577 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
578 | if (!fb->is_open()) |
---|
579 | ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
580 | << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s)."); |
---|
581 | } |
---|
582 | |
---|
583 | /*! |
---|
584 | * \brief Open a file stream to write the info logs |
---|
585 | * Open a file stream with a specific file name suffix+rank |
---|
586 | * to write the info logs. |
---|
587 | * \param fileName [in] protype file name |
---|
588 | */ |
---|
589 | void CClient::openInfoStream(const StdString& fileName) |
---|
590 | { |
---|
591 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
592 | openStream(fileName, ".out", fb); |
---|
593 | |
---|
594 | info.write2File(fb); |
---|
595 | report.write2File(fb); |
---|
596 | } |
---|
597 | |
---|
598 | //! Write the info logs to standard output |
---|
599 | void CClient::openInfoStream() |
---|
600 | { |
---|
601 | info.write2StdOut(); |
---|
602 | report.write2StdOut(); |
---|
603 | } |
---|
604 | |
---|
605 | //! Close the info logs file if it opens |
---|
606 | void CClient::closeInfoStream() |
---|
607 | { |
---|
608 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
609 | } |
---|
610 | |
---|
611 | /*! |
---|
612 | * \brief Open a file stream to write the error log |
---|
613 | * Open a file stream with a specific file name suffix+rank |
---|
614 | * to write the error log. |
---|
615 | * \param fileName [in] protype file name |
---|
616 | */ |
---|
617 | void CClient::openErrorStream(const StdString& fileName) |
---|
618 | { |
---|
619 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
620 | openStream(fileName, ".err", fb); |
---|
621 | |
---|
622 | error.write2File(fb); |
---|
623 | } |
---|
624 | |
---|
625 | //! Write the error log to standard error output |
---|
626 | void CClient::openErrorStream() |
---|
627 | { |
---|
628 | error.write2StdErr(); |
---|
629 | } |
---|
630 | |
---|
631 | //! Close the error log file if it opens |
---|
632 | void CClient::closeErrorStream() |
---|
633 | { |
---|
634 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
635 | } |
---|
636 | } |
---|