1 | #include "globalScopeData.hpp" |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "cxios.hpp" |
---|
4 | #include "client.hpp" |
---|
5 | #include "type.hpp" |
---|
6 | #include "context.hpp" |
---|
7 | #include "context_client.hpp" |
---|
8 | #include "oasis_cinterface.hpp" |
---|
9 | #include "mpi.hpp" |
---|
10 | #include "timer.hpp" |
---|
11 | #include "buffer_client.hpp" |
---|
12 | #include "string_tools.hpp" |
---|
13 | #include "ressources_manager.hpp" |
---|
14 | #include "services_manager.hpp" |
---|
15 | #include <functional> |
---|
16 | #include <cstdio> |
---|
17 | #include "workflow_graph.hpp" |
---|
18 | #include "release_static_allocation.hpp" |
---|
19 | #include "mem_checker.hpp" |
---|
20 | |
---|
21 | namespace xios |
---|
22 | { |
---|
23 | |
---|
24 | const double serverPublishDefaultTimeout=10; |
---|
25 | |
---|
26 | MPI_Comm CClient::intraComm_ ; |
---|
27 | MPI_Comm CClient::interComm_ ; |
---|
28 | MPI_Comm CClient::clientsComm_ ; |
---|
29 | |
---|
30 | std::list<MPI_Comm> CClient::contextInterComms; |
---|
31 | int CClient::serverLeader ; |
---|
32 | bool CClient::is_MPI_Initialized ; |
---|
33 | int CClient::rank_ = INVALID_RANK; |
---|
34 | StdOFStream CClient::m_infoStream; |
---|
35 | StdOFStream CClient::m_errorStream; |
---|
36 | CPoolRessource* CClient::poolRessource_=nullptr ; |
---|
37 | extern CLogType logTimers ; |
---|
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 | xios::MPI_Comm_split(globalComm, color, commRank, &clientComm) ; |
---|
108 | CXios::getMpiGarbageCollector().registerCommunicator(clientComm) ; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | 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) ; |
---|
113 | } |
---|
114 | } |
---|
115 | else // localComm is given |
---|
116 | { |
---|
117 | CTimer::get("XIOS").resume() ; |
---|
118 | CTimer::get("XIOS init/finalize",false).resume() ; |
---|
119 | xios::MPI_Comm_dup(localComm,&clientComm) ; |
---|
120 | CXios::getMpiGarbageCollector().registerCommunicator(clientComm) ; |
---|
121 | xios::MPI_Comm_dup(localComm,&intraComm_) ; |
---|
122 | CXios::getMpiGarbageCollector().registerCommunicator(intraComm_) ; |
---|
123 | |
---|
124 | if (CXios::usingServer) |
---|
125 | { |
---|
126 | MPI_Comm_rank(intraComm_,&rank_) ; |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | ///////////////////////////////////////// |
---|
133 | ///////////// PART 2 //////////////////// |
---|
134 | ///////////////////////////////////////// |
---|
135 | |
---|
136 | |
---|
137 | // Create the XIOS communicator for every process which is related |
---|
138 | // to XIOS, as well on client side as on server side |
---|
139 | |
---|
140 | MPI_Comm xiosGlobalComm ; |
---|
141 | string strIds=CXios::getin<string>("clients_code_id","") ; |
---|
142 | vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ; |
---|
143 | if (strIds.empty()) |
---|
144 | { |
---|
145 | // no code Ids given, suppose XIOS initialisation is global |
---|
146 | int commRank, commGlobalRank, serverLeader, clientLeader,serverRemoteLeader,clientRemoteLeader ; |
---|
147 | MPI_Comm splitComm,interComm ; |
---|
148 | MPI_Comm_rank(globalComm,&commGlobalRank) ; |
---|
149 | xios::MPI_Comm_split(globalComm, 0, commGlobalRank, &splitComm) ; |
---|
150 | int splitCommSize, globalCommSize ; |
---|
151 | |
---|
152 | MPI_Comm_size(splitComm,&splitCommSize) ; |
---|
153 | MPI_Comm_size(globalComm,&globalCommSize) ; |
---|
154 | if (splitCommSize==globalCommSize) // no server |
---|
155 | { |
---|
156 | xios::MPI_Comm_dup(globalComm,&xiosGlobalComm) ; |
---|
157 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | MPI_Comm_rank(splitComm,&commRank) ; |
---|
162 | if (commRank==0) clientLeader=commGlobalRank ; |
---|
163 | else clientLeader=0 ; |
---|
164 | serverLeader=0 ; |
---|
165 | MPI_Allreduce(&clientLeader,&clientRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ; |
---|
166 | MPI_Allreduce(&serverLeader,&serverRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ; |
---|
167 | xios::MPI_Intercomm_create(splitComm, 0, globalComm, serverRemoteLeader,1341,&interComm) ; |
---|
168 | xios::MPI_Intercomm_merge(interComm,true,&xiosGlobalComm) ; |
---|
169 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
170 | xios::MPI_Comm_free( &interComm ); |
---|
171 | } |
---|
172 | xios::MPI_Comm_free( &splitComm ); |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | |
---|
177 | xiosGlobalCommByFileExchange(clientComm, codeId) ; |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | int commRank ; |
---|
182 | MPI_Comm_rank(CXios::getXiosComm(), &commRank) ; |
---|
183 | xios::MPI_Comm_split(CXios::getXiosComm(),false,commRank, &clientsComm_) ; |
---|
184 | CXios::getMpiGarbageCollector().registerCommunicator(clientsComm_) ; |
---|
185 | |
---|
186 | // is using server or not ? |
---|
187 | int xiosCommSize, clientsCommSize ; |
---|
188 | MPI_Comm_size(CXios::getXiosComm(), &xiosCommSize) ; |
---|
189 | MPI_Comm_size(clientsComm_, &clientsCommSize) ; |
---|
190 | if (xiosCommSize==clientsCommSize) CXios::setNotUsingServer() ; |
---|
191 | else CXios::setUsingServer() ; |
---|
192 | |
---|
193 | ///////////////////////////////////////// |
---|
194 | ///////////// PART 3 //////////////////// |
---|
195 | ///////////////////////////////////////// |
---|
196 | |
---|
197 | CXios::launchDaemonsManager(false) ; |
---|
198 | shared_ptr<CEventScheduler> eventScheduler ; |
---|
199 | poolRessource_ = new CPoolRessource(clientComm, eventScheduler, codeId, false) ; |
---|
200 | |
---|
201 | ///////////////////////////////////////// |
---|
202 | ///////////// PART 4 //////////////////// |
---|
203 | ///////////////////////////////////////// |
---|
204 | /* |
---|
205 | MPI_Request req ; |
---|
206 | MPI_Status status ; |
---|
207 | MPI_Ibarrier(CXios::getXiosComm(),&req) ; // be sure that all services are created now, could be remove later if more asynchronisity |
---|
208 | int ok=false ; |
---|
209 | while (!ok) |
---|
210 | { |
---|
211 | CXios::getDaemonsManager()->eventLoop() ; |
---|
212 | MPI_Test(&req,&ok,&status) ; |
---|
213 | } |
---|
214 | */ |
---|
215 | returnComm = clientComm ; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | void CClient::xiosGlobalCommByFileExchange(MPI_Comm clientComm, const string& codeId) |
---|
220 | { |
---|
221 | |
---|
222 | MPI_Comm globalComm=CXios::getGlobalComm() ; |
---|
223 | MPI_Comm xiosGlobalComm ; |
---|
224 | |
---|
225 | string strIds=CXios::getin<string>("clients_code_id","") ; |
---|
226 | vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ; |
---|
227 | |
---|
228 | int commRank, globalRank, clientRank, serverRank ; |
---|
229 | MPI_Comm_rank(clientComm, &commRank) ; |
---|
230 | MPI_Comm_rank(globalComm, &globalRank) ; |
---|
231 | string clientFileName("__xios_publisher::"+codeId+"__to_remove__") ; |
---|
232 | |
---|
233 | int error ; |
---|
234 | |
---|
235 | if (commRank==0) // if root process publish name |
---|
236 | { |
---|
237 | std::ofstream ofs (clientFileName, std::ofstream::out); |
---|
238 | ofs<<globalRank ; |
---|
239 | ofs.close(); |
---|
240 | |
---|
241 | // get server root rank |
---|
242 | |
---|
243 | std::ifstream ifs ; |
---|
244 | string fileName=("__xios_publisher::"+CXios::xiosCodeId+"__to_remove__") ; |
---|
245 | |
---|
246 | double timeout = CXios::getin<double>("server_puplish_timeout",serverPublishDefaultTimeout) ; |
---|
247 | double time ; |
---|
248 | |
---|
249 | do |
---|
250 | { |
---|
251 | CTimer::get("server_publish_timeout").resume() ; |
---|
252 | ifs.clear() ; |
---|
253 | ifs.open(fileName, std::ifstream::in) ; |
---|
254 | CTimer::get("server_publish_timeout").suspend() ; |
---|
255 | } while (ifs.fail() && CTimer::get("server_publish_timeout").getCumulatedTime()<timeout) ; |
---|
256 | |
---|
257 | if (CTimer::get("server_publish_timeout").getCumulatedTime()>=timeout || ifs.fail()) |
---|
258 | { |
---|
259 | ifs.clear() ; |
---|
260 | ifs.close() ; |
---|
261 | ifs.clear() ; |
---|
262 | error=true ; |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | ifs>>serverRank ; |
---|
267 | ifs.close() ; |
---|
268 | error=false ; |
---|
269 | } |
---|
270 | |
---|
271 | } |
---|
272 | MPI_Bcast(&error,1,MPI_INT,0,clientComm) ; |
---|
273 | |
---|
274 | if (error==false) // you have a server |
---|
275 | { |
---|
276 | MPI_Comm intraComm ; |
---|
277 | xios::MPI_Comm_dup(clientComm,&intraComm) ; |
---|
278 | MPI_Comm interComm ; |
---|
279 | |
---|
280 | int pos=0 ; |
---|
281 | for(int i=0 ; codeId!=clientsCodeId[i]; i++) pos=pos+1 ; |
---|
282 | |
---|
283 | bool high=true ; |
---|
284 | for(int i=pos ; i<clientsCodeId.size(); i++) |
---|
285 | { |
---|
286 | xios::MPI_Intercomm_create(intraComm, 0, globalComm, serverRank, 3141, &interComm); |
---|
287 | CXios::getMpiGarbageCollector().registerCommunicator(interComm) ; |
---|
288 | xios::MPI_Comm_free(&intraComm) ; |
---|
289 | xios::MPI_Intercomm_merge(interComm,high, &intraComm ) ; |
---|
290 | high=false ; |
---|
291 | if (i==pos) { |
---|
292 | interComm_=interComm ; |
---|
293 | } |
---|
294 | } |
---|
295 | xiosGlobalComm=intraComm ; |
---|
296 | } |
---|
297 | else // no server detected |
---|
298 | { |
---|
299 | vector<int> clientsRank(clientsCodeId.size()) ; |
---|
300 | |
---|
301 | if (commRank==0) |
---|
302 | { |
---|
303 | for(int i=0;i<clientsRank.size();i++) |
---|
304 | { |
---|
305 | std::ifstream ifs ; |
---|
306 | string fileName=("__xios_publisher::"+clientsCodeId[i]+"__to_remove__") ; |
---|
307 | do |
---|
308 | { |
---|
309 | ifs.clear() ; |
---|
310 | ifs.open(fileName, std::ifstream::in) ; |
---|
311 | } while (ifs.fail()) ; |
---|
312 | ifs>>clientsRank[i] ; |
---|
313 | ifs.close() ; |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | int client ; |
---|
318 | MPI_Comm intraComm ; |
---|
319 | xios::MPI_Comm_dup(clientComm,&intraComm) ; |
---|
320 | MPI_Comm interComm ; |
---|
321 | |
---|
322 | int pos=0 ; |
---|
323 | for(int i=0 ; codeId!=clientsCodeId[i]; i++) pos=pos+1 ; |
---|
324 | |
---|
325 | bool high=true ; |
---|
326 | for(int i=pos+1 ; i<clientsCodeId.size(); i++) |
---|
327 | { |
---|
328 | if (codeId==clientsCodeId[0]) // first model play the server rule |
---|
329 | { |
---|
330 | xios::MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[i], 3141, &interComm); |
---|
331 | xios::MPI_Intercomm_merge(interComm,false, &intraComm ) ; |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | xios::MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[0], 3141, &interComm); |
---|
336 | xios::MPI_Intercomm_merge(interComm,high, &intraComm ) ; |
---|
337 | high=false ; |
---|
338 | } |
---|
339 | if (i==pos) { |
---|
340 | interComm_=interComm ; // NOT TESTED ! |
---|
341 | } |
---|
342 | } |
---|
343 | xiosGlobalComm=intraComm ; |
---|
344 | } |
---|
345 | |
---|
346 | MPI_Barrier(xiosGlobalComm); |
---|
347 | if (commRank==0) std::remove(clientFileName.c_str()) ; |
---|
348 | MPI_Barrier(xiosGlobalComm); |
---|
349 | |
---|
350 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
351 | |
---|
352 | } |
---|
353 | |
---|
354 | // to check on other architecture |
---|
355 | void CClient::xiosGlobalCommByPublishing(MPI_Comm clientComm, const string& codeId) |
---|
356 | { |
---|
357 | |
---|
358 | // untested. need to be developped an a true MPI compliant library |
---|
359 | |
---|
360 | /* |
---|
361 | // try to discover other client/server |
---|
362 | // do you have a xios server ? |
---|
363 | char portName[MPI_MAX_PORT_NAME]; |
---|
364 | int ierr ; |
---|
365 | int commRank ; |
---|
366 | MPI_Comm_rank(clientComm,&commRank) ; |
---|
367 | |
---|
368 | MPI_Barrier(globalComm) ; |
---|
369 | if (commRank==0) |
---|
370 | { |
---|
371 | |
---|
372 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN ); |
---|
373 | const char* serviceName=CXios::xiosCodeId.c_str() ; |
---|
374 | ierr=MPI_Lookup_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
375 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL ); |
---|
376 | } |
---|
377 | ierr=MPI_SUCCESS ; |
---|
378 | MPI_Bcast(&ierr,1,MPI_INT,0,clientComm) ; |
---|
379 | |
---|
380 | if (ierr==MPI_SUCCESS) // you have a server |
---|
381 | { |
---|
382 | MPI_Comm intraComm=clientComm ; |
---|
383 | MPI_Comm interComm ; |
---|
384 | for(int i=0 ; i<clientsCodeId.size(); i++) |
---|
385 | { |
---|
386 | MPI_Comm_connect(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
387 | xios::MPI_Intercomm_merge(interComm, true, &intraComm ) ; |
---|
388 | } |
---|
389 | xiosGlobalComm=intraComm ; |
---|
390 | } |
---|
391 | else // you don't have any server |
---|
392 | { |
---|
393 | if (codeId==clientsCodeId[0]) // first code will publish his name |
---|
394 | { |
---|
395 | |
---|
396 | if (commRank==0) // if root process publish name |
---|
397 | { |
---|
398 | MPI_Open_port(MPI_INFO_NULL, portName); |
---|
399 | MPI_Publish_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
400 | } |
---|
401 | |
---|
402 | MPI_Comm intraComm=clientComm ; |
---|
403 | MPI_Comm interComm ; |
---|
404 | for(int i=0 ; i<clientsCodeId.size()-1; i++) |
---|
405 | { |
---|
406 | MPI_Comm_accept(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
407 | xios::MPI_Intercomm_merge(interComm,false, &intraComm ) ; |
---|
408 | } |
---|
409 | } |
---|
410 | else // other clients are connecting to the first one |
---|
411 | { |
---|
412 | if (commRank==0) |
---|
413 | { |
---|
414 | |
---|
415 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN ); |
---|
416 | ierr=MPI_Lookup_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
417 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL ); |
---|
418 | } |
---|
419 | |
---|
420 | MPI_Bcast(&ierr,1,MPI_INT,0,clientComm) ; |
---|
421 | |
---|
422 | if (ierr==MPI_SUCCESS) // you can connect |
---|
423 | { |
---|
424 | MPI_Comm intraComm=clientComm ; |
---|
425 | MPI_Comm interComm ; |
---|
426 | for(int i=0 ; i<clientsCodeId.size()-1; i++) |
---|
427 | { |
---|
428 | MPI_Comm_connect(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
429 | xios::MPI_Intercomm_merge(interComm, true, &intraComm ) ; |
---|
430 | } |
---|
431 | xiosGlobalComm=intraComm ; |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | */ |
---|
436 | } |
---|
437 | |
---|
438 | |
---|
439 | ///--------------------------------------------------------------- |
---|
440 | /*! |
---|
441 | * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
442 | * \brief Sends a request to create a context to server. Creates client/server contexts. |
---|
443 | * \param [in] id id of context. |
---|
444 | * \param [in] contextComm. |
---|
445 | * Function is only called by client. |
---|
446 | */ |
---|
447 | void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
448 | { |
---|
449 | int commRank, commSize ; |
---|
450 | MPI_Comm_rank(contextComm,&commRank) ; |
---|
451 | MPI_Comm_size(contextComm,&commSize) ; |
---|
452 | |
---|
453 | shared_ptr<CEventScheduler> eventScheduler ; |
---|
454 | |
---|
455 | getPoolRessource()->createService(contextComm, eventScheduler, id, 0, CServicesManager::CLIENT, 1) ; |
---|
456 | // getPoolRessource()->createService(contextComm, eventScheduler, id+"_"+CXios::defaultWriterId, 0, CServicesManager::WRITER, 1) ; |
---|
457 | getPoolRessource()->createNewServiceOnto(id+"_"+CXios::defaultWriterId, CServicesManager::WRITER, id) ; |
---|
458 | // getPoolRessource()->createService(contextComm, eventScheduler, id+"_"+CXios::defaultReaderId, 0, CServicesManager::READER, 1) ; |
---|
459 | getPoolRessource()->createNewServiceOnto(id+"_"+CXios::defaultReaderId, CServicesManager::READER, id) ; |
---|
460 | |
---|
461 | if (commRank==0) while (!CXios::getServicesManager()->hasService(getPoolRessource()->getId(), id, 0)) { CXios::getDaemonsManager()->eventLoop();} |
---|
462 | |
---|
463 | if (commRank==0) CXios::getContextsManager()->createServerContext(getPoolRessource()->getId(), id, 0, id) ; |
---|
464 | int type=CServicesManager::CLIENT ; |
---|
465 | string name = CXios::getContextsManager()->getServerContextName(getPoolRessource()->getId(), id, 0, type, id) ; |
---|
466 | double time ; |
---|
467 | double lastTime=0 ; |
---|
468 | double latency=0 ; |
---|
469 | bool out=false ; |
---|
470 | while (!out) |
---|
471 | { |
---|
472 | time=MPI_Wtime() ; |
---|
473 | if (time-lastTime > latency) |
---|
474 | { |
---|
475 | out=CXios::getContextsManager()->hasContext(name, contextComm); |
---|
476 | lastTime=time ; |
---|
477 | } |
---|
478 | if (!out) CXios::getDaemonsManager()->eventLoop() ; |
---|
479 | } |
---|
480 | |
---|
481 | } |
---|
482 | |
---|
483 | |
---|
484 | |
---|
485 | /*! |
---|
486 | * \fn void CClient::callOasisEnddef(void) |
---|
487 | * \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 |
---|
488 | * Function is only called by client. |
---|
489 | */ |
---|
490 | void CClient::callOasisEnddef(void) |
---|
491 | { |
---|
492 | bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ; |
---|
493 | if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl |
---|
494 | <<"Variable <call_oasis_enddef> must be set to true"<<endl) ; |
---|
495 | if (!CXios::isClient) // != isServer (change recently ) |
---|
496 | // Attached mode |
---|
497 | { |
---|
498 | // nothing to do |
---|
499 | } |
---|
500 | else |
---|
501 | { |
---|
502 | int rank ; |
---|
503 | int msg=0 ; |
---|
504 | |
---|
505 | MPI_Comm_rank(intraComm_,&rank) ; |
---|
506 | if (rank==0) |
---|
507 | { |
---|
508 | MPI_Send(&msg,1,MPI_INT,0,5,interComm_) ; // tags oasis_endded = 5 |
---|
509 | } |
---|
510 | |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | void CClient::finalize(void) |
---|
515 | { |
---|
516 | MPI_Barrier(clientsComm_) ; |
---|
517 | int commRank ; |
---|
518 | MPI_Comm_rank(clientsComm_, &commRank) ; |
---|
519 | if (commRank==0) CXios::getRessourcesManager()->finalize() ; |
---|
520 | |
---|
521 | CTimer::get("XIOS finalize").suspend(); |
---|
522 | CTimer::get("XIOS init/finalize",false).suspend() ; |
---|
523 | CTimer::get("XIOS").suspend() ; |
---|
524 | CXios::finalizeDaemonsManager() ; |
---|
525 | finalizePoolRessource() ; |
---|
526 | CContext::removeAllContexts() ; // free memory for related context |
---|
527 | CXios::getMpiGarbageCollector().release() ; // release unfree MPI ressources |
---|
528 | MPI_Comm xiosComm=CXios::getXiosComm() ; |
---|
529 | xios::MPI_Comm_free(&xiosComm) ; |
---|
530 | CCommTrack::dumpComm() ; |
---|
531 | if (!is_MPI_Initialized) |
---|
532 | { |
---|
533 | if (!CXios::usingOasis) MPI_Finalize() ; |
---|
534 | } |
---|
535 | |
---|
536 | info(20) << "Client side context is finalized"<<endl ; |
---|
537 | report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ; |
---|
538 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
539 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
540 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ; |
---|
541 | 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 ; |
---|
542 | // report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
543 | report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ; |
---|
544 | 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 ; |
---|
545 | |
---|
546 | if (info.isActive(logProfile)) |
---|
547 | { |
---|
548 | printProfile(); |
---|
549 | } |
---|
550 | |
---|
551 | if (info.isActive(logTimers)) report(0)<<"\n"<<CTimer::getAllCumulatedTime()<<endl ; |
---|
552 | if (CXios::reportMemory) |
---|
553 | { |
---|
554 | report(100)<<CMemChecker::getAllCumulatedMem()<<endl ; |
---|
555 | } |
---|
556 | CWorkflowGraph::drawWorkFlowGraph_client(); |
---|
557 | |
---|
558 | xios::releaseStaticAllocation() ; |
---|
559 | |
---|
560 | } |
---|
561 | |
---|
562 | void CClient::printProfile() |
---|
563 | { |
---|
564 | list< pair<string,int> > timer_name; |
---|
565 | timer_name.push_back({"XIOS init",0}); |
---|
566 | timer_name.push_back({"XIOS init context",0}); |
---|
567 | timer_name.push_back({"XIOS close definition",0}); |
---|
568 | timer_name.push_back({"XIOS solve inheritance",0}); |
---|
569 | timer_name.push_back({"XIOS update calendar",0}); |
---|
570 | timer_name.push_back({"Check late data (read)",1}); |
---|
571 | timer_name.push_back({"XIOS recv field",0}); |
---|
572 | timer_name.push_back({"XIOS send field",0}); |
---|
573 | timer_name.push_back({"Context event loop",1}); |
---|
574 | timer_name.push_back({"Model to client",1}); |
---|
575 | timer_name.push_back({"Client workflow",1}); |
---|
576 | timer_name.push_back({"Applying filters",2}); |
---|
577 | timer_name.push_back({"Transformation transfers",3}); |
---|
578 | timer_name.push_back({"Transformation MPI",4}); |
---|
579 | timer_name.push_back({"Temporal filters",3}); |
---|
580 | timer_name.push_back({"Scatter event",3}); |
---|
581 | timer_name.push_back({"Field : send data",4}); |
---|
582 | timer_name.push_back({"Blocking time",0}); |
---|
583 | timer_name.push_back({"XIOS context finalize",0}); |
---|
584 | timer_name.push_back({"XIOS finalize",0}); |
---|
585 | |
---|
586 | report(0)<< endl; |
---|
587 | double total_time = CTimer::get("XIOS").getCumulatedTime(); |
---|
588 | for(auto it_timer_name = timer_name.begin(); it_timer_name != timer_name.end(); it_timer_name++) |
---|
589 | { |
---|
590 | double timer_time = CTimer::get(it_timer_name->first).getCumulatedTime(); |
---|
591 | if ( timer_time / total_time > 0.001 ) |
---|
592 | { |
---|
593 | ostringstream printed_line; |
---|
594 | printed_line << setprecision(3) << std::fixed; |
---|
595 | for(int itab=0;itab<it_timer_name->second;itab++) |
---|
596 | printed_line << " "; |
---|
597 | printed_line << it_timer_name->first << " : " << timer_time <<endl; |
---|
598 | string string_line = printed_line.str(); |
---|
599 | report(0)<< string_line; |
---|
600 | } |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | void CClient::finalizePoolRessource() |
---|
605 | { |
---|
606 | delete poolRessource_ ; poolRessource_=nullptr ; |
---|
607 | } |
---|
608 | |
---|
609 | /*! |
---|
610 | * Return global rank without oasis and current rank in model intraComm in case of oasis |
---|
611 | */ |
---|
612 | int CClient::getRank() |
---|
613 | { |
---|
614 | return rank_; |
---|
615 | } |
---|
616 | |
---|
617 | /*! |
---|
618 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
619 | * The file name will be suffix+rank+extension. |
---|
620 | * |
---|
621 | * \param fileName[in] protype file name |
---|
622 | * \param ext [in] extension of the file |
---|
623 | * \param fb [in/out] the file buffer |
---|
624 | */ |
---|
625 | void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
626 | { |
---|
627 | StdStringStream fileNameClient; |
---|
628 | int numDigit = 0; |
---|
629 | int size = 0; |
---|
630 | int rank; |
---|
631 | MPI_Comm_size(CXios::getGlobalComm(), &size); |
---|
632 | MPI_Comm_rank(CXios::getGlobalComm(),&rank); |
---|
633 | while (size) |
---|
634 | { |
---|
635 | size /= 10; |
---|
636 | ++numDigit; |
---|
637 | } |
---|
638 | |
---|
639 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext; |
---|
640 | |
---|
641 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
642 | if (!fb->is_open()) |
---|
643 | ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
644 | << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s)."); |
---|
645 | } |
---|
646 | |
---|
647 | /*! |
---|
648 | * \brief Open a file stream to write the info logs |
---|
649 | * Open a file stream with a specific file name suffix+rank |
---|
650 | * to write the info logs. |
---|
651 | * \param fileName [in] protype file name |
---|
652 | */ |
---|
653 | void CClient::openInfoStream(const StdString& fileName) |
---|
654 | { |
---|
655 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
656 | openStream(fileName, ".out", fb); |
---|
657 | |
---|
658 | info.write2File(fb); |
---|
659 | report.write2File(fb); |
---|
660 | } |
---|
661 | |
---|
662 | //! Write the info logs to standard output |
---|
663 | void CClient::openInfoStream() |
---|
664 | { |
---|
665 | info.write2StdOut(); |
---|
666 | report.write2StdOut(); |
---|
667 | } |
---|
668 | |
---|
669 | //! Close the info logs file if it opens |
---|
670 | void CClient::closeInfoStream() |
---|
671 | { |
---|
672 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
673 | } |
---|
674 | |
---|
675 | /*! |
---|
676 | * \brief Open a file stream to write the error log |
---|
677 | * Open a file stream with a specific file name suffix+rank |
---|
678 | * to write the error log. |
---|
679 | * \param fileName [in] protype file name |
---|
680 | */ |
---|
681 | void CClient::openErrorStream(const StdString& fileName) |
---|
682 | { |
---|
683 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
684 | openStream(fileName, ".err", fb); |
---|
685 | |
---|
686 | error.write2File(fb); |
---|
687 | } |
---|
688 | |
---|
689 | //! Write the error log to standard error output |
---|
690 | void CClient::openErrorStream() |
---|
691 | { |
---|
692 | error.write2StdErr(); |
---|
693 | } |
---|
694 | |
---|
695 | //! Close the error log file if it opens |
---|
696 | void CClient::closeErrorStream() |
---|
697 | { |
---|
698 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
699 | } |
---|
700 | } |
---|