1 | #include "event_scheduler.hpp" |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "mpi.hpp" |
---|
4 | #include "tracer.hpp" |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | |
---|
9 | |
---|
10 | CEventScheduler::CEventScheduler(const MPI_Comm& comm) |
---|
11 | { |
---|
12 | MPI_Comm_dup(comm, &communicator) ; |
---|
13 | MPI_Comm_size(communicator,&mpiSize) ; |
---|
14 | MPI_Comm_rank(communicator,&mpiRank); |
---|
15 | |
---|
16 | |
---|
17 | int maxChild=1 ; |
---|
18 | |
---|
19 | int m ; |
---|
20 | do |
---|
21 | { |
---|
22 | m=1 ; |
---|
23 | maxChild=maxChild+1 ; |
---|
24 | for(int i=0;i<maxChild;i++) m=m*maxChild ; |
---|
25 | } while(m<mpiSize) ; |
---|
26 | |
---|
27 | |
---|
28 | int maxLevel=0 ; |
---|
29 | for(int size=1; size<=mpiSize; size*=maxChild) maxLevel++ ; |
---|
30 | |
---|
31 | int begin, end, nb ; |
---|
32 | int pos, n ; |
---|
33 | |
---|
34 | parent=vector<int>(maxLevel+1) ; |
---|
35 | child=vector<vector<int> >(maxLevel+1,vector<int>(maxChild)) ; |
---|
36 | nbChild=vector<int> (maxLevel+1) ; |
---|
37 | |
---|
38 | level=0 ; |
---|
39 | begin=0 ; |
---|
40 | end=mpiSize-1 ; |
---|
41 | nb=end-begin+1 ; |
---|
42 | |
---|
43 | do |
---|
44 | { |
---|
45 | n=0 ; |
---|
46 | pos=begin ; |
---|
47 | nbChild[level]=0 ; |
---|
48 | parent[level+1]=begin ; |
---|
49 | for(int i=0;i<maxChild && i<nb ;i++) |
---|
50 | { |
---|
51 | if (i<nb%maxChild) n = nb/maxChild + 1 ; |
---|
52 | else n = nb/maxChild ; |
---|
53 | |
---|
54 | if (mpiRank>=pos && mpiRank<pos+n) |
---|
55 | { |
---|
56 | begin=pos ; |
---|
57 | end=pos+n-1 ; |
---|
58 | } |
---|
59 | child[level][i]=pos ; |
---|
60 | pos=pos+n ; |
---|
61 | nbChild[level]++ ; |
---|
62 | } |
---|
63 | nb=end-begin+1 ; |
---|
64 | level=level+1 ; |
---|
65 | } while (nb>1) ; |
---|
66 | |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | CEventScheduler::~CEventScheduler() |
---|
71 | { |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId) |
---|
76 | { |
---|
77 | registerEvent(timeLine, contextHashId, level) ; |
---|
78 | } |
---|
79 | |
---|
80 | void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId, const size_t lev) |
---|
81 | { |
---|
82 | |
---|
83 | traceOff() ; |
---|
84 | SPendingRequest* sentRequest=new SPendingRequest ; |
---|
85 | sentRequest->buffer[0]=timeLine ; |
---|
86 | sentRequest->buffer[1]=contextHashId ; |
---|
87 | sentRequest->buffer[2]=lev-1 ; |
---|
88 | |
---|
89 | pendingSentParentRequest.push(sentRequest) ; |
---|
90 | MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, parent[lev], 0, communicator, &sentRequest->request) ; |
---|
91 | traceOn() ; |
---|
92 | } |
---|
93 | |
---|
94 | bool CEventScheduler::queryEvent(const size_t timeLine, const size_t contextHashId) |
---|
95 | { |
---|
96 | |
---|
97 | if (! eventStack.empty() && eventStack.front().first==timeLine && eventStack.front().second==contextHashId) |
---|
98 | { |
---|
99 | eventStack.pop() ; |
---|
100 | return true ; |
---|
101 | } |
---|
102 | else return false ; |
---|
103 | } |
---|
104 | |
---|
105 | void CEventScheduler::checkEvent(void) |
---|
106 | { |
---|
107 | traceOff() ; |
---|
108 | checkChildRequest() ; |
---|
109 | checkParentRequest() ; |
---|
110 | traceOn() ; |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | void CEventScheduler::checkParentRequest(void) |
---|
115 | { |
---|
116 | int completed ; |
---|
117 | MPI_Status status ; |
---|
118 | int received ; |
---|
119 | SPendingRequest* recvRequest ; |
---|
120 | completed=true ; |
---|
121 | |
---|
122 | // check sent request to parent |
---|
123 | while (! pendingSentParentRequest.empty() && completed) |
---|
124 | { |
---|
125 | MPI_Test( & pendingSentParentRequest.front()->request, &completed, &status) ; |
---|
126 | if (completed) |
---|
127 | { |
---|
128 | delete pendingSentParentRequest.front() ; |
---|
129 | pendingSentParentRequest.pop() ; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | // probe if a message is coming from parent |
---|
134 | received=true ; |
---|
135 | while(received) |
---|
136 | { |
---|
137 | MPI_Iprobe(MPI_ANY_SOURCE,1,communicator,&received, &status) ; |
---|
138 | if (received) |
---|
139 | { |
---|
140 | recvRequest=new SPendingRequest ; |
---|
141 | MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, 1, communicator, &(recvRequest->request)) ; |
---|
142 | pendingRecvParentRequest.push(recvRequest) ; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | // check sent request from parent |
---|
147 | completed=true ; |
---|
148 | while (! pendingRecvParentRequest.empty() && completed) |
---|
149 | { |
---|
150 | recvRequest=pendingRecvParentRequest.front() ; |
---|
151 | MPI_Test( &(recvRequest->request), &completed, &status) ; |
---|
152 | if (completed) |
---|
153 | { |
---|
154 | size_t timeLine=recvRequest->buffer[0] ; |
---|
155 | size_t hashId=recvRequest->buffer[1] ; |
---|
156 | size_t lev=recvRequest->buffer[2] ; |
---|
157 | delete recvRequest ; |
---|
158 | pendingRecvParentRequest.pop() ; |
---|
159 | |
---|
160 | if (lev==level) eventStack.push(pair<size_t,size_t>(timeLine,hashId)) ; |
---|
161 | else bcastEvent(timeLine, hashId, lev) ; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | void CEventScheduler::checkChildRequest(void) |
---|
168 | { |
---|
169 | // function call only by parent mpi process |
---|
170 | |
---|
171 | MPI_Status status ; |
---|
172 | int received ; |
---|
173 | received=true ; |
---|
174 | SPendingRequest* recvRequest ; |
---|
175 | |
---|
176 | // check for posted requests and make the corresponding receive |
---|
177 | while(received) |
---|
178 | { |
---|
179 | MPI_Iprobe(MPI_ANY_SOURCE,0,communicator,&received, &status) ; |
---|
180 | if (received) |
---|
181 | { |
---|
182 | recvRequest=new SPendingRequest ; |
---|
183 | MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, 0, communicator, &recvRequest->request) ; |
---|
184 | pendingRecvChildRequest.push_back(recvRequest) ; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | // check if receive request is achieved |
---|
189 | |
---|
190 | for(list<SPendingRequest*>::iterator it=pendingRecvChildRequest.begin(); it!=pendingRecvChildRequest.end() ; ) |
---|
191 | { |
---|
192 | MPI_Test(&((*it)->request),&received,&status) ; |
---|
193 | if (received) |
---|
194 | { |
---|
195 | size_t timeLine=(*it)->buffer[0] ; |
---|
196 | size_t hashId=(*it)->buffer[1] ; |
---|
197 | size_t lev=(*it)->buffer[2] ; |
---|
198 | |
---|
199 | SEvent event={timeLine,hashId,lev} ; |
---|
200 | delete *it ; // free mem |
---|
201 | it=pendingRecvChildRequest.erase(it) ; // get out of the list |
---|
202 | |
---|
203 | map< SEvent,int>::iterator itEvent=recvEvent.find(event) ; |
---|
204 | if (itEvent==recvEvent.end()) |
---|
205 | { |
---|
206 | itEvent=(recvEvent.insert(pair< SEvent ,int > (event,1))).first ; |
---|
207 | |
---|
208 | } |
---|
209 | else (itEvent->second)++ ; |
---|
210 | if (itEvent->second==nbChild[lev]) |
---|
211 | { |
---|
212 | if (lev==0) |
---|
213 | { |
---|
214 | bcastEvent(timeLine,hashId,lev) ; |
---|
215 | recvEvent.erase(itEvent) ; |
---|
216 | } |
---|
217 | else |
---|
218 | { |
---|
219 | registerEvent( timeLine,hashId,lev) ; |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | else ++it ; |
---|
224 | } |
---|
225 | |
---|
226 | // check if bcast request is achieved |
---|
227 | |
---|
228 | for(list<SPendingRequest*>::iterator it=pendingSentChildRequest.begin(); it!=pendingSentChildRequest.end() ; ) |
---|
229 | { |
---|
230 | MPI_Test(&(*it)->request,&received,&status) ; |
---|
231 | if (received) |
---|
232 | { |
---|
233 | delete *it ; // free memory |
---|
234 | it = pendingSentChildRequest.erase(it) ; // get out of the list |
---|
235 | |
---|
236 | } |
---|
237 | else ++it ; |
---|
238 | |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | void CEventScheduler::bcastEvent(const size_t timeLine, const size_t contextHashId, const size_t lev) |
---|
243 | { |
---|
244 | SPendingRequest* sentRequest ; |
---|
245 | |
---|
246 | |
---|
247 | for(int i=0; i<nbChild[lev];i++) |
---|
248 | { |
---|
249 | sentRequest=new SPendingRequest ; |
---|
250 | sentRequest->buffer[0]=timeLine ; |
---|
251 | sentRequest->buffer[1]=contextHashId ; |
---|
252 | sentRequest->buffer[2]=lev+1 ; |
---|
253 | MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, child[lev][i], 1, communicator, & sentRequest->request) ; |
---|
254 | pendingSentChildRequest.push_back(sentRequest) ; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | } |
---|