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 <boost/functional/hash.hpp> |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | namespace xios |
---|
23 | { |
---|
24 | |
---|
25 | CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_) |
---|
26 | { |
---|
27 | context=parent; |
---|
28 | intraComm=intraComm_; |
---|
29 | MPI_Comm_size(intraComm,&intraCommSize); |
---|
30 | MPI_Comm_rank(intraComm,&intraCommRank); |
---|
31 | |
---|
32 | interComm=interComm_; |
---|
33 | int flag; |
---|
34 | MPI_Comm_test_inter(interComm,&flag); |
---|
35 | |
---|
36 | if (flag) attachedMode=false ; |
---|
37 | else attachedMode=true ; |
---|
38 | |
---|
39 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
40 | else MPI_Comm_size(interComm,&commSize); |
---|
41 | |
---|
42 | |
---|
43 | currentTimeLine=1; |
---|
44 | scheduled=false; |
---|
45 | finished=false; |
---|
46 | boost::hash<string> hashString; |
---|
47 | if (CServer::serverLevel == 1) |
---|
48 | hashId=hashString(context->getId() + boost::lexical_cast<string>(context->clientPrimServer.size())); |
---|
49 | else |
---|
50 | hashId=hashString(context->getId()); |
---|
51 | |
---|
52 | if (!isAttachedModeEnabled()) MPI_Intercomm_merge(interComm_,true,&interCommMerged) ; |
---|
53 | |
---|
54 | MPI_Comm_split(intraComm_,intraCommRank,intraCommRank, &commSelf) ; |
---|
55 | itLastTimeLine=lastTimeLine.begin() ; |
---|
56 | |
---|
57 | pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test) |
---|
58 | if (isAttachedModeEnabled()) pureOneSided=false ; // no one sided in attach mode |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | //! Attached mode is used ? |
---|
63 | //! \return true if attached mode is used, false otherwise |
---|
64 | bool CContextServer::isAttachedModeEnabled() const |
---|
65 | { |
---|
66 | return attachedMode ; |
---|
67 | } |
---|
68 | |
---|
69 | void CContextServer::setPendingEvent(void) |
---|
70 | { |
---|
71 | pendingEvent=true; |
---|
72 | } |
---|
73 | |
---|
74 | bool CContextServer::hasPendingEvent(void) |
---|
75 | { |
---|
76 | return pendingEvent; |
---|
77 | } |
---|
78 | |
---|
79 | bool CContextServer::hasFinished(void) |
---|
80 | { |
---|
81 | return finished; |
---|
82 | } |
---|
83 | |
---|
84 | bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/) |
---|
85 | { |
---|
86 | listen(); |
---|
87 | checkPendingRequest(); |
---|
88 | if (enableEventsProcessing) processEvents(); |
---|
89 | return finished; |
---|
90 | } |
---|
91 | |
---|
92 | void CContextServer::listen(void) |
---|
93 | { |
---|
94 | int rank; |
---|
95 | int flag; |
---|
96 | int count; |
---|
97 | char * addr; |
---|
98 | MPI_Status status; |
---|
99 | map<int,CServerBuffer*>::iterator it; |
---|
100 | bool okLoop; |
---|
101 | |
---|
102 | traceOff(); |
---|
103 | MPI_Iprobe(MPI_ANY_SOURCE, 20,interComm,&flag,&status); |
---|
104 | traceOn(); |
---|
105 | |
---|
106 | if (flag==true) |
---|
107 | { |
---|
108 | rank=status.MPI_SOURCE ; |
---|
109 | okLoop = true; |
---|
110 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
111 | okLoop = !listenPendingRequest(status) ; |
---|
112 | if (okLoop) |
---|
113 | { |
---|
114 | for(rank=0;rank<commSize;rank++) |
---|
115 | { |
---|
116 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
117 | { |
---|
118 | |
---|
119 | traceOff(); |
---|
120 | MPI_Iprobe(rank, 20,interComm,&flag,&status); |
---|
121 | traceOn(); |
---|
122 | if (flag==true) listenPendingRequest(status) ; |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | bool CContextServer::listenPendingRequest(MPI_Status& status) |
---|
130 | { |
---|
131 | int count; |
---|
132 | char * addr; |
---|
133 | map<int,CServerBuffer*>::iterator it; |
---|
134 | int rank=status.MPI_SOURCE ; |
---|
135 | |
---|
136 | it=buffers.find(rank); |
---|
137 | if (it==buffers.end()) // Receive the buffer size and allocate the buffer |
---|
138 | { |
---|
139 | StdSize buffSize = 0; |
---|
140 | MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status); |
---|
141 | mapBufferSize_.insert(std::make_pair(rank, buffSize)); |
---|
142 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first; |
---|
143 | if (!isAttachedModeEnabled()) |
---|
144 | { |
---|
145 | MPI_Comm OneSidedInterComm, oneSidedComm ; |
---|
146 | MPI_Intercomm_create(commSelf, 0, interCommMerged, rank, 0, &OneSidedInterComm ); |
---|
147 | MPI_Intercomm_merge(OneSidedInterComm,true,&oneSidedComm); |
---|
148 | buffers[rank]->createWindows(oneSidedComm) ; |
---|
149 | } |
---|
150 | lastTimeLine[rank]=0 ; |
---|
151 | itLastTimeLine=lastTimeLine.begin() ; |
---|
152 | |
---|
153 | return true; |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
158 | if (it->second->isBufferFree(count)) |
---|
159 | { |
---|
160 | addr=(char*)it->second->getBuffer(count); |
---|
161 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]); |
---|
162 | bufferRequest[rank]=addr; |
---|
163 | return true; |
---|
164 | } |
---|
165 | else |
---|
166 | return false; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void CContextServer::checkPendingRequest(void) |
---|
172 | { |
---|
173 | map<int,MPI_Request>::iterator it; |
---|
174 | list<int> recvRequest; |
---|
175 | list<int>::iterator itRecv; |
---|
176 | int rank; |
---|
177 | int flag; |
---|
178 | int count; |
---|
179 | MPI_Status status; |
---|
180 | |
---|
181 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
182 | { |
---|
183 | rank=it->first; |
---|
184 | traceOff(); |
---|
185 | MPI_Test(& it->second, &flag, &status); |
---|
186 | traceOn(); |
---|
187 | if (flag==true) |
---|
188 | { |
---|
189 | buffers[rank]->updateCurrentWindows() ; |
---|
190 | recvRequest.push_back(rank); |
---|
191 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
192 | processRequest(rank,bufferRequest[rank],count); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
197 | { |
---|
198 | pendingRequest.erase(*itRecv); |
---|
199 | bufferRequest.erase(*itRecv); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | void CContextServer::getBufferFromClient(size_t timeLine) |
---|
204 | { |
---|
205 | if (!isAttachedModeEnabled()) // one sided desactivated in attached mode |
---|
206 | { |
---|
207 | int rank ; |
---|
208 | char *buffer ; |
---|
209 | size_t count ; |
---|
210 | |
---|
211 | if (itLastTimeLine==lastTimeLine.end()) itLastTimeLine=lastTimeLine.begin() ; |
---|
212 | for(;itLastTimeLine!=lastTimeLine.end();++itLastTimeLine) |
---|
213 | { |
---|
214 | rank=itLastTimeLine->first ; |
---|
215 | if (itLastTimeLine->second < timeLine && pendingRequest.count(rank)==0) |
---|
216 | { |
---|
217 | if (buffers[rank]->getBufferFromClient(timeLine, buffer, count)) |
---|
218 | { |
---|
219 | processRequest(rank, buffer, count); |
---|
220 | break ; |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
229 | { |
---|
230 | |
---|
231 | CBufferIn buffer(buff,count); |
---|
232 | char* startBuffer,endBuffer; |
---|
233 | int size, offset; |
---|
234 | size_t timeLine=0; |
---|
235 | map<size_t,CEventServer*>::iterator it; |
---|
236 | |
---|
237 | |
---|
238 | CTimer::get("Process request").resume(); |
---|
239 | while(count>0) |
---|
240 | { |
---|
241 | char* startBuffer=(char*)buffer.ptr(); |
---|
242 | CBufferIn newBuffer(startBuffer,buffer.remain()); |
---|
243 | newBuffer>>size>>timeLine; |
---|
244 | it=events.find(timeLine); |
---|
245 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first; |
---|
246 | it->second->push(rank,buffers[rank],startBuffer,size); |
---|
247 | |
---|
248 | buffer.advance(size); |
---|
249 | count=buffer.remain(); |
---|
250 | } |
---|
251 | |
---|
252 | if (timeLine>0) lastTimeLine[rank]=timeLine ; |
---|
253 | |
---|
254 | CTimer::get("Process request").suspend(); |
---|
255 | } |
---|
256 | |
---|
257 | void CContextServer::processEvents(void) |
---|
258 | { |
---|
259 | map<size_t,CEventServer*>::iterator it; |
---|
260 | CEventServer* event; |
---|
261 | |
---|
262 | it=events.find(currentTimeLine); |
---|
263 | if (it!=events.end()) |
---|
264 | { |
---|
265 | event=it->second; |
---|
266 | |
---|
267 | if (event->isFull()) |
---|
268 | { |
---|
269 | if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side |
---|
270 | { |
---|
271 | CServer::eventScheduler->registerEvent(currentTimeLine,hashId); |
---|
272 | scheduled=true; |
---|
273 | } |
---|
274 | else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) ) |
---|
275 | { |
---|
276 | // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes |
---|
277 | // The best way to properly solve this problem will be to use the event scheduler also in attached mode |
---|
278 | // for now just set up a MPI barrier |
---|
279 | if (!CServer::eventScheduler && CXios::isServer) MPI_Barrier(intraComm) ; |
---|
280 | |
---|
281 | CTimer::get("Process events").resume(); |
---|
282 | dispatchEvent(*event); |
---|
283 | CTimer::get("Process events").suspend(); |
---|
284 | pendingEvent=false; |
---|
285 | delete event; |
---|
286 | events.erase(it); |
---|
287 | currentTimeLine++; |
---|
288 | scheduled = false; |
---|
289 | } |
---|
290 | } |
---|
291 | else getBufferFromClient(currentTimeLine) ; |
---|
292 | } |
---|
293 | else if (pureOneSided) getBufferFromClient(currentTimeLine) ; // if pure one sided check buffer even if no event recorded at current time line |
---|
294 | } |
---|
295 | |
---|
296 | CContextServer::~CContextServer() |
---|
297 | { |
---|
298 | map<int,CServerBuffer*>::iterator it; |
---|
299 | for(it=buffers.begin();it!=buffers.end();++it) delete it->second; |
---|
300 | } |
---|
301 | |
---|
302 | void CContextServer::releaseBuffers() |
---|
303 | { |
---|
304 | map<int,CServerBuffer*>::iterator it; |
---|
305 | bool out ; |
---|
306 | do |
---|
307 | { |
---|
308 | out=true ; |
---|
309 | for(it=buffers.begin();it!=buffers.end();++it) |
---|
310 | { |
---|
311 | out = out && it->second->freeWindows() ; |
---|
312 | |
---|
313 | } |
---|
314 | } while (! out) ; |
---|
315 | } |
---|
316 | |
---|
317 | void CContextServer::dispatchEvent(CEventServer& event) |
---|
318 | { |
---|
319 | string contextName; |
---|
320 | string buff; |
---|
321 | int MsgSize; |
---|
322 | int rank; |
---|
323 | list<CEventServer::SSubEvent>::iterator it; |
---|
324 | StdString ctxId = context->getId(); |
---|
325 | CContext::setCurrent(ctxId); |
---|
326 | StdSize totalBuf = 0; |
---|
327 | |
---|
328 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
329 | { |
---|
330 | finished=true; |
---|
331 | info(20)<<" CContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl; |
---|
332 | releaseBuffers() ; |
---|
333 | context->finalize(); |
---|
334 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
335 | iteMap = mapBufferSize_.end(), itMap; |
---|
336 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
337 | { |
---|
338 | rank = itMap->first; |
---|
339 | report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
340 | << " +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl; |
---|
341 | totalBuf += itMap->second; |
---|
342 | } |
---|
343 | report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
344 | } |
---|
345 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
346 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
347 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
348 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
349 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
350 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
351 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
352 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
353 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
354 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
355 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
356 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event); |
---|
357 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
358 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
359 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
360 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
361 | else |
---|
362 | { |
---|
363 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
364 | } |
---|
365 | } |
---|
366 | } |
---|