Debian Codegraph @

src/scheduler_agent.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2012, 2022 INCF
4 *
5 * MUSIC is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MUSIC is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <music/scheduler_agent.hh>
20
21 //#define MUSIC_DEBUG
22 #include "music/debug.hh"
23
24 #if MUSIC_USE_MPI
25
26 #include <algorithm>
27 #include <iostream>
28 #include <cassert>
29 #ifdef MUSIC_DEBUG
30 #include <cstdlib>
31 #endif
32
33 namespace MUSIC
34 {
35 SchedulerAgent::SchedulerAgent (Scheduler *scheduler) :
36 scheduler_ (scheduler)
37 {
38
39 }
40
41
42 MulticommAgent::MulticommAgent (Scheduler *scheduler) :
43 SchedulerAgent (scheduler)
44 {
45 multiProxies = NULL;
46 multiBuffer_ = NULL;
47 filter1 = NULL;
48 filter2 = NULL;
49 }
50
51
52 MulticommAgent::Filter1::Filter1 (MulticommAgent &multCommObj) :
53 multCommObj_ (multCommObj)
54 {
55
56 }
57
58
59 /* remedius
60 * This filter is applied to the set of selected connectors in the fillSchedule().
61 * It partitioned the set of connectors into two groups:
62 * returns True: connectors that can be lumped:if scheduled send times <= multCommObj_.time_.
63 * This condition is sufficient because each conflicting send time can be postponed to the most latter scheduled for the same node.
64 * The most latter possible time is a multCommObj_.time_ (the receive time).
65 * returns False: connectors that go to the Filter2 (connectors that possibly can be added to the current group)
66 */
67 bool
68 MulticommAgent::Filter1::operator() (SConnectionP &conn)
69 {
70 if (conn.second.sSend <= multCommObj_.time_)
71 {
72 int sId = conn.first.pre ().data ().color;
73 int rId = conn.first.post ().data ().color;
74 //keeps track of all receive nodes
75 multCommObj_.rNodes |= 1 << rId;
76 //guarantees that if there are both send and receive are scheduled to the same node,
77 // the receive time will be chosen.
78 multCommObj_.commTimes[rId] = multCommObj_.time_;
79 // take the most later time
80 if (conn.second.sSend >= multCommObj_.commTimes[sId])
81 multCommObj_.commTimes[sId] = conn.second.sSend;
82 return true;
83 }
84 return false;
85 }
86
87
88 MulticommAgent::Filter2::Filter2 (MulticommAgent &multCommObj) :
89 multCommObj_ (multCommObj)
90 {
91
92 }
93
94
95 /* remedius
96 * This filter is applied to the rest of the selected connectors, that were classified as False by the Filter1.
97 * It adds those of the rest connectors(which scheduled send times > multCommObj_.time_.) that either don't have a conflict
98 * with the already selected connectors by Filter1 or if the conflict is solvable.
99 * The conflict is not solvable if both send and receive are scheduled for the same node.
100 */
101 bool
102 MulticommAgent::Filter2::operator() (SConnectionP &conn)
103 {
104 int sId = conn.first.pre ().data ().color;
105 int rId = conn.first.post ().data ().color;
106 std::map<int, Clock>::iterator it;
107 it = multCommObj_.commTimes.find (sId);
108 if (it == multCommObj_.commTimes.end ()
109 || ! (multCommObj_.rNodes & (1 << sId)))
110 {
111 multCommObj_.commTimes[sId] = conn.second.sSend;
112 multCommObj_.commTimes[rId] = multCommObj_.time_;
113 multCommObj_.rNodes |= 1 << rId;
114 return true;
115 }
116 return false;
117 }
118
119
120 MulticommAgent::~MulticommAgent ()
121 {
122 for (std::vector<MultiConnector*>::iterator connector =
123 multiConnectors.begin (); connector != multiConnectors.end ();
124 ++connector)
125 if (*connector != NULL)
126 delete *connector;
127 if (multiProxies != NULL)
128 delete multiProxies;
129 if (multiBuffer_ != NULL)
130 delete multiBuffer_;
131 if (filter1 != NULL)
132 delete filter1;
133 if (filter2 != NULL)
134 delete filter2;
135 }
136
137
138
139 void
140 MulticommAgent::initialize (std::vector<Connector*>& connectors)
141 {
142 filter1 = new Filter1 (*this);
143 filter2 = new Filter2 (*this);
144 multiBuffer_ = new MultiBuffer (scheduler_->comm(), scheduler_->getLeader(), connectors);
145 //contains the multiconnectors which were created (I'm included)
146 multiConnectors.resize (Connector::idRange ());
147 //denotes multiIds which were created (I'm not included)
148 multiProxies = new std::vector<bool>;
149 multiProxies->resize (Connector::proxyIdRange ());
150 for (unsigned int i = 0; i < Connector::idRange (); ++i)
151 multiConnectors[i] = NULL;
152 for (unsigned int i = 0; i < Connector::proxyIdRange (); ++i)
153 (*multiProxies)[i] = false;
154
155 }
156
157
158 #if 0
159 void
160 MulticommAgent::createMultiConnectors (Clock& localTime, MPI_Comm comm,
161 int leader, std::vector<Connector*>& connectors)
162 {
163 multiBuffer_ = new MultiBuffer (comm, leader, connectors);
164 multiConnectors.resize (Connector::idRange ());
165 multiProxies = new std::vector<bool>;
166 multiProxies->resize (Connector::proxyIdRange ());
167
168 for (unsigned int self_node = 0; self_node < scheduler_->nApplications ();
169 ++self_node)
170 {
171
172 scheduler_->reset (self_node);
173
174 if (!create (localTime))
175 scheduler_->pushForward ();
176
177 localTime.ticks (-1);
178
179 for (int i = 0; i < N_PLANNING_CYCLES; ++i)
180 {
181 localTime.tick ();
182 if (!create (localTime))
183 scheduler_->pushForward ();
184
185 }
186
187 //finalize
188 schedule.clear ();
189
190 localTime.reset ();
191 }
192
193 scheduler_->reset (-1);
194 delete multiProxies;
195 }
196
197 #endif
198
199 std::vector<Connector*>
200 MulticommAgent::connectorsFromMultiId (unsigned int multiId)
201 {
202 std::vector<Connector*> connectors;
203 for (unsigned int flag = 1; multiId != 0; flag <<= 1)
204 if (multiId & flag)
205 {
206 connectors.push_back (Connector::connectorFromIdFlag (flag));
207 multiId &= ~flag;
208 }
209 return connectors;
210 }
211
212 #if 0
213 bool
214 MulticommAgent::create (MUSIC::Clock &localTime)
215 {
216 std::vector<MultiCommObject>::iterator comm;
217 bool continue_;
218 do
219 {
220 continue_ = fillSchedule ();
221 for (comm = schedule.begin ();
222 comm != schedule.end () && (*comm).time <= localTime;
223 ++comm)
224 {
225 unsigned int multiId = (*comm).multiId ();
226 if (multiId != 0)
227 {
228 if (multiConnectors[multiId] == NULL)
229 {
230 std::vector<Connector*> connectors = connectorsFromMultiId (
231 multiId);
232 multiConnectors[multiId] = new MultiConnector (multiBuffer_,
233 connectors);
234 }
235 }
236 else
237 {
238 unsigned int proxyId = (*comm).proxyId ();
239 if (proxyId != 0 && ! (*multiProxies)[proxyId])
240 {
241 // std::cout << "Rank " << mpi_get_rank (MPI_COMM_WORLD)
242 // << ": Proxy " << proxyId << std::endl;
243 MPI_Comm newcomm;
244 MPI_Comm_create (MPI_COMM_WORLD, MPI_GROUP_EMPTY, &newcomm);
245 MPI_Barrier (MPI_COMM_WORLD);
246 (*multiProxies)[proxyId] = true;
247 }
248 }
249 }
250 schedule.erase (schedule.begin (), comm);
251 }
252 while (continue_ && schedule.empty ());
253 return !schedule.empty ();
254 }
255
256 #endif
257
258 bool
259 MulticommAgent::tick (MUSIC::Clock &localTime)
260 {
261 std::vector<MultiCommObject>::iterator comm;
262 bool continue_;
263 do
264 {
265 continue_ = fillSchedule ();
266 for (comm = schedule.begin ();
267 comm != schedule.end () && (*comm).time <= localTime; ++comm)
268 {
269 unsigned int multiId = (*comm).multiId ();
270 unsigned int proxyId = (*comm).proxyId ();
271 // if we participate in the multicommunication but the multiconnector was not yet created
272 if (multiId != 0 && multiConnectors[multiId] == NULL)
273 {
274 std::vector<Connector*> connectors = connectorsFromMultiId (
275 multiId);
276 multiConnectors[multiId] = new MultiConnector (multiBuffer_,
277 connectors);
278
279 }// if we do not participate in the multicommunication and the multiconnector was not yet created
280 else if (multiId == 0 && !((*multiProxies)[proxyId]))
281 {
282 MPI_Comm newcomm;
283 MPI_Comm_create (MPI_COMM_WORLD, MPI_GROUP_EMPTY, &newcomm);
284 MPI_Barrier (MPI_COMM_WORLD);
285 (*multiProxies)[proxyId] = true;
286 }
287
288 if ( multiId != 0)
289 {
290 assert (multiConnectors[multiId] != NULL);
291 multiConnectors[multiId]->tick ();
292 }
293 }
294 schedule.erase (schedule.begin (), comm);
295 // we continue looping while:
296 // 1. we haven't found the communication from the future: !schedule.empty()
297 // 2. or the next SConnection can't be processed by the current agent: continue_= false
298 }
299 while (continue_ && schedule.empty ());
300 return !schedule.empty ();
301 }
302
303
304 // *schedule object can be either non empty or empty
305 // if it's empty it will be filled with collective connectors if appropriate
306 // if it's non empty, it contains collective connectors filled in before
307 // *last_sconn can be either p2p or collective connector
308 bool
309 MulticommAgent::fillSchedule ()
310 {
311 if (!schedule.empty ())
312 return true;
313 Scheduler::SConnection last_sconn = scheduler_->getLastSConnection ();
314 if (!last_sconn.data ().connector->idFlag ())
315 return false;
316 Clock nextReceive = last_sconn.data ().nextReceive; //last connection
317 SConnectionPV nextSConnections;
318 do
319 {
320 #if 0
321 if (last_sconn.data ().isLoopConnected
322 || scheduler_->isLocalConnection (last_sconn))
323 #endif
324 nextSConnections.push_back (
325 std::make_pair (last_sconn, last_sconn.data ()));
326
327 last_sconn = scheduler_->pushForward ();
328 }
329 while (last_sconn.data ().connector->idFlag () //those that request multicommunication
330 && (last_sconn.data ().nextReceive == nextReceive)); //those that have scheduled receive at current time
331 // || (!last_sconn.data ().isLoopConnected //
332 // && !scheduler_->isLocalConnection (last_sconn))));
333
334 if (nextSConnections.size () > 0)
335 NextMultiConnection (nextSConnections);
336
337 return last_sconn.data ().connector->idFlag ();
338 }
339
340
341 void
342 MulticommAgent::NextMultiConnection (SConnectionPV &candidates)
343 {
344 time_ = candidates[0].second.nextReceive;
345 // std::map<int, Clock> prevCommTime;
346 SConnectionPV::iterator iter_bound = candidates.begin ();
347 SConnectionPV::iterator cur_bound = candidates.begin ();
348 do
349 {
350 cur_bound = iter_bound;
351
352 commTimes.clear ();
353 rNodes = 0;
354
355 // after the first iteration inner partitioning (Filter1) will always return .begin()
356 iter_bound = std::stable_partition (
357 std::stable_partition (cur_bound, candidates.end (), *filter1),
358 candidates.end (), *filter2);
359
360 //postpone sends in the multiconn for consistency
361 for (SConnectionPV::iterator it = cur_bound; it != iter_bound; ++it)
362 (*it).second.postponeNextSend (
363 commTimes[ (*it).first.pre ().data ().color]);
364
365 std::map<int, Clock>::iterator id_iter;
366 if ( (id_iter = commTimes.find (scheduler_->self_node ()))
367 != commTimes.end ())
368 scheduleMulticonn ( (*id_iter).second, cur_bound, iter_bound);
369 }
370 while (iter_bound != candidates.end ());
371 }
372
373
374 void
375 MulticommAgent::scheduleMulticonn (Clock &time, SConnectionPV::iterator first,
376 SConnectionPV::iterator last)
377 {
378 unsigned int multiId = 0;
379 unsigned int proxyId = 0;
380 MUSIC_LOGR ("SelfNode: " << scheduler_->self_node() << " Time: "<< time.time() << std::endl << "MultiConnector:");
381 for (SConnectionPV::iterator it = first; it != last;
382 ++it)
383 {
384 if ( (*it).second.connector->isProxy ())
385 proxyId |= (*it).second.connector->idFlag ();
386 else
387 multiId |= (*it).second.connector->idFlag ();
388 MUSIC_LOGR ("("<< (*it).preNode ()->getId () <<" -> "<< (*it).postNode ()->getId () << ") at " << (*it).nextSend().time () << " -> "<< (*it).nextReceive ().time ());
389 }
390 schedule.push_back (MultiCommObject (time, multiId , proxyId));
391 }
392
393 #if 0
394 void
395 MulticommAgent::preFinalize (std::set<int> &cnn_ports)
396 {
397 for (std::vector<MultiConnector*>::iterator m = multiConnectors.begin ();
398 m != multiConnectors.end (); ++m)
399 if (*m != NULL)
400 cnn_ports.insert ( (*m)->connectorCode ());
401 }
402 #endif
403
404 void
405 MulticommAgent::finalize (std::set<int> &cnn_ports)
406 {
407 std::vector<MultiCommObject>::iterator comm;
408 bool continue_;
409 do
410 {
411 continue_ = fillSchedule ();
412 for (comm = schedule.begin ();
413 comm != schedule.end () && !cnn_ports.empty (); ++comm)
414 {
415 unsigned int multiId = (*comm).multiId ();
416 unsigned int proxyId = (*comm).proxyId ();
417 // if we participate in the multicommunication but the multiconnector was not yet created
418 if (multiId != 0 && multiConnectors[multiId] == NULL)
419 {
420 std::vector<Connector*> connectors = connectorsFromMultiId (
421 multiId);
422 multiConnectors[multiId] = new MultiConnector (multiBuffer_,
423 connectors);
424 } // if we do not participate in the multicommunication and the multiconnector was not yet created
425 else if (multiId == 0 && ! ( (*multiProxies)[proxyId]))
426 {
427 MPI_Comm newcomm;
428 MPI_Comm_create (MPI_COMM_WORLD, MPI_GROUP_EMPTY, &newcomm);
429 MPI_Barrier (MPI_COMM_WORLD);
430 (*multiProxies)[proxyId] = true;
431
432 }
433 if (multiId != 0)
434 {
435 MultiConnector* m = multiConnectors[multiId];
436 #if 0
437 if (cnn_ports.find (m->connectorCode ()) == cnn_ports.end ())
438 continue;
439 #endif
440 if (m->isFinalized ())
441 // an output port was finalized in tick ()
442 {
443 #if 0
444 cnn_ports.erase (m->connectorCode ());
445 #endif
446 std::vector<Connector*> conns = connectorsFromMultiId (multiId);
447 std::vector<Connector *>::iterator it;
448 for(it = conns.begin(); it != conns.end(); ++it)
449 cnn_ports.erase ((*it)->receiverPortCode());
450 continue;
451 #if 0
452 // Sometimes the scheduler fails to generate all
453 // multiConnectors during finalization. Here, we
454 // weed out multiConnectors that were finalized as
455 // a consequence of the finalization of m.
456 for (std::vector<MultiConnector*>::iterator m =
457 multiConnectors.begin (); m != multiConnectors.end ();
458 ++m)
459 if (*m != NULL
460 && (cnn_ports.find ( (*m)->connectorCode ())
461 != cnn_ports.end ()) && (*m)->isFinalized ())
462 cnn_ports.erase ( (*m)->connectorCode ());
463 continue;
464 #endif
465 }
466 // finalize () needs to come after isFinalized check
467 // since it can itself set finalized state
468 m->finalize ();
469
470 m->tick ();
471 }
472 }
473 schedule.clear ();
474 }
475 while (continue_ && !cnn_ports.empty ());
476 }
477
478
479 UnicommAgent::UnicommAgent (Scheduler *scheduler) :
480 SchedulerAgent (scheduler)
481 {
482 }
483
484
485 // *schedule object can be either non empty or empty
486 // if it's empty it will be filled with p2p connector if appropriate
487 // if it's non empty, it contains p2p connectors filled in before
488 // *last_sconn can be either p2p or collective connector
489 bool
490 UnicommAgent::fillSchedule ()
491 {
492 if (!schedule.empty ())
493 return true;
494 Scheduler::SConnection last_sconn = scheduler_->getLastSConnection ();
495 if (last_sconn.data ().connector->idFlag ())
496 return false;
497 if (scheduler_->isLocalConnection (last_sconn))
498 {
499 Clock nextComm = (
500 scheduler_->self_node () == last_sconn.post ().data ().color ?
501 last_sconn.data ().nextReceive :
502 last_sconn.data ().nextSend);
503 schedule.time = nextComm;
504 schedule.connector = last_sconn.data ().connector;
505 MUSIC_LOGR (" :Scheduled communication:"<< last_sconn.preNode ()->getId () <<"->"
506 << last_sconn.postNode ()->getId ()<< "at("
507 << last_sconn.nextSend ().time () << ", "
508 << last_sconn.nextReceive ().time () <<")");
509
510 }
511 scheduler_->pushForward ();
512 return true;
513 }
514
515
516 bool
517 UnicommAgent::tick (Clock& localTime)
518 {
519 bool continue_;
520 do
521 {
522 continue_ = fillSchedule ();
523 if (!schedule.empty () && schedule.time <= localTime)
524 {
525 schedule.connector->tick ();
526 schedule.reset ();
527 }
528 }
529 while (continue_ && schedule.empty ());
530 return !schedule.empty ();
531 }
532
533
534 void
535 UnicommAgent::finalize (std::set<int> &cnn_ports)
536 {
537 bool continue_;
538 do
539 {
540 continue_ = fillSchedule ();
541 if (!schedule.empty ())
542 {
543 Connector* connector = schedule.connector;
544 schedule.reset ();
545 if ( (cnn_ports.find (connector->receiverPortCode ())
546 == cnn_ports.end ()))
547 continue;
548 if (connector->isFinalized ())
549 // an output port was finalized in tick ()
550 {
551 cnn_ports.erase (connector->receiverPortCode ());
552 continue;
553 }
554 // finalize () needs to come after isFinalized check
555 // since it can itself set finalized state
556 connector->finalize ();
557 }
558 }
559 while (continue_ && !cnn_ports.empty ());
560 }
561
562 }
563 #endif