1 | #include "globalScopeData.hpp" |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "cxios.hpp" |
---|
4 | #include "server.hpp" |
---|
5 | #include "client.hpp" |
---|
6 | #include "type.hpp" |
---|
7 | #include "context.hpp" |
---|
8 | #include "object_template.hpp" |
---|
9 | #include "oasis_cinterface.hpp" |
---|
10 | #include <boost/functional/hash.hpp> |
---|
11 | #include <boost/algorithm/string.hpp> |
---|
12 | #include "mpi.hpp" |
---|
13 | #include "tracer.hpp" |
---|
14 | #include "timer.hpp" |
---|
15 | #include "event_scheduler.hpp" |
---|
16 | |
---|
17 | namespace xios |
---|
18 | { |
---|
19 | MPI_Comm CServer::intraComm ; |
---|
20 | std::list<MPI_Comm> CServer::interCommLeft ; |
---|
21 | std::list<MPI_Comm> CServer::interCommRight ; |
---|
22 | std::list<MPI_Comm> CServer::contextInterComms; |
---|
23 | std::list<MPI_Comm> CServer::contextIntraComms; |
---|
24 | int CServer::serverLevel = 0 ; |
---|
25 | int CServer::nbContexts = 0; |
---|
26 | bool CServer::isRoot = false ; |
---|
27 | int CServer::rank_ = INVALID_RANK; |
---|
28 | StdOFStream CServer::m_infoStream; |
---|
29 | StdOFStream CServer::m_errorStream; |
---|
30 | map<string,CContext*> CServer::contextList ; |
---|
31 | vector<int> CServer::sndServerGlobalRanks; |
---|
32 | bool CServer::finished=false ; |
---|
33 | bool CServer::is_MPI_Initialized ; |
---|
34 | CEventScheduler* CServer::eventScheduler = 0; |
---|
35 | |
---|
36 | //--------------------------------------------------------------- |
---|
37 | /*! |
---|
38 | * \fn void CServer::initialize(void) |
---|
39 | * Creates intraComm for each possible type of servers (classical, primary or secondary). |
---|
40 | * In case of secondary servers intraComm is created for each secondary server pool. |
---|
41 | * (For now the assumption is that there is one proc per pool.) |
---|
42 | * Creates interComm and stores them into the following lists: |
---|
43 | * classical server -- interCommLeft |
---|
44 | * primary server -- interCommLeft and interCommRight |
---|
45 | * secondary server -- interCommLeft for each pool. |
---|
46 | */ |
---|
47 | void CServer::initialize(void) |
---|
48 | { |
---|
49 | int initialized ; |
---|
50 | MPI_Initialized(&initialized) ; |
---|
51 | if (initialized) is_MPI_Initialized=true ; |
---|
52 | else is_MPI_Initialized=false ; |
---|
53 | int rank ; |
---|
54 | |
---|
55 | // Not using OASIS |
---|
56 | if (!CXios::usingOasis) |
---|
57 | { |
---|
58 | |
---|
59 | if (!is_MPI_Initialized) |
---|
60 | { |
---|
61 | MPI_Init(NULL, NULL); |
---|
62 | } |
---|
63 | CTimer::get("XIOS").resume() ; |
---|
64 | |
---|
65 | boost::hash<string> hashString ; |
---|
66 | unsigned long hashServer = hashString(CXios::xiosCodeId); |
---|
67 | |
---|
68 | unsigned long* hashAll ; |
---|
69 | unsigned long* srvLevelAll ; |
---|
70 | |
---|
71 | int size ; |
---|
72 | int myColor ; |
---|
73 | int i,c ; |
---|
74 | MPI_Comm newComm; |
---|
75 | |
---|
76 | MPI_Comm_size(CXios::globalComm, &size) ; |
---|
77 | MPI_Comm_rank(CXios::globalComm, &rank_); |
---|
78 | |
---|
79 | hashAll=new unsigned long[size] ; |
---|
80 | MPI_Allgather(&hashServer, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ; |
---|
81 | |
---|
82 | map<unsigned long, int> colors ; |
---|
83 | map<unsigned long, int> leaders ; |
---|
84 | map<unsigned long, int>::iterator it ; |
---|
85 | |
---|
86 | // (1) Establish client leaders, distribute processes between two server levels |
---|
87 | vector<int> srvRanks; |
---|
88 | for(i=0,c=0;i<size;i++) |
---|
89 | { |
---|
90 | if (colors.find(hashAll[i])==colors.end()) |
---|
91 | { |
---|
92 | colors[hashAll[i]]=c ; |
---|
93 | leaders[hashAll[i]]=i ; |
---|
94 | c++ ; |
---|
95 | } |
---|
96 | if (CXios::usingServer2) |
---|
97 | if (hashAll[i] == hashServer) |
---|
98 | srvRanks.push_back(i); |
---|
99 | } |
---|
100 | for (i=0; i<srvRanks.size(); i++) |
---|
101 | { |
---|
102 | if (i > srvRanks.size()*(100-CXios::ratioServer2)/100) |
---|
103 | { |
---|
104 | sndServerGlobalRanks.push_back(srvRanks[i]); |
---|
105 | if (rank_ == srvRanks[i]) serverLevel=2; |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | if (rank_ == srvRanks[i]) serverLevel=1; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | // (2) Create intraComm |
---|
114 | myColor = (serverLevel == 2) ? rank_ : colors[hashServer]; |
---|
115 | MPI_Comm_split(CXios::globalComm, myColor, rank_, &intraComm) ; |
---|
116 | |
---|
117 | // (3) Create interComm |
---|
118 | if (serverLevel == 0) |
---|
119 | { |
---|
120 | int clientLeader; |
---|
121 | for(it=leaders.begin();it!=leaders.end();it++) |
---|
122 | { |
---|
123 | if (it->first!=hashServer) |
---|
124 | { |
---|
125 | clientLeader=it->second ; |
---|
126 | int intraCommSize, intraCommRank ; |
---|
127 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
128 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
129 | info(50)<<"intercommCreate::server (classical mode) "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
130 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
131 | |
---|
132 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ; |
---|
133 | interCommLeft.push_back(newComm) ; |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | else if (serverLevel == 1) |
---|
138 | { |
---|
139 | int clientLeader, srvSndLeader; |
---|
140 | int srvPrmLeader ; |
---|
141 | |
---|
142 | for (it=leaders.begin();it!=leaders.end();it++) |
---|
143 | { |
---|
144 | if (it->first != hashServer) |
---|
145 | { |
---|
146 | clientLeader=it->second ; |
---|
147 | int intraCommSize, intraCommRank ; |
---|
148 | MPI_Comm_size(intraComm, &intraCommSize) ; |
---|
149 | MPI_Comm_rank(intraComm, &intraCommRank) ; |
---|
150 | info(50)<<"intercommCreate::server (server level 1) "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
151 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
152 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ; |
---|
153 | interCommLeft.push_back(newComm) ; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | for (int i = 0; i < sndServerGlobalRanks.size(); ++i) |
---|
158 | { |
---|
159 | int intraCommSize, intraCommRank ; |
---|
160 | MPI_Comm_size(intraComm, &intraCommSize) ; |
---|
161 | MPI_Comm_rank(intraComm, &intraCommRank) ; |
---|
162 | info(50)<<"intercommCreate::client (server level 1) "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
163 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< sndServerGlobalRanks[i]<<endl ; |
---|
164 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, sndServerGlobalRanks[i], 1, &newComm) ; |
---|
165 | interCommRight.push_back(newComm) ; |
---|
166 | } |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | int clientLeader; |
---|
171 | clientLeader = leaders[hashString(CXios::xiosCodeId)]; |
---|
172 | int intraCommSize, intraCommRank ; |
---|
173 | MPI_Comm_size(intraComm, &intraCommSize) ; |
---|
174 | MPI_Comm_rank(intraComm, &intraCommRank) ; |
---|
175 | info(50)<<"intercommCreate::server (server level 2) "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
176 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
177 | |
---|
178 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 1, &newComm) ; |
---|
179 | interCommLeft.push_back(newComm) ; |
---|
180 | } |
---|
181 | |
---|
182 | delete [] hashAll ; |
---|
183 | |
---|
184 | } |
---|
185 | // using OASIS |
---|
186 | else |
---|
187 | { |
---|
188 | int size; |
---|
189 | int myColor; |
---|
190 | unsigned long* srvGlobalRanks; |
---|
191 | if (!is_MPI_Initialized) oasis_init(CXios::xiosCodeId); |
---|
192 | |
---|
193 | CTimer::get("XIOS").resume() ; |
---|
194 | MPI_Comm localComm; |
---|
195 | oasis_get_localcomm(localComm); |
---|
196 | |
---|
197 | // Create server intraComm |
---|
198 | if (!CXios::usingServer2) |
---|
199 | { |
---|
200 | MPI_Comm_dup(localComm, &intraComm); |
---|
201 | MPI_Comm_rank(localComm,&rank_) ; |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | int globalRank; |
---|
206 | MPI_Comm_rank(localComm,&rank_) ; |
---|
207 | MPI_Comm_size(localComm,&size) ; |
---|
208 | MPI_Comm_rank(CXios::globalComm,&globalRank) ; |
---|
209 | srvGlobalRanks = new unsigned long[size] ; |
---|
210 | MPI_Allgather(&globalRank, 1, MPI_LONG, srvGlobalRanks, 1, MPI_LONG, localComm) ; |
---|
211 | |
---|
212 | for (int i=size*CXios::ratioServer2/100; i<size; i++) |
---|
213 | sndServerGlobalRanks.push_back(srvGlobalRanks[i]); |
---|
214 | |
---|
215 | if ( rank_ < (size - sndServerGlobalRanks.size()) ) |
---|
216 | { |
---|
217 | serverLevel = 1; |
---|
218 | myColor = 0; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | serverLevel = 2; |
---|
223 | myColor = rank_; |
---|
224 | } |
---|
225 | MPI_Comm_split(localComm, myColor, rank_, &intraComm) ; |
---|
226 | |
---|
227 | } |
---|
228 | MPI_Comm_size(intraComm,&size) ; |
---|
229 | string codesId=CXios::getin<string>("oasis_codes_id") ; |
---|
230 | |
---|
231 | vector<string> splitted ; |
---|
232 | boost::split( splitted, codesId, boost::is_any_of(","), boost::token_compress_on ) ; |
---|
233 | vector<string>::iterator it ; |
---|
234 | |
---|
235 | MPI_Comm newComm ; |
---|
236 | int globalRank ; |
---|
237 | MPI_Comm_rank(CXios::globalComm,&globalRank); |
---|
238 | |
---|
239 | // (1) Create interComms with models |
---|
240 | for(it=splitted.begin();it!=splitted.end();it++) |
---|
241 | { |
---|
242 | oasis_get_intercomm(newComm,*it) ; |
---|
243 | if ( !CXios::usingServer2 || serverLevel == 1) |
---|
244 | { |
---|
245 | interCommLeft.push_back(newComm) ; |
---|
246 | if (rank_==0) MPI_Send(&globalRank,1,MPI_INT,0,0,newComm) ; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | // (2) Create interComms between primary and secondary servers |
---|
251 | if (serverLevel == 1) |
---|
252 | { |
---|
253 | for (int i = 0; i < sndServerGlobalRanks.size(); ++i) |
---|
254 | { |
---|
255 | int srvSndLeader = sndServerGlobalRanks[i]; |
---|
256 | info(50)<<"intercommCreate::client (server level 1) "<<globalRank<<" intraCommSize : "<<size |
---|
257 | <<" intraCommRank :"<<rank_<<" clientLeader "<< srvSndLeader<<endl ; |
---|
258 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvSndLeader, 0, &newComm) ; |
---|
259 | interCommRight.push_back(newComm) ; |
---|
260 | } |
---|
261 | } |
---|
262 | else if (serverLevel == 2) |
---|
263 | { |
---|
264 | info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<size |
---|
265 | <<" intraCommRank :"<<rank_<<" clientLeader "<< srvGlobalRanks[0] <<endl ; |
---|
266 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvGlobalRanks[0], 0, &newComm) ; |
---|
267 | interCommLeft.push_back(newComm) ; |
---|
268 | } |
---|
269 | if (CXios::usingServer2) delete [] srvGlobalRanks ; |
---|
270 | oasis_enddef() ; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | MPI_Comm_rank(intraComm, &rank) ; |
---|
275 | if (rank==0) isRoot=true; |
---|
276 | else isRoot=false; |
---|
277 | |
---|
278 | eventScheduler = new CEventScheduler(intraComm) ; |
---|
279 | } |
---|
280 | |
---|
281 | void CServer::finalize(void) |
---|
282 | { |
---|
283 | CTimer::get("XIOS").suspend() ; |
---|
284 | |
---|
285 | delete eventScheduler ; |
---|
286 | |
---|
287 | for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++) |
---|
288 | MPI_Comm_free(&(*it)); |
---|
289 | |
---|
290 | for (std::list<MPI_Comm>::iterator it = contextIntraComms.begin(); it != contextIntraComms.end(); it++) |
---|
291 | MPI_Comm_free(&(*it)); |
---|
292 | |
---|
293 | // for (std::list<MPI_Comm>::iterator it = interComm.begin(); it != interComm.end(); it++) |
---|
294 | // MPI_Comm_free(&(*it)); |
---|
295 | |
---|
296 | // for (std::list<MPI_Comm>::iterator it = interCommLeft.begin(); it != interCommLeft.end(); it++) |
---|
297 | // MPI_Comm_free(&(*it)); |
---|
298 | |
---|
299 | for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++) |
---|
300 | MPI_Comm_free(&(*it)); |
---|
301 | |
---|
302 | MPI_Comm_free(&intraComm); |
---|
303 | |
---|
304 | if (!is_MPI_Initialized) |
---|
305 | { |
---|
306 | if (CXios::usingOasis) oasis_finalize(); |
---|
307 | else MPI_Finalize() ; |
---|
308 | } |
---|
309 | report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl ; |
---|
310 | report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl ; |
---|
311 | report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl ; |
---|
312 | report(100)<<CTimer::getAllCumulatedTime()<<endl ; |
---|
313 | } |
---|
314 | |
---|
315 | void CServer::eventLoop(void) |
---|
316 | { |
---|
317 | bool stop=false ; |
---|
318 | |
---|
319 | CTimer::get("XIOS server").resume() ; |
---|
320 | while(!stop) |
---|
321 | { |
---|
322 | if (isRoot) |
---|
323 | { |
---|
324 | listenContext(); |
---|
325 | listenRootContext(); |
---|
326 | if (!finished) listenFinalize() ; |
---|
327 | } |
---|
328 | else |
---|
329 | { |
---|
330 | listenRootContext(); |
---|
331 | if (!finished) listenRootFinalize() ; |
---|
332 | } |
---|
333 | |
---|
334 | contextEventLoop() ; |
---|
335 | if (finished && contextList.empty()) stop=true ; |
---|
336 | eventScheduler->checkEvent() ; |
---|
337 | } |
---|
338 | CTimer::get("XIOS server").suspend() ; |
---|
339 | } |
---|
340 | |
---|
341 | void CServer::listenFinalize(void) |
---|
342 | { |
---|
343 | list<MPI_Comm>::iterator it, itr; |
---|
344 | int msg ; |
---|
345 | int flag ; |
---|
346 | |
---|
347 | for(it=interCommLeft.begin();it!=interCommLeft.end();it++) |
---|
348 | { |
---|
349 | MPI_Status status ; |
---|
350 | traceOff() ; |
---|
351 | MPI_Iprobe(0,0,*it,&flag,&status) ; |
---|
352 | traceOn() ; |
---|
353 | if (flag==true) |
---|
354 | { |
---|
355 | MPI_Recv(&msg,1,MPI_INT,0,0,*it,&status) ; |
---|
356 | info(20)<<" CServer : Receive client finalize"<<endl ; |
---|
357 | // Sending server finalize message to secondary servers (if any) |
---|
358 | for(itr=interCommRight.begin();itr!=interCommRight.end();itr++) |
---|
359 | { |
---|
360 | MPI_Send(&msg,1,MPI_INT,0,0,*itr) ; |
---|
361 | } |
---|
362 | MPI_Comm_free(&(*it)); |
---|
363 | interCommLeft.erase(it) ; |
---|
364 | break ; |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | if (interCommLeft.empty()) |
---|
369 | { |
---|
370 | int i,size ; |
---|
371 | MPI_Comm_size(intraComm,&size) ; |
---|
372 | MPI_Request* requests= new MPI_Request[size-1] ; |
---|
373 | MPI_Status* status= new MPI_Status[size-1] ; |
---|
374 | |
---|
375 | for(int i=1;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,4,intraComm,&requests[i-1]) ; |
---|
376 | MPI_Waitall(size-1,requests,status) ; |
---|
377 | |
---|
378 | finished=true ; |
---|
379 | delete [] requests ; |
---|
380 | delete [] status ; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | |
---|
385 | void CServer::listenRootFinalize() |
---|
386 | { |
---|
387 | int flag ; |
---|
388 | MPI_Status status ; |
---|
389 | int msg ; |
---|
390 | |
---|
391 | traceOff() ; |
---|
392 | MPI_Iprobe(0,4,intraComm, &flag, &status) ; |
---|
393 | traceOn() ; |
---|
394 | if (flag==true) |
---|
395 | { |
---|
396 | MPI_Recv(&msg,1,MPI_INT,0,4,intraComm,&status) ; |
---|
397 | finished=true ; |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | void CServer::listenContext(void) |
---|
402 | { |
---|
403 | |
---|
404 | MPI_Status status ; |
---|
405 | int flag ; |
---|
406 | static char* buffer ; |
---|
407 | static MPI_Request request ; |
---|
408 | static bool recept=false ; |
---|
409 | int rank ; |
---|
410 | int count ; |
---|
411 | |
---|
412 | if (recept==false) |
---|
413 | { |
---|
414 | traceOff() ; |
---|
415 | MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ; |
---|
416 | traceOn() ; |
---|
417 | if (flag==true) |
---|
418 | { |
---|
419 | rank=status.MPI_SOURCE ; |
---|
420 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
421 | buffer=new char[count] ; |
---|
422 | MPI_Irecv((void*)buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ; |
---|
423 | recept=true ; |
---|
424 | } |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | traceOff() ; |
---|
429 | MPI_Test(&request,&flag,&status) ; |
---|
430 | traceOn() ; |
---|
431 | if (flag==true) |
---|
432 | { |
---|
433 | rank=status.MPI_SOURCE ; |
---|
434 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
435 | recvContextMessage((void*)buffer,count) ; |
---|
436 | delete [] buffer ; |
---|
437 | recept=false ; |
---|
438 | } |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | void CServer::recvContextMessage(void* buff,int count) |
---|
443 | { |
---|
444 | static map<string,contextMessage> recvContextId; |
---|
445 | map<string,contextMessage>::iterator it ; |
---|
446 | CBufferIn buffer(buff,count) ; |
---|
447 | string id ; |
---|
448 | int clientLeader ; |
---|
449 | int nbMessage ; |
---|
450 | |
---|
451 | buffer>>id>>nbMessage>>clientLeader ; |
---|
452 | |
---|
453 | it=recvContextId.find(id) ; |
---|
454 | if (it==recvContextId.end()) |
---|
455 | { |
---|
456 | contextMessage msg={0,0} ; |
---|
457 | pair<map<string,contextMessage>::iterator,bool> ret ; |
---|
458 | ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ; |
---|
459 | it=ret.first ; |
---|
460 | } |
---|
461 | it->second.nbRecv+=1 ; |
---|
462 | it->second.leaderRank+=clientLeader ; |
---|
463 | |
---|
464 | if (it->second.nbRecv==nbMessage) |
---|
465 | { |
---|
466 | int size ; |
---|
467 | MPI_Comm_size(intraComm,&size) ; |
---|
468 | // MPI_Request* requests= new MPI_Request[size-1] ; |
---|
469 | // MPI_Status* status= new MPI_Status[size-1] ; |
---|
470 | MPI_Request* requests= new MPI_Request[size] ; |
---|
471 | MPI_Status* status= new MPI_Status[size] ; |
---|
472 | |
---|
473 | CMessage msg ; |
---|
474 | msg<<id<<it->second.leaderRank; |
---|
475 | int messageSize=msg.size() ; |
---|
476 | void * sendBuff = new char[messageSize] ; |
---|
477 | CBufferOut sendBuffer(sendBuff,messageSize) ; |
---|
478 | sendBuffer<<msg ; |
---|
479 | |
---|
480 | // Include root itself in order not to have a divergence |
---|
481 | for(int i=0; i<size; i++) |
---|
482 | { |
---|
483 | MPI_Isend(sendBuff,count,MPI_CHAR,i,2,intraComm,&requests[i]) ; |
---|
484 | } |
---|
485 | |
---|
486 | recvContextId.erase(it) ; |
---|
487 | delete [] requests ; |
---|
488 | delete [] status ; |
---|
489 | |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | void CServer::listenRootContext(void) |
---|
494 | { |
---|
495 | MPI_Status status ; |
---|
496 | int flag ; |
---|
497 | static void* buffer ; |
---|
498 | static MPI_Request request ; |
---|
499 | static bool recept=false ; |
---|
500 | int rank ; |
---|
501 | // int count ; |
---|
502 | static int count ; |
---|
503 | const int root=0 ; |
---|
504 | boost::hash<string> hashString; |
---|
505 | size_t hashId = hashString("RegisterContext"); |
---|
506 | |
---|
507 | // (1) Receive context id from the root |
---|
508 | if (recept==false) |
---|
509 | { |
---|
510 | traceOff() ; |
---|
511 | MPI_Iprobe(root,2,intraComm, &flag, &status) ; |
---|
512 | traceOn() ; |
---|
513 | if (flag==true) |
---|
514 | { |
---|
515 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
516 | buffer=new char[count] ; |
---|
517 | MPI_Irecv((void*)buffer,count,MPI_CHAR,root,2,intraComm,&request) ; |
---|
518 | recept=true ; |
---|
519 | } |
---|
520 | } |
---|
521 | // (2) If context id is received, save it into a buffer and register an event |
---|
522 | else |
---|
523 | { |
---|
524 | MPI_Test(&request,&flag,&status) ; |
---|
525 | if (flag==true) |
---|
526 | { |
---|
527 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
528 | eventScheduler->registerEvent(nbContexts,hashId); |
---|
529 | // registerContext((void*)buffer,count) ; |
---|
530 | // delete [] buffer ; |
---|
531 | recept=false ; |
---|
532 | } |
---|
533 | } |
---|
534 | // (3) If event has been scheduled, call register context |
---|
535 | if (eventScheduler->queryEvent(nbContexts,hashId)) |
---|
536 | { |
---|
537 | registerContext(buffer,count) ; |
---|
538 | ++nbContexts; |
---|
539 | delete [] buffer ; |
---|
540 | } |
---|
541 | } |
---|
542 | |
---|
543 | void CServer::registerContext(void* buff, int count, int leaderRank) |
---|
544 | { |
---|
545 | string contextId; |
---|
546 | CBufferIn buffer(buff, count); |
---|
547 | // buffer >> contextId; |
---|
548 | buffer >> contextId>>leaderRank; |
---|
549 | CContext* context; |
---|
550 | |
---|
551 | info(20) << "CServer : Register new Context : " << contextId << endl; |
---|
552 | |
---|
553 | if (contextList.find(contextId) != contextList.end()) |
---|
554 | ERROR("void CServer::registerContext(void* buff, int count, int leaderRank)", |
---|
555 | << "Context '" << contextId << "' has already been registred"); |
---|
556 | |
---|
557 | context=CContext::create(contextId); |
---|
558 | contextList[contextId]=context; |
---|
559 | |
---|
560 | // Primary or classical server: create communication channel with a client |
---|
561 | // (1) create interComm (with a client) |
---|
562 | // (2) initialize client and server (contextClient and contextServer) |
---|
563 | MPI_Comm inter; |
---|
564 | if (serverLevel < 2) |
---|
565 | { |
---|
566 | MPI_Comm contextInterComm; |
---|
567 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, leaderRank, 10+leaderRank, &contextInterComm); |
---|
568 | MPI_Intercomm_merge(contextInterComm,1,&inter); |
---|
569 | MPI_Barrier(inter); |
---|
570 | MPI_Comm_free(&inter); |
---|
571 | context->initServer(intraComm,contextInterComm); |
---|
572 | contextInterComms.push_back(contextInterComm); |
---|
573 | |
---|
574 | } |
---|
575 | // Secondary server: create communication channel with a primary server |
---|
576 | // (1) duplicate interComm with a primary server |
---|
577 | // (2) initialize client and server (contextClient and contextServer) |
---|
578 | // Remark: in the case of the secondary server there is no need to create an interComm calling MPI_Intercomm_create, |
---|
579 | // because interComm of CContext is defined on the same processes as the interComm of CServer. |
---|
580 | // So just duplicate it. |
---|
581 | else if (serverLevel == 2) |
---|
582 | { |
---|
583 | MPI_Comm_dup(interCommLeft.front(), &inter); |
---|
584 | contextInterComms.push_back(inter); |
---|
585 | context->initServer(intraComm, contextInterComms.back()); |
---|
586 | } |
---|
587 | |
---|
588 | // Primary server: |
---|
589 | // (1) send create context message to secondary servers |
---|
590 | // (2) initialize communication channels with secondary servers (create contextClient and contextServer) |
---|
591 | if (serverLevel == 1) |
---|
592 | { |
---|
593 | int i = 0, size; |
---|
594 | CMessage msg; |
---|
595 | int messageSize; |
---|
596 | MPI_Comm_size(intraComm, &size) ; |
---|
597 | for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++, ++i) |
---|
598 | { |
---|
599 | StdString str = contextId +"_server_" + boost::lexical_cast<string>(i); |
---|
600 | msg<<str<<size<<rank_ ; |
---|
601 | messageSize = msg.size() ; |
---|
602 | buff = new char[messageSize] ; |
---|
603 | CBufferOut buffer(buff,messageSize) ; |
---|
604 | buffer<<msg ; |
---|
605 | MPI_Send(buff, buffer.count(), MPI_CHAR, sndServerGlobalRanks[i], 1, CXios::globalComm) ; |
---|
606 | MPI_Comm_dup(*it, &inter); |
---|
607 | contextInterComms.push_back(inter); |
---|
608 | MPI_Comm_dup(intraComm, &inter); |
---|
609 | contextIntraComms.push_back(inter); |
---|
610 | context->initClient(contextIntraComms.back(), contextInterComms.back()) ; |
---|
611 | delete [] buff ; |
---|
612 | } |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | void CServer::contextEventLoop(void) |
---|
617 | { |
---|
618 | bool isFinalized ; |
---|
619 | map<string,CContext*>::iterator it ; |
---|
620 | |
---|
621 | for(it=contextList.begin();it!=contextList.end();it++) |
---|
622 | { |
---|
623 | isFinalized=it->second->isFinalized(); |
---|
624 | if (isFinalized) |
---|
625 | { |
---|
626 | contextList.erase(it) ; |
---|
627 | break ; |
---|
628 | } |
---|
629 | else |
---|
630 | it->second->checkBuffersAndListen(); |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | //! Get rank of the current process in the intraComm |
---|
635 | int CServer::getRank() |
---|
636 | { |
---|
637 | int rank; |
---|
638 | MPI_Comm_rank(intraComm,&rank); |
---|
639 | return rank; |
---|
640 | } |
---|
641 | |
---|
642 | vector<int>& CServer::getSecondaryServerGlobalRanks() |
---|
643 | { |
---|
644 | return sndServerGlobalRanks; |
---|
645 | } |
---|
646 | |
---|
647 | /*! |
---|
648 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
649 | * The file name will be suffix+rank+extension. |
---|
650 | * |
---|
651 | * \param fileName[in] protype file name |
---|
652 | * \param ext [in] extension of the file |
---|
653 | * \param fb [in/out] the file buffer |
---|
654 | */ |
---|
655 | void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
656 | { |
---|
657 | StdStringStream fileNameClient; |
---|
658 | int numDigit = 0; |
---|
659 | int size = 0; |
---|
660 | int id; |
---|
661 | MPI_Comm_size(CXios::globalComm, &size); |
---|
662 | while (size) |
---|
663 | { |
---|
664 | size /= 10; |
---|
665 | ++numDigit; |
---|
666 | } |
---|
667 | id = rank_; //getRank(); |
---|
668 | |
---|
669 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << id << ext; |
---|
670 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
671 | if (!fb->is_open()) |
---|
672 | ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
673 | << std::endl << "Can not open <" << fileNameClient << "> file to write the server log(s)."); |
---|
674 | } |
---|
675 | |
---|
676 | /*! |
---|
677 | * \brief Open a file stream to write the info logs |
---|
678 | * Open a file stream with a specific file name suffix+rank |
---|
679 | * to write the info logs. |
---|
680 | * \param fileName [in] protype file name |
---|
681 | */ |
---|
682 | void CServer::openInfoStream(const StdString& fileName) |
---|
683 | { |
---|
684 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
685 | openStream(fileName, ".out", fb); |
---|
686 | |
---|
687 | info.write2File(fb); |
---|
688 | report.write2File(fb); |
---|
689 | } |
---|
690 | |
---|
691 | //! Write the info logs to standard output |
---|
692 | void CServer::openInfoStream() |
---|
693 | { |
---|
694 | info.write2StdOut(); |
---|
695 | report.write2StdOut(); |
---|
696 | } |
---|
697 | |
---|
698 | //! Close the info logs file if it opens |
---|
699 | void CServer::closeInfoStream() |
---|
700 | { |
---|
701 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
702 | } |
---|
703 | |
---|
704 | /*! |
---|
705 | * \brief Open a file stream to write the error log |
---|
706 | * Open a file stream with a specific file name suffix+rank |
---|
707 | * to write the error log. |
---|
708 | * \param fileName [in] protype file name |
---|
709 | */ |
---|
710 | void CServer::openErrorStream(const StdString& fileName) |
---|
711 | { |
---|
712 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
713 | openStream(fileName, ".err", fb); |
---|
714 | |
---|
715 | error.write2File(fb); |
---|
716 | } |
---|
717 | |
---|
718 | //! Write the error log to standard error output |
---|
719 | void CServer::openErrorStream() |
---|
720 | { |
---|
721 | error.write2StdErr(); |
---|
722 | } |
---|
723 | |
---|
724 | //! Close the error log file if it opens |
---|
725 | void CServer::closeErrorStream() |
---|
726 | { |
---|
727 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
728 | } |
---|
729 | } |
---|