1 | #include "xios_spl.hpp" |
---|
2 | #include "context_client.hpp" |
---|
3 | #include "context_server.hpp" |
---|
4 | #include "event_client.hpp" |
---|
5 | #include "buffer_out.hpp" |
---|
6 | #include "buffer_client.hpp" |
---|
7 | #include "type.hpp" |
---|
8 | #include "event_client.hpp" |
---|
9 | #include "context.hpp" |
---|
10 | #include "mpi.hpp" |
---|
11 | #include "timer.hpp" |
---|
12 | #include "cxios.hpp" |
---|
13 | #include "server.hpp" |
---|
14 | |
---|
15 | namespace xios |
---|
16 | { |
---|
17 | /*! |
---|
18 | \param [in] parent Pointer to context on client side |
---|
19 | \param [in] intraComm_ communicator of group client |
---|
20 | \param [in] interComm_ communicator of group server |
---|
21 | \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode). |
---|
22 | */ |
---|
23 | CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer) |
---|
24 | : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4) |
---|
25 | { |
---|
26 | |
---|
27 | context = parent; |
---|
28 | intraComm = intraComm_; |
---|
29 | interComm = interComm_; |
---|
30 | MPI_Comm_rank(intraComm, &clientRank); |
---|
31 | MPI_Comm_size(intraComm, &clientSize); |
---|
32 | |
---|
33 | int flag; |
---|
34 | MPI_Comm_test_inter(interComm, &flag); |
---|
35 | if (flag) isAttached_=false ; |
---|
36 | else isAttached_=true ; |
---|
37 | |
---|
38 | pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test) |
---|
39 | if (isAttachedModeEnabled()) pureOneSided=false ; // no one sided in attach mode |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | if (flag) MPI_Comm_remote_size(interComm, &serverSize); |
---|
44 | else MPI_Comm_size(interComm, &serverSize); |
---|
45 | |
---|
46 | computeLeader(clientRank, clientSize, serverSize, ranksServerLeader, ranksServerNotLeader); |
---|
47 | |
---|
48 | if (flag) MPI_Intercomm_merge(interComm_,false,&interCommMerged) ; |
---|
49 | |
---|
50 | if (!isAttachedModeEnabled()) |
---|
51 | { |
---|
52 | windows.resize(serverSize) ; |
---|
53 | MPI_Comm winComm ; |
---|
54 | for(int rank=0; rank<serverSize; rank++) |
---|
55 | { |
---|
56 | windows[rank].resize(2) ; |
---|
57 | MPI_Comm_split(interCommMerged, rank, clientRank, &winComm); |
---|
58 | int myRank ; |
---|
59 | MPI_Comm_rank(winComm,&myRank); |
---|
60 | MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[rank][0]); |
---|
61 | MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[rank][1]); |
---|
62 | MPI_Comm_free(&winComm) ; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | MPI_Comm_split(intraComm_,clientRank,clientRank, &commSelf) ; |
---|
67 | |
---|
68 | timeLine = 1; |
---|
69 | } |
---|
70 | |
---|
71 | void CContextClient::computeLeader(int clientRank, int clientSize, int serverSize, |
---|
72 | std::list<int>& rankRecvLeader, |
---|
73 | std::list<int>& rankRecvNotLeader) |
---|
74 | { |
---|
75 | if ((0 == clientSize) || (0 == serverSize)) return; |
---|
76 | |
---|
77 | if (clientSize < serverSize) |
---|
78 | { |
---|
79 | int serverByClient = serverSize / clientSize; |
---|
80 | int remain = serverSize % clientSize; |
---|
81 | int rankStart = serverByClient * clientRank; |
---|
82 | |
---|
83 | if (clientRank < remain) |
---|
84 | { |
---|
85 | serverByClient++; |
---|
86 | rankStart += clientRank; |
---|
87 | } |
---|
88 | else |
---|
89 | rankStart += remain; |
---|
90 | |
---|
91 | for (int i = 0; i < serverByClient; i++) |
---|
92 | rankRecvLeader.push_back(rankStart + i); |
---|
93 | |
---|
94 | rankRecvNotLeader.resize(0); |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | int clientByServer = clientSize / serverSize; |
---|
99 | int remain = clientSize % serverSize; |
---|
100 | |
---|
101 | if (clientRank < (clientByServer + 1) * remain) |
---|
102 | { |
---|
103 | if (clientRank % (clientByServer + 1) == 0) |
---|
104 | rankRecvLeader.push_back(clientRank / (clientByServer + 1)); |
---|
105 | else |
---|
106 | rankRecvNotLeader.push_back(clientRank / (clientByServer + 1)); |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | int rank = clientRank - (clientByServer + 1) * remain; |
---|
111 | if (rank % clientByServer == 0) |
---|
112 | rankRecvLeader.push_back(remain + rank / clientByServer); |
---|
113 | else |
---|
114 | rankRecvNotLeader.push_back(remain + rank / clientByServer); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /*! |
---|
120 | In case of attached mode, the current context must be reset to context for client |
---|
121 | \param [in] event Event sent to server |
---|
122 | */ |
---|
123 | void CContextClient::sendEvent(CEventClient& event) |
---|
124 | { |
---|
125 | list<int> ranks = event.getRanks(); |
---|
126 | info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<endl ; |
---|
127 | if (CXios::checkEventSync) |
---|
128 | { |
---|
129 | int typeId, classId, typeId_in, classId_in, timeLine_out; |
---|
130 | typeId_in=event.getTypeId() ; |
---|
131 | classId_in=event.getClassId() ; |
---|
132 | // MPI_Allreduce(&timeLine,&timeLine_out, 1, MPI_UINT64_T, MPI_SUM, intraComm) ; // MPI_UINT64_T standardized by MPI 3 |
---|
133 | MPI_Allreduce(&timeLine,&timeLine_out, 1, MPI_LONG_LONG_INT, MPI_SUM, intraComm) ; |
---|
134 | MPI_Allreduce(&typeId_in,&typeId, 1, MPI_INT, MPI_SUM, intraComm) ; |
---|
135 | MPI_Allreduce(&classId_in,&classId, 1, MPI_INT, MPI_SUM, intraComm) ; |
---|
136 | if (typeId/clientSize!=event.getTypeId() || classId/clientSize!=event.getClassId() || timeLine_out/clientSize!=timeLine) |
---|
137 | { |
---|
138 | ERROR("void CContextClient::sendEvent(CEventClient& event)", |
---|
139 | << "Event are not coherent between client."); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | if (!event.isEmpty()) |
---|
144 | { |
---|
145 | list<int> sizes = event.getSizes(); |
---|
146 | |
---|
147 | // We force the getBuffers call to be non-blocking on classical servers |
---|
148 | list<CBufferOut*> buffList; |
---|
149 | getBuffers(timeLine, ranks, sizes, buffList) ; |
---|
150 | |
---|
151 | event.send(timeLine, sizes, buffList); |
---|
152 | |
---|
153 | //for (auto itRank = ranks.begin(); itRank != ranks.end(); itRank++) buffers[*itRank]->infoBuffer() ; |
---|
154 | |
---|
155 | unlockBuffers(ranks) ; |
---|
156 | info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<" sent"<<endl ; |
---|
157 | |
---|
158 | checkBuffers(ranks); |
---|
159 | } |
---|
160 | |
---|
161 | if (isAttachedModeEnabled()) // couldBuffer is always true in attached mode |
---|
162 | { |
---|
163 | waitEvent(ranks); |
---|
164 | CContext::setCurrent(context->getId()); |
---|
165 | } |
---|
166 | |
---|
167 | timeLine++; |
---|
168 | } |
---|
169 | |
---|
170 | /*! |
---|
171 | If client is also server (attached mode), after sending event, it should process right away |
---|
172 | the incoming event. |
---|
173 | \param [in] ranks list rank of server connected this client |
---|
174 | */ |
---|
175 | void CContextClient::waitEvent(list<int>& ranks) |
---|
176 | { |
---|
177 | while (checkBuffers(ranks)) |
---|
178 | { |
---|
179 | CXios::getDaemonsManager()->eventLoop() ; |
---|
180 | } |
---|
181 | |
---|
182 | MPI_Request req ; |
---|
183 | MPI_Status status ; |
---|
184 | |
---|
185 | MPI_Ibarrier(intraComm,&req) ; |
---|
186 | int flag=false ; |
---|
187 | |
---|
188 | do |
---|
189 | { |
---|
190 | CXios::getDaemonsManager()->eventLoop() ; |
---|
191 | MPI_Test(&req,&flag,&status) ; |
---|
192 | } while (!flag) ; |
---|
193 | |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | void CContextClient::waitEvent_old(list<int>& ranks) |
---|
199 | { |
---|
200 | parentServer->server->setPendingEvent(); |
---|
201 | while (checkBuffers(ranks)) |
---|
202 | { |
---|
203 | parentServer->server->listen(); |
---|
204 | parentServer->server->checkPendingRequest(); |
---|
205 | } |
---|
206 | |
---|
207 | while (parentServer->server->hasPendingEvent()) |
---|
208 | { |
---|
209 | parentServer->server->eventLoop(); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | /*! |
---|
214 | * Get buffers for each connection to the servers. This function blocks until there is enough room in the buffers unless |
---|
215 | * it is explicitly requested to be non-blocking. |
---|
216 | * |
---|
217 | * |
---|
218 | * \param [in] timeLine time line of the event which will be sent to servers |
---|
219 | * \param [in] serverList list of rank of connected server |
---|
220 | * \param [in] sizeList size of message corresponding to each connection |
---|
221 | * \param [out] retBuffers list of buffers that can be used to store an event |
---|
222 | * \param [in] nonBlocking whether this function should be non-blocking |
---|
223 | * \return whether the already allocated buffers could be used |
---|
224 | */ |
---|
225 | bool CContextClient::getBuffers(const size_t timeLine, const list<int>& serverList, const list<int>& sizeList, list<CBufferOut*>& retBuffers, |
---|
226 | bool nonBlocking /*= false*/) |
---|
227 | { |
---|
228 | list<int>::const_iterator itServer, itSize; |
---|
229 | list<CClientBuffer*> bufferList; |
---|
230 | map<int,CClientBuffer*>::const_iterator it; |
---|
231 | list<CClientBuffer*>::iterator itBuffer; |
---|
232 | bool areBuffersFree; |
---|
233 | |
---|
234 | for (itServer = serverList.begin(); itServer != serverList.end(); itServer++) |
---|
235 | { |
---|
236 | it = buffers.find(*itServer); |
---|
237 | if (it == buffers.end()) |
---|
238 | { |
---|
239 | newBuffer(*itServer); |
---|
240 | it = buffers.find(*itServer); |
---|
241 | } |
---|
242 | bufferList.push_back(it->second); |
---|
243 | } |
---|
244 | |
---|
245 | CTimer::get("Blocking time").resume(); |
---|
246 | do |
---|
247 | { |
---|
248 | areBuffersFree = true; |
---|
249 | for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++) |
---|
250 | { |
---|
251 | areBuffersFree &= (*itBuffer)->isBufferFree(*itSize); |
---|
252 | } |
---|
253 | |
---|
254 | if (!areBuffersFree) |
---|
255 | { |
---|
256 | for (itBuffer = bufferList.begin(); itBuffer != bufferList.end(); itBuffer++) (*itBuffer)->unlockBuffer(); |
---|
257 | checkBuffers(); |
---|
258 | |
---|
259 | context->server->listen(); |
---|
260 | |
---|
261 | if (context->serverPrimServer.size()>0) |
---|
262 | { |
---|
263 | for (int i = 0; i < context->serverPrimServer.size(); ++i) context->serverPrimServer[i]->listen(); |
---|
264 | //ym CServer::contextEventLoop(false) ; // avoid dead-lock at finalize... |
---|
265 | context->globalEventLoop() ; |
---|
266 | } |
---|
267 | |
---|
268 | } |
---|
269 | } while (!areBuffersFree && !nonBlocking); |
---|
270 | CTimer::get("Blocking time").suspend(); |
---|
271 | |
---|
272 | if (areBuffersFree) |
---|
273 | { |
---|
274 | for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++) |
---|
275 | retBuffers.push_back((*itBuffer)->getBuffer(timeLine, *itSize)); |
---|
276 | } |
---|
277 | return areBuffersFree; |
---|
278 | } |
---|
279 | |
---|
280 | /*! |
---|
281 | Make a new buffer for a certain connection to server with specific rank |
---|
282 | \param [in] rank rank of connected server |
---|
283 | */ |
---|
284 | void CContextClient::newBuffer(int rank) |
---|
285 | { |
---|
286 | if (!mapBufferSize_.count(rank)) |
---|
287 | { |
---|
288 | error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl; |
---|
289 | mapBufferSize_[rank] = CXios::minBufferSize; |
---|
290 | maxEventSizes[rank] = CXios::minBufferSize; |
---|
291 | } |
---|
292 | |
---|
293 | vector<MPI_Win> Wins(2,MPI_WIN_NULL) ; |
---|
294 | if (!isAttachedModeEnabled()) Wins=windows[rank] ; |
---|
295 | |
---|
296 | CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, Wins, clientRank, rank, mapBufferSize_[rank], maxEventSizes[rank]); |
---|
297 | // Notify the server |
---|
298 | CBufferOut* bufOut = buffer->getBuffer(0, 3*sizeof(MPI_Aint)); |
---|
299 | MPI_Aint sendBuff[3] ; |
---|
300 | sendBuff[0]=mapBufferSize_[rank]; // Stupid C++ |
---|
301 | sendBuff[1]=buffers[rank]->getWinAddress(0); |
---|
302 | sendBuff[2]=buffers[rank]->getWinAddress(1); |
---|
303 | info(100)<<"CContextClient::newBuffer : rank "<<rank<<" winAdress[0] "<<buffers[rank]->getWinAddress(0)<<" winAdress[1] "<<buffers[rank]->getWinAddress(1)<<endl; |
---|
304 | bufOut->put(sendBuff, 3); // Stupid C++ |
---|
305 | buffer->checkBuffer(true); |
---|
306 | |
---|
307 | } |
---|
308 | |
---|
309 | /*! |
---|
310 | Verify state of buffers. Buffer is under pending state if there is no message on it |
---|
311 | \return state of buffers, pending(true), ready(false) |
---|
312 | */ |
---|
313 | bool CContextClient::checkBuffers(void) |
---|
314 | { |
---|
315 | map<int,CClientBuffer*>::iterator itBuff; |
---|
316 | bool pending = false; |
---|
317 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) |
---|
318 | pending |= itBuff->second->checkBuffer(!pureOneSided); |
---|
319 | return pending; |
---|
320 | } |
---|
321 | |
---|
322 | //! Release all buffers |
---|
323 | void CContextClient::releaseBuffers() |
---|
324 | { |
---|
325 | map<int,CClientBuffer*>::iterator itBuff; |
---|
326 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) |
---|
327 | { |
---|
328 | delete itBuff->second; |
---|
329 | } |
---|
330 | buffers.clear(); |
---|
331 | |
---|
332 | /* don't know when release windows |
---|
333 | |
---|
334 | if (!isAttachedModeEnabled()) |
---|
335 | { |
---|
336 | for(int rank=0; rank<serverSize; rank++) |
---|
337 | { |
---|
338 | MPI_Win_free(&windows[rank][0]); |
---|
339 | MPI_Win_free(&windows[rank][1]); |
---|
340 | } |
---|
341 | } |
---|
342 | */ |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | /*! |
---|
347 | Lock the buffers for one sided communications |
---|
348 | \param [in] ranks list rank of server to which client connects to |
---|
349 | */ |
---|
350 | void CContextClient::lockBuffers(list<int>& ranks) |
---|
351 | { |
---|
352 | list<int>::iterator it; |
---|
353 | for (it = ranks.begin(); it != ranks.end(); it++) buffers[*it]->lockBuffer(); |
---|
354 | } |
---|
355 | |
---|
356 | /*! |
---|
357 | Unlock the buffers for one sided communications |
---|
358 | \param [in] ranks list rank of server to which client connects to |
---|
359 | */ |
---|
360 | void CContextClient::unlockBuffers(list<int>& ranks) |
---|
361 | { |
---|
362 | list<int>::iterator it; |
---|
363 | for (it = ranks.begin(); it != ranks.end(); it++) buffers[*it]->unlockBuffer(); |
---|
364 | } |
---|
365 | |
---|
366 | /*! |
---|
367 | Verify state of buffers corresponding to a connection |
---|
368 | \param [in] ranks list rank of server to which client connects to |
---|
369 | \return state of buffers, pending(true), ready(false) |
---|
370 | */ |
---|
371 | bool CContextClient::checkBuffers(list<int>& ranks) |
---|
372 | { |
---|
373 | list<int>::iterator it; |
---|
374 | bool pending = false; |
---|
375 | for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer(!pureOneSided); |
---|
376 | return pending; |
---|
377 | } |
---|
378 | |
---|
379 | /*! |
---|
380 | * Set the buffer size for each connection. Warning: This function is collective. |
---|
381 | * |
---|
382 | * \param [in] mapSize maps the rank of the connected servers to the size of the correspoinding buffer |
---|
383 | * \param [in] maxEventSize maps the rank of the connected servers to the size of the biggest event |
---|
384 | */ |
---|
385 | void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize) |
---|
386 | { |
---|
387 | mapBufferSize_ = mapSize; |
---|
388 | maxEventSizes = maxEventSize; |
---|
389 | } |
---|
390 | |
---|
391 | /*! |
---|
392 | Get leading server in the group of connected server |
---|
393 | \return ranks of leading servers |
---|
394 | */ |
---|
395 | const std::list<int>& CContextClient::getRanksServerNotLeader(void) const |
---|
396 | { |
---|
397 | return ranksServerNotLeader; |
---|
398 | } |
---|
399 | |
---|
400 | /*! |
---|
401 | Check if client connects to leading server |
---|
402 | \return connected(true), not connected (false) |
---|
403 | */ |
---|
404 | bool CContextClient::isServerNotLeader(void) const |
---|
405 | { |
---|
406 | return !ranksServerNotLeader.empty(); |
---|
407 | } |
---|
408 | |
---|
409 | /*! |
---|
410 | Get leading server in the group of connected server |
---|
411 | \return ranks of leading servers |
---|
412 | */ |
---|
413 | const std::list<int>& CContextClient::getRanksServerLeader(void) const |
---|
414 | { |
---|
415 | return ranksServerLeader; |
---|
416 | } |
---|
417 | |
---|
418 | /*! |
---|
419 | Check if client connects to leading server |
---|
420 | \return connected(true), not connected (false) |
---|
421 | */ |
---|
422 | bool CContextClient::isServerLeader(void) const |
---|
423 | { |
---|
424 | return !ranksServerLeader.empty(); |
---|
425 | } |
---|
426 | |
---|
427 | /*! |
---|
428 | * Finalize context client and do some reports. Function is non-blocking. |
---|
429 | */ |
---|
430 | void CContextClient::finalize(void) |
---|
431 | { |
---|
432 | map<int,CClientBuffer*>::iterator itBuff; |
---|
433 | std::list<int>::iterator ItServerLeader; |
---|
434 | |
---|
435 | bool stop = false; |
---|
436 | |
---|
437 | int* nbServerConnectionLocal = new int[serverSize] ; |
---|
438 | int* nbServerConnectionGlobal = new int[serverSize] ; |
---|
439 | for(int i=0;i<serverSize;++i) nbServerConnectionLocal[i]=0 ; |
---|
440 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) nbServerConnectionLocal[itBuff->first]=1 ; |
---|
441 | for (ItServerLeader = ranksServerLeader.begin(); ItServerLeader != ranksServerLeader.end(); ItServerLeader++) nbServerConnectionLocal[*ItServerLeader]=1 ; |
---|
442 | |
---|
443 | MPI_Allreduce(nbServerConnectionLocal, nbServerConnectionGlobal, serverSize, MPI_INT, MPI_SUM, intraComm); |
---|
444 | |
---|
445 | CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE); |
---|
446 | CMessage msg; |
---|
447 | |
---|
448 | for (int i=0;i<serverSize;++i) if (nbServerConnectionLocal[i]==1) event.push(i, nbServerConnectionGlobal[i], msg) ; |
---|
449 | sendEvent(event); |
---|
450 | |
---|
451 | delete[] nbServerConnectionLocal ; |
---|
452 | delete[] nbServerConnectionGlobal ; |
---|
453 | |
---|
454 | |
---|
455 | CTimer::get("Blocking time").resume(); |
---|
456 | checkBuffers(); |
---|
457 | CTimer::get("Blocking time").suspend(); |
---|
458 | |
---|
459 | std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
460 | iteMap = mapBufferSize_.end(), itMap; |
---|
461 | |
---|
462 | StdSize totalBuf = 0; |
---|
463 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
464 | { |
---|
465 | report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl |
---|
466 | << " +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl; |
---|
467 | totalBuf += itMap->second; |
---|
468 | } |
---|
469 | report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl; |
---|
470 | |
---|
471 | } |
---|
472 | |
---|
473 | |
---|
474 | /*! |
---|
475 | */ |
---|
476 | bool CContextClient::havePendingRequests(void) |
---|
477 | { |
---|
478 | bool pending = false; |
---|
479 | map<int,CClientBuffer*>::iterator itBuff; |
---|
480 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) |
---|
481 | pending |= itBuff->second->hasPendingRequest(); |
---|
482 | return pending; |
---|
483 | } |
---|
484 | |
---|
485 | bool CContextClient::isNotifiedFinalized(void) |
---|
486 | { |
---|
487 | if (isAttachedModeEnabled()) return true ; |
---|
488 | |
---|
489 | bool finalized = true; |
---|
490 | map<int,CClientBuffer*>::iterator itBuff; |
---|
491 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) |
---|
492 | finalized &= itBuff->second->isNotifiedFinalized(); |
---|
493 | return finalized; |
---|
494 | } |
---|
495 | |
---|
496 | } |
---|