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 | interComm=interComm_; |
---|
32 | int flag; |
---|
33 | MPI_Comm_test_inter(interComm,&flag); |
---|
34 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
35 | else MPI_Comm_size(interComm,&commSize); |
---|
36 | |
---|
37 | currentTimeLine=0; |
---|
38 | scheduled=false; |
---|
39 | finished=false; |
---|
40 | |
---|
41 | boost::hash<string> hashString; |
---|
42 | hashId=hashString(context->getId()); |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | // CContextServer::CContextServer(CContext* parent, int srvLvl, MPI_Comm intraComm_,MPI_Comm interComm_) |
---|
47 | // { |
---|
48 | // context=parent; |
---|
49 | // intraComm=intraComm_; |
---|
50 | // MPI_Comm_size(intraComm,&intraCommSize); |
---|
51 | // MPI_Comm_rank(intraComm,&intraCommRank); |
---|
52 | // interComm=interComm_; |
---|
53 | // int flag; |
---|
54 | // MPI_Comm_test_inter(interComm,&flag); |
---|
55 | // if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
56 | // else MPI_Comm_size(interComm,&commSize); |
---|
57 | // |
---|
58 | // currentTimeLine=0; |
---|
59 | // scheduled=false; |
---|
60 | // finished=false; |
---|
61 | // |
---|
62 | // boost::hash<string> hashString; |
---|
63 | // StdString contextId = context->getId(); |
---|
64 | // hashId=hashString(contextId); |
---|
65 | // |
---|
66 | // } |
---|
67 | void CContextServer::setPendingEvent(void) |
---|
68 | { |
---|
69 | pendingEvent=true; |
---|
70 | } |
---|
71 | |
---|
72 | bool CContextServer::hasPendingEvent(void) |
---|
73 | { |
---|
74 | return pendingEvent; |
---|
75 | } |
---|
76 | |
---|
77 | bool CContextServer::hasFinished(void) |
---|
78 | { |
---|
79 | return finished; |
---|
80 | } |
---|
81 | |
---|
82 | bool CContextServer::eventLoop(void) |
---|
83 | { |
---|
84 | listen(); |
---|
85 | checkPendingRequest(); |
---|
86 | processEvents(); |
---|
87 | return finished; |
---|
88 | } |
---|
89 | |
---|
90 | void CContextServer::listen(void) |
---|
91 | { |
---|
92 | int rank; |
---|
93 | int flag; |
---|
94 | int count; |
---|
95 | char * addr; |
---|
96 | MPI_Status status; |
---|
97 | map<int,CServerBuffer*>::iterator it; |
---|
98 | |
---|
99 | for(rank=0;rank<commSize;rank++) |
---|
100 | { |
---|
101 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
102 | { |
---|
103 | traceOff(); |
---|
104 | MPI_Iprobe(rank,20,interComm,&flag,&status); |
---|
105 | traceOn(); |
---|
106 | if (flag==true) |
---|
107 | { |
---|
108 | it=buffers.find(rank); |
---|
109 | if (it==buffers.end()) // Receive the buffer size and allocate the buffer |
---|
110 | { |
---|
111 | StdSize buffSize = 0; |
---|
112 | MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status); |
---|
113 | mapBufferSize_.insert(std::make_pair(rank, buffSize)); |
---|
114 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first; |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
119 | if (it->second->isBufferFree(count)) |
---|
120 | { |
---|
121 | addr=(char*)it->second->getBuffer(count); |
---|
122 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]); |
---|
123 | bufferRequest[rank]=addr; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | void CContextServer::checkPendingRequest(void) |
---|
132 | { |
---|
133 | map<int,MPI_Request>::iterator it; |
---|
134 | list<int> recvRequest; |
---|
135 | list<int>::iterator itRecv; |
---|
136 | int rank; |
---|
137 | int flag; |
---|
138 | int count; |
---|
139 | MPI_Status status; |
---|
140 | |
---|
141 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
142 | { |
---|
143 | rank=it->first; |
---|
144 | traceOff(); |
---|
145 | MPI_Test(& it->second, &flag, &status); |
---|
146 | traceOn(); |
---|
147 | if (flag==true) |
---|
148 | { |
---|
149 | recvRequest.push_back(rank); |
---|
150 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
151 | processRequest(rank,bufferRequest[rank],count); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
156 | { |
---|
157 | pendingRequest.erase(*itRecv); |
---|
158 | bufferRequest.erase(*itRecv); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
163 | { |
---|
164 | |
---|
165 | CBufferIn buffer(buff,count); |
---|
166 | char* startBuffer,endBuffer; |
---|
167 | int size, offset; |
---|
168 | size_t timeLine; |
---|
169 | map<size_t,CEventServer*>::iterator it; |
---|
170 | |
---|
171 | while(count>0) |
---|
172 | { |
---|
173 | char* startBuffer=(char*)buffer.ptr(); |
---|
174 | CBufferIn newBuffer(startBuffer,buffer.remain()); |
---|
175 | newBuffer>>size>>timeLine; |
---|
176 | |
---|
177 | it=events.find(timeLine); |
---|
178 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first; |
---|
179 | it->second->push(rank,buffers[rank],startBuffer,size); |
---|
180 | |
---|
181 | buffer.advance(size); |
---|
182 | count=buffer.remain(); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void CContextServer::processEvents(void) |
---|
187 | { |
---|
188 | map<size_t,CEventServer*>::iterator it; |
---|
189 | CEventServer* event; |
---|
190 | |
---|
191 | it=events.find(currentTimeLine); |
---|
192 | if (it!=events.end()) |
---|
193 | { |
---|
194 | event=it->second; |
---|
195 | |
---|
196 | if (event->isFull()) |
---|
197 | { |
---|
198 | if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side |
---|
199 | { |
---|
200 | CServer::eventScheduler->registerEvent(currentTimeLine,hashId); |
---|
201 | scheduled=true; |
---|
202 | } |
---|
203 | else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) ) |
---|
204 | { |
---|
205 | // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes |
---|
206 | // The best way to properly solve this problem will be to use the event scheduler also in attached mode |
---|
207 | // for now just set up a MPI barrier |
---|
208 | // if (!CServer::eventScheduler) MPI_Barrier(intraComm) ; |
---|
209 | |
---|
210 | CTimer::get("Process events").resume(); |
---|
211 | dispatchEvent(*event); |
---|
212 | CTimer::get("Process events").suspend(); |
---|
213 | pendingEvent=false; |
---|
214 | delete event; |
---|
215 | events.erase(it); |
---|
216 | currentTimeLine++; |
---|
217 | scheduled = false; |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | CContextServer::~CContextServer() |
---|
224 | { |
---|
225 | map<int,CServerBuffer*>::iterator it; |
---|
226 | for(it=buffers.begin();it!=buffers.end();++it) delete it->second; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | void CContextServer::dispatchEvent(CEventServer& event) |
---|
231 | { |
---|
232 | string contextName; |
---|
233 | string buff; |
---|
234 | int MsgSize; |
---|
235 | int rank; |
---|
236 | list<CEventServer::SSubEvent>::iterator it; |
---|
237 | CContext::setCurrent(context->getId()); |
---|
238 | |
---|
239 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
240 | { |
---|
241 | finished=true; |
---|
242 | info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl; |
---|
243 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
244 | iteMap = mapBufferSize_.end(), itMap; |
---|
245 | StdSize totalBuf = 0; |
---|
246 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
247 | { |
---|
248 | report(10)<< " Memory report : Context <"<<context->getId()<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
249 | << " +) With client of rank " << itMap->first << " : " << itMap->second << " bytes " << endl; |
---|
250 | totalBuf += itMap->second; |
---|
251 | } |
---|
252 | context->finalize(); |
---|
253 | report(0)<< " Memory report : Context <"<<context->getId()<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
254 | } |
---|
255 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
256 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
257 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
258 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
259 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
260 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
261 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
262 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
263 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
264 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
265 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
266 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event); |
---|
267 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
268 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
269 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
270 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
271 | else |
---|
272 | { |
---|
273 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|