1 | #include "context_server.hpp" |
---|
2 | #include "buffer_in.hpp" |
---|
3 | #include "type.hpp" |
---|
4 | #include "context.hpp" |
---|
5 | #include "object_template.hpp" |
---|
6 | #include "group_template.hpp" |
---|
7 | #include "attribute_template.hpp" |
---|
8 | #include "domain.hpp" |
---|
9 | #include "field.hpp" |
---|
10 | #include "file.hpp" |
---|
11 | #include "grid.hpp" |
---|
12 | #include "mpi.hpp" |
---|
13 | #include "tracer.hpp" |
---|
14 | #include "timer.hpp" |
---|
15 | #include "cxios.hpp" |
---|
16 | #include "event_scheduler.hpp" |
---|
17 | #include "server.hpp" |
---|
18 | #include "servers_ressource.hpp" |
---|
19 | #include "pool_ressource.hpp" |
---|
20 | #include "services.hpp" |
---|
21 | #include "contexts_manager.hpp" |
---|
22 | |
---|
23 | #include <boost/functional/hash.hpp> |
---|
24 | #include <random> |
---|
25 | #include <chrono> |
---|
26 | |
---|
27 | |
---|
28 | namespace xios |
---|
29 | { |
---|
30 | using namespace std ; |
---|
31 | |
---|
32 | CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_) : eventScheduler_(nullptr), isProcessingEvent_(false) |
---|
33 | { |
---|
34 | context=parent; |
---|
35 | intraComm=intraComm_; |
---|
36 | MPI_Comm_size(intraComm,&intraCommSize); |
---|
37 | MPI_Comm_rank(intraComm,&intraCommRank); |
---|
38 | |
---|
39 | interComm=interComm_; |
---|
40 | int flag; |
---|
41 | MPI_Comm_test_inter(interComm,&flag); |
---|
42 | |
---|
43 | if (flag) attachedMode=false ; |
---|
44 | else attachedMode=true ; |
---|
45 | |
---|
46 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
47 | else MPI_Comm_size(interComm,&commSize); |
---|
48 | |
---|
49 | |
---|
50 | SRegisterContextInfo contextInfo ; |
---|
51 | CXios::getContextsManager()->getContextInfo(context->getId(), contextInfo, intraComm) ; |
---|
52 | |
---|
53 | if (contextInfo.serviceType != CServicesManager::CLIENT) // we must have an event scheduler => to be retrieve from the associated services |
---|
54 | { |
---|
55 | if (!isAttachedModeEnabled()) eventScheduler_=CXios::getPoolRessource()->getService(contextInfo.serviceId,contextInfo.partitionId)->getEventScheduler() ; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | currentTimeLine=1; |
---|
60 | scheduled=false; |
---|
61 | finished=false; |
---|
62 | |
---|
63 | // generate unique hash for server |
---|
64 | auto time=chrono::system_clock::now().time_since_epoch().count() ; |
---|
65 | std::default_random_engine rd(time); // not reproducible from a run to another |
---|
66 | std::uniform_int_distribution<size_t> dist; |
---|
67 | hashId=dist(rd) ; |
---|
68 | MPI_Bcast(&hashId,1,MPI_SIZE_T,0,intraComm) ; // Bcast to all server of the context |
---|
69 | |
---|
70 | |
---|
71 | if (!isAttachedModeEnabled()) |
---|
72 | { |
---|
73 | MPI_Intercomm_merge(interComm_,true,&interCommMerged) ; |
---|
74 | // create windows for one sided comm |
---|
75 | int interCommMergedRank; |
---|
76 | MPI_Comm winComm ; |
---|
77 | MPI_Comm_rank(intraComm, &interCommMergedRank); |
---|
78 | windows.resize(2) ; |
---|
79 | for(int rank=commSize; rank<commSize+intraCommSize; rank++) |
---|
80 | { |
---|
81 | if (rank==commSize+interCommMergedRank) |
---|
82 | { |
---|
83 | MPI_Comm_split(interCommMerged, interCommMergedRank, rank, &winComm); |
---|
84 | int myRank ; |
---|
85 | MPI_Comm_rank(winComm,&myRank); |
---|
86 | MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[0]); |
---|
87 | MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[1]); |
---|
88 | } |
---|
89 | else MPI_Comm_split(interCommMerged, interCommMergedRank, rank, &winComm); |
---|
90 | MPI_Comm_free(&winComm) ; |
---|
91 | } |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | windows.resize(2) ; |
---|
96 | windows[0]=MPI_WIN_NULL ; |
---|
97 | windows[1]=MPI_WIN_NULL ; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | MPI_Comm_split(intraComm_,intraCommRank,intraCommRank, &commSelf) ; |
---|
103 | itLastTimeLine=lastTimeLine.begin() ; |
---|
104 | |
---|
105 | pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test) |
---|
106 | if (isAttachedModeEnabled()) pureOneSided=false ; // no one sided in attach mode |
---|
107 | |
---|
108 | } |
---|
109 | |
---|
110 | //! Attached mode is used ? |
---|
111 | //! \return true if attached mode is used, false otherwise |
---|
112 | bool CContextServer::isAttachedModeEnabled() const |
---|
113 | { |
---|
114 | return attachedMode ; |
---|
115 | } |
---|
116 | |
---|
117 | void CContextServer::setPendingEvent(void) |
---|
118 | { |
---|
119 | pendingEvent=true; |
---|
120 | } |
---|
121 | |
---|
122 | bool CContextServer::hasPendingEvent(void) |
---|
123 | { |
---|
124 | return pendingEvent; |
---|
125 | } |
---|
126 | |
---|
127 | bool CContextServer::hasFinished(void) |
---|
128 | { |
---|
129 | return finished; |
---|
130 | } |
---|
131 | |
---|
132 | bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/) |
---|
133 | { |
---|
134 | listen(); |
---|
135 | checkPendingRequest(); |
---|
136 | if (enableEventsProcessing) processEvents(); |
---|
137 | return finished; |
---|
138 | } |
---|
139 | |
---|
140 | void CContextServer::listen(void) |
---|
141 | { |
---|
142 | int rank; |
---|
143 | int flag; |
---|
144 | int count; |
---|
145 | char * addr; |
---|
146 | MPI_Status status; |
---|
147 | map<int,CServerBuffer*>::iterator it; |
---|
148 | bool okLoop; |
---|
149 | |
---|
150 | traceOff(); |
---|
151 | MPI_Iprobe(MPI_ANY_SOURCE, 20,interComm,&flag,&status); |
---|
152 | traceOn(); |
---|
153 | |
---|
154 | if (flag==true) |
---|
155 | { |
---|
156 | rank=status.MPI_SOURCE ; |
---|
157 | okLoop = true; |
---|
158 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
159 | okLoop = !listenPendingRequest(status) ; |
---|
160 | if (okLoop) |
---|
161 | { |
---|
162 | for(rank=0;rank<commSize;rank++) |
---|
163 | { |
---|
164 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
165 | { |
---|
166 | |
---|
167 | traceOff(); |
---|
168 | MPI_Iprobe(rank, 20,interComm,&flag,&status); |
---|
169 | traceOn(); |
---|
170 | if (flag==true) listenPendingRequest(status) ; |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | bool CContextServer::listenPendingRequest(MPI_Status& status) |
---|
178 | { |
---|
179 | int count; |
---|
180 | char * addr; |
---|
181 | map<int,CServerBuffer*>::iterator it; |
---|
182 | int rank=status.MPI_SOURCE ; |
---|
183 | |
---|
184 | it=buffers.find(rank); |
---|
185 | if (it==buffers.end()) // Receive the buffer size and allocate the buffer |
---|
186 | { |
---|
187 | MPI_Aint recvBuff[3] ; |
---|
188 | MPI_Recv(recvBuff, 3, MPI_AINT, rank, 20, interComm, &status); |
---|
189 | StdSize buffSize = recvBuff[0]; |
---|
190 | vector<MPI_Aint> winAdress(2) ; |
---|
191 | winAdress[0]=recvBuff[1] ; winAdress[1]=recvBuff[2] ; |
---|
192 | mapBufferSize_.insert(std::make_pair(rank, buffSize)); |
---|
193 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(windows, winAdress, rank, buffSize)))).first; |
---|
194 | |
---|
195 | lastTimeLine[rank]=0 ; |
---|
196 | itLastTimeLine=lastTimeLine.begin() ; |
---|
197 | |
---|
198 | return true; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
203 | if (it->second->isBufferFree(count)) |
---|
204 | { |
---|
205 | addr=(char*)it->second->getBuffer(count); |
---|
206 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]); |
---|
207 | bufferRequest[rank]=addr; |
---|
208 | return true; |
---|
209 | } |
---|
210 | else |
---|
211 | return false; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | void CContextServer::checkPendingRequest(void) |
---|
217 | { |
---|
218 | map<int,MPI_Request>::iterator it; |
---|
219 | list<int> recvRequest; |
---|
220 | list<int>::iterator itRecv; |
---|
221 | int rank; |
---|
222 | int flag; |
---|
223 | int count; |
---|
224 | MPI_Status status; |
---|
225 | |
---|
226 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
227 | { |
---|
228 | rank=it->first; |
---|
229 | traceOff(); |
---|
230 | MPI_Test(& it->second, &flag, &status); |
---|
231 | traceOn(); |
---|
232 | if (flag==true) |
---|
233 | { |
---|
234 | buffers[rank]->updateCurrentWindows() ; |
---|
235 | recvRequest.push_back(rank); |
---|
236 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
237 | processRequest(rank,bufferRequest[rank],count); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
242 | { |
---|
243 | pendingRequest.erase(*itRecv); |
---|
244 | bufferRequest.erase(*itRecv); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | void CContextServer::getBufferFromClient(size_t timeLine) |
---|
249 | { |
---|
250 | if (!isAttachedModeEnabled()) // one sided desactivated in attached mode |
---|
251 | { |
---|
252 | int rank ; |
---|
253 | char *buffer ; |
---|
254 | size_t count ; |
---|
255 | |
---|
256 | if (itLastTimeLine==lastTimeLine.end()) itLastTimeLine=lastTimeLine.begin() ; |
---|
257 | for(;itLastTimeLine!=lastTimeLine.end();++itLastTimeLine) |
---|
258 | { |
---|
259 | rank=itLastTimeLine->first ; |
---|
260 | if (itLastTimeLine->second < timeLine && pendingRequest.count(rank)==0) |
---|
261 | { |
---|
262 | if (buffers[rank]->getBufferFromClient(timeLine, buffer, count)) |
---|
263 | { |
---|
264 | processRequest(rank, buffer, count); |
---|
265 | break ; |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
274 | { |
---|
275 | |
---|
276 | CBufferIn buffer(buff,count); |
---|
277 | char* startBuffer,endBuffer; |
---|
278 | int size, offset; |
---|
279 | size_t timeLine=0; |
---|
280 | map<size_t,CEventServer*>::iterator it; |
---|
281 | |
---|
282 | |
---|
283 | CTimer::get("Process request").resume(); |
---|
284 | while(count>0) |
---|
285 | { |
---|
286 | char* startBuffer=(char*)buffer.ptr(); |
---|
287 | CBufferIn newBuffer(startBuffer,buffer.remain()); |
---|
288 | newBuffer>>size>>timeLine; |
---|
289 | it=events.find(timeLine); |
---|
290 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first; |
---|
291 | it->second->push(rank,buffers[rank],startBuffer,size); |
---|
292 | |
---|
293 | buffer.advance(size); |
---|
294 | count=buffer.remain(); |
---|
295 | } |
---|
296 | |
---|
297 | if (timeLine>0) lastTimeLine[rank]=timeLine ; |
---|
298 | |
---|
299 | CTimer::get("Process request").suspend(); |
---|
300 | } |
---|
301 | |
---|
302 | void CContextServer::processEvents(void) |
---|
303 | { |
---|
304 | map<size_t,CEventServer*>::iterator it; |
---|
305 | CEventServer* event; |
---|
306 | |
---|
307 | // if (context->isProcessingEvent()) return ; |
---|
308 | if (isProcessingEvent_) return ; |
---|
309 | |
---|
310 | it=events.find(currentTimeLine); |
---|
311 | if (it!=events.end()) |
---|
312 | { |
---|
313 | event=it->second; |
---|
314 | |
---|
315 | if (event->isFull()) |
---|
316 | { |
---|
317 | if (!scheduled && eventScheduler_) // Skip event scheduling for attached mode and reception on client side |
---|
318 | { |
---|
319 | eventScheduler_->registerEvent(currentTimeLine,hashId); |
---|
320 | scheduled=true; |
---|
321 | } |
---|
322 | else if (!eventScheduler_ || eventScheduler_->queryEvent(currentTimeLine,hashId) ) |
---|
323 | { |
---|
324 | // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes |
---|
325 | // The best way to properly solve this problem will be to use the event scheduler also in attached mode |
---|
326 | // for now just set up a MPI barrier |
---|
327 | if (!eventScheduler_ && CXios::isServer) MPI_Barrier(intraComm) ; |
---|
328 | |
---|
329 | // context->setProcessingEvent() ; |
---|
330 | isProcessingEvent_=true ; |
---|
331 | CTimer::get("Process events").resume(); |
---|
332 | dispatchEvent(*event); |
---|
333 | CTimer::get("Process events").suspend(); |
---|
334 | isProcessingEvent_=false ; |
---|
335 | // context->unsetProcessingEvent() ; |
---|
336 | pendingEvent=false; |
---|
337 | delete event; |
---|
338 | events.erase(it); |
---|
339 | currentTimeLine++; |
---|
340 | scheduled = false; |
---|
341 | } |
---|
342 | } |
---|
343 | else getBufferFromClient(currentTimeLine) ; |
---|
344 | } |
---|
345 | else if (pureOneSided) getBufferFromClient(currentTimeLine) ; // if pure one sided check buffer even if no event recorded at current time line |
---|
346 | } |
---|
347 | |
---|
348 | CContextServer::~CContextServer() |
---|
349 | { |
---|
350 | map<int,CServerBuffer*>::iterator it; |
---|
351 | for(it=buffers.begin();it!=buffers.end();++it) delete it->second; |
---|
352 | } |
---|
353 | |
---|
354 | void CContextServer::releaseBuffers() |
---|
355 | { |
---|
356 | map<int,CServerBuffer*>::iterator it; |
---|
357 | bool out ; |
---|
358 | do |
---|
359 | { |
---|
360 | out=true ; |
---|
361 | for(it=buffers.begin();it!=buffers.end();++it) |
---|
362 | { |
---|
363 | // out = out && it->second->freeWindows() ; |
---|
364 | |
---|
365 | } |
---|
366 | } while (! out) ; |
---|
367 | } |
---|
368 | |
---|
369 | void CContextServer::notifyClientsFinalize(void) |
---|
370 | { |
---|
371 | for(auto it=buffers.begin();it!=buffers.end();++it) |
---|
372 | { |
---|
373 | it->second->notifyClientFinalize() ; |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | void CContextServer::dispatchEvent(CEventServer& event) |
---|
378 | { |
---|
379 | string contextName; |
---|
380 | string buff; |
---|
381 | int MsgSize; |
---|
382 | int rank; |
---|
383 | list<CEventServer::SSubEvent>::iterator it; |
---|
384 | StdString ctxId = context->getId(); |
---|
385 | CContext::setCurrent(ctxId); |
---|
386 | StdSize totalBuf = 0; |
---|
387 | |
---|
388 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
389 | { |
---|
390 | finished=true; |
---|
391 | info(20)<<" CContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl; |
---|
392 | // releaseBuffers() ; |
---|
393 | notifyClientsFinalize() ; |
---|
394 | context->finalize(); |
---|
395 | |
---|
396 | /* don't know where release windows |
---|
397 | MPI_Win_free(&windows[0]) ; |
---|
398 | MPI_Win_free(&windows[1]) ; |
---|
399 | */ |
---|
400 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
401 | iteMap = mapBufferSize_.end(), itMap; |
---|
402 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
403 | { |
---|
404 | rank = itMap->first; |
---|
405 | report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
406 | << " +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl; |
---|
407 | totalBuf += itMap->second; |
---|
408 | } |
---|
409 | report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
410 | } |
---|
411 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
412 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
413 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
414 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
415 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
416 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
417 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
418 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
419 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
420 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
421 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
422 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event); |
---|
423 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
424 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
425 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
426 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
427 | else |
---|
428 | { |
---|
429 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
430 | } |
---|
431 | } |
---|
432 | } |
---|