Debian Codegraph @

src/scheduler.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 //#define MUSIC_DEBUG
20 #include "music/scheduler.hh"
21 #include "music/scheduler_agent.hh"
22
23 #include "music/debug.hh"
24
25 #if MUSIC_USE_MPI
26
27 #include <cmath>
28 #include <iostream>
29 #include <set>
30 #include <climits>
31
32 #ifdef MUSIC_DEBUG
33 #include <cstdlib>
34 #endif
35
36 namespace MUSIC
37 {
38
39 Scheduler::Scheduler (MPI_Comm comm, int leader) :
40 nodes (NULL), appl_data_ (NULL), conn_data_ (NULL)
41 {
42 comm_ = comm;
43 leader_ = leader;
44 }
45
46
47 Scheduler::~Scheduler ()
48 {
49 if (nodes != NULL)
50 delete nodes;
51 if (appl_data_ != NULL)
52 delete[] appl_data_;
53 if (conn_data_ != NULL)
54 delete[] conn_data_;
55 }
56
57
58 void
59 Scheduler::initialize (TemporalNegotiatorGraph *appl_graph,
60 std::vector<Connector*>& connectors)
61 {
62
63 nodes = new SGraph (appl_graph->local_node_color (), appl_graph->nNodes (),
64 appl_graph->getNConnections ());
65
66 setApplications (appl_graph);
67
68 setConnections (appl_graph, connectors);
69
70 color_ = appl_graph->local_node_color ();
71
72 iter_node = nodes->begin ();
73 iter_conn = 0;
74
75 pushForward ();
76
77 std::vector<SchedulerAgent *>::iterator agent;
78 for (agent = agents_.begin(); agent != agents_.end(); agent++)
79 (*agent)->initialize(connectors);
80
81 initializeAgentState ();
82
83 }
84
85
86 void
87 Scheduler::setAgent (SchedulerAgent* agent)
88 {
89 agents_.push_back (agent);
90 }
91
92
93 void
94 Scheduler::initializeAgentState ()
95 {
96 cur_agent_ = agents_.begin ();
97 }
98
99
100 AEdge<Scheduler::SApplData, Scheduler::SConnData>
101 Scheduler::pushForward ()
102 {
103 while (true)
104 {
105 #if 0
106 // std::vector<Node*>::iterator node;
107 for (; iter_node < nodes.size (); ++iter_node )
108 {
109 Node *node = nodes.at(iter_node);
110 if (iter_conn < 0 && node->nextReceive () > node->localTime ().time ())
111 node->advance ();
112
113 std::vector<SConnection*>* conns = node->outputConnections ();
114 // std::vector<SConnection*>::iterator conn;
115
116 for (++iter_conn;
117 iter_conn < static_cast<int> (conns->size ());
118 ++iter_conn)
119 {
120 SConnection *conn = conns->at(iter_conn);
121 //do we have data ready to be sent?
122 if (conn->nextSend () <= conn->preNode ()->localTime ()
123 && conn->nextReceive () == conn->postNode ()->localTime ())
124 {
125 SConnection sconn = *conn;
126 sconn.postponeNextSend( conn->preNode()->localTime());
127 conn->advance();
128 return sconn;
129 }
130 }
131 iter_conn = -1;
132 }
133 #endif
134 for (; iter_node < nodes->end (); iter_node++)
135 {
136 if (iter_conn == 0)
137 iter_conn = (*iter_node).begin_o ();
138 else
139 {
140 advanceConnection ( (**iter_conn).data ());
141 ++iter_conn;
142 }
143 for (; iter_conn < (*iter_node).end_o (); iter_conn++)
144 {
145
146 //do we have data ready to be sent?
147 if ( (*iter_conn)->data ().nextSend
148 <= (*iter_conn)->pre ().data ().localTime
149 && (*iter_conn)->data ().nextReceive
150 == (*iter_conn)->post ().data ().localTime)
151 {
152 last_sconn_ = * (*iter_conn);
153
154 last_sconn_.data ().postponeNextSend (
155 (*iter_conn)->pre ().data ().localTime);
156
157 return last_sconn_;
158 }
159 }
160
161 iter_conn = 0;
162 }
163
164
165 for (iter_node = nodes->begin (); iter_node < nodes->end ();
166 iter_node++)
167 {
168 Clock next_recv = nextApplicationReceive (*iter_node);
169 if ((*iter_node).data ().localTime < next_recv)
170 (*iter_node).data ().localTime.tick ();
171 }
172 iter_node = nodes->begin ();
173
174 }
175 }
176
177 #if 0
178 void
179 Scheduler::createMultiConnectors (Clock localTime,
180 std::vector<Connector*>& connectors,
181 MultiBuffer* multiBuffer,
182 std::vector<MultiConnector*>& multiConnectors)
183 {
184 //if (mpi_get_rank (MPI_COMM_WORLD) == 2)
185 // std::cout << "prep localTime = " << localTime.time () << std::endl;
186 // We need to create the MultiConnectors ahead of time since their
187 // creation requires communication between all members of
188 // COMM_WORLD. Temporary solution: Create all MultiConnectors
189 // that will be needed during the first 100 steps.
190 std::vector<std::pair<double, Connector *> > schedule;
191
192 std::vector<bool> multiProxies;
193 multiProxies.resize (Connector::proxyIdRange ());
194
195 for (int self_node = 0; self_node < (int) nodes.size (); ++self_node)
196 {
197 localTime.reset ();
198
199 createMultiConnNext (self_node,
200 localTime,
201 connectors,
202 multiBuffer,
203 schedule);
204
205 localTime.ticks (-1);
206
207 std::vector<Connector*> cCache;
208 for (int i = 0; i < 100; ++i)
209 {
210 localTime.tick ();
211 createMultiConnStep (self_node,
212 localTime,
213 connectors,
214 multiBuffer,
215 multiConnectors,
216 multiProxies,
217 cCache,
218 schedule,
219 false);
220 }
221 createMultiConnStep (self_node,
222 localTime,
223 connectors,
224 multiBuffer,
225 multiConnectors,
226 multiProxies,
227 cCache,
228 schedule,
229 true);
230 schedule.clear ();
231
232 // Now reset node and connection clocks to starting values
233 for (std::vector<Node*>::iterator node = nodes.begin ();
234 node != nodes.end ();
235 ++node)
236 (*node).localTime.reset ();
237
238 for (std::vector<SConnection*>::iterator conn = connections.begin ();
239 conn != connections.end ();
240 ++conn)
241 {
242 (*conn)->resetClocks ();
243 (*conn)->advance ();
244 }
245 }
246 }
247
248 void
249 Scheduler::createMultiConnNext (int self_node,
250 Clock& localTime,
251 std::vector<Connector*>& connectors,
252 MultiBuffer* multiBuffer_,
253 std::vector<std::pair<double, Connector *> > &schedule)
254 {
255 while (schedule.empty ()
256 // always plan forward past the first time point to make
257 // sure that all events at that time are scheduled
258 || schedule.front ().first == schedule.back ().first)
259 {
260 std::vector<Node*>::iterator node;
261 for ( node=nodes.begin (); node < nodes.end (); ++node )
262 {
263 if ((*node)->nextReceive () > (*node)->localTime ().time ())
264 (*node)->advance ();
265
266 std::vector<SConnection*>* conns = (*node)->outputConnections ();
267 std::vector<SConnection*>::iterator conn;
268 for (conn = conns->begin (); conn < conns->end (); ++conn)
269 //do we have data ready to be sent?
270 if ((*conn)->nextSend () <= (*conn)->preNode ()->localTime ()
271 && (*conn)->nextReceive () == (*conn)->postNode ()->localTime ())
272 {
273 if (self_node == (*conn)->postNode ()->getId () //input
274 || self_node == (*conn)->preNode ()->getId ())//output
275 {
276 double nextComm
277 = (self_node == (*conn)->postNode ()->getId ()
278 ? (*conn)->postNode ()->localTime ().time ()
279 : (*conn)->preNode ()->localTime ().time ());
280 schedule.push_back
281 (std::pair<double, Connector*>(nextComm,
282 (*conn)->getConnector ()));
283 }
284
285 MUSIC_LOG0 ("Scheduled communication:"<< (*conn)->preNode ()->getId () <<"->"<< (*conn)->postNode ()->getId () << "at(" << (*conn)->preNode ()->localTime ().time () << ", "<< (*conn)->postNode ()->localTime ().time () <<")");
286 (*conn)->advance ();
287 }
288 }
289 }
290 }
291
292 void
293 Scheduler::createMultiConnStep (int self_node,
294 Clock& localTime,
295 std::vector<Connector*>& connectors,
296 MultiBuffer* multiBuffer_,
297 std::vector<MultiConnector*>& multiConnectors,
298 std::vector<bool>& multiProxies,
299 std::vector<Connector*>& cCache,
300 std::vector<std::pair<double, Connector *> > &schedule,
301 bool finalize)
302 {
303 std::set<int>* connsToFinalize;
304 int finalCount = 0;
305
306 if (finalize)
307 {
308 connsToFinalize = new std::set<int>;
309 for (std::vector<Connector*>::iterator c = connectors.begin ();
310 c != connectors.end ();
311 ++c)
312 connsToFinalize->insert ((*c)->receiverPortCode ());
313 }
314
315 while (schedule[0].first <= localTime.time()
316 || (finalize && //!connsToFinalize->empty ()
317 ++finalCount <= 4000))
318 {
319 std::vector< std::pair<double, Connector*> >::iterator comm;
320 for (comm = schedule.begin ();
321 comm != schedule.end() && comm->first <= localTime.time ();
322 ++comm)
323 {
324 Connector* connector = comm->second;
325
326 if (connector == NULL)
327 continue;
328
329 if (finalize)
330 connsToFinalize->erase (connector->receiverPortCode ());
331
332 if (!connector->idFlag ())
333 continue;
334
335 unsigned int multiId = 0;
336 unsigned int multiProxyId = 0;
337 if (!connector->isProxy ())
338 {
339 multiId = connector->idFlag ();
340 cCache.push_back (connector);
341 }
342 else
343 {
344 dynamic_cast<ProxyConnector*> (connector)->setNode (self_node);
345 multiProxyId = connector->idFlag ();
346 }
347 bool isProxy = connector->isProxy ();
348 int remoteLeader = connector->remoteLeader ();
349 bool direction = connector->isInput ();
350 // yes, horrible quadratic algorithm for now
351 for (std::vector< std::pair<double, Connector*> >::iterator c
352 = comm + 1;
353 c != schedule.end () && c->first <= localTime.time();
354 ++c)
355 {
356 if (c->second == NULL || c->second->idFlag () == 0)
357 continue;
358 if (c->second->isProxy ())
359 dynamic_cast<ProxyConnector*> (c->second)->setNode (self_node);
360 // lumping criterion
361 if (!(c->second->isProxy () ^ isProxy)
362 && c->second->remoteLeader () == remoteLeader
363 && c->second->isInput () == direction)
364 {
365 if (finalize)
366 connsToFinalize->erase (c->second->receiverPortCode ());
367 if (!c->second->isProxy ())
368 {
369 cCache.push_back (c->second);
370 multiId |= c->second->idFlag ();
371 }
372 else
373 {
374 multiProxyId |= c->second->idFlag ();
375 }
376 c->second = NULL; // taken
377 }
378 }
379
380 //if mpi_get_rank ((MPI_COMM_WORLD) == 2)
381 // std::cout << "Prep multiId = " << multiId << std::endl;
382 if (cCache.size () == 0)
383 {
384 if (!multiProxies[multiProxyId])
385 {
386 std::cout << "Rank " << mpi_get_rank (MPI_COMM_WORLD)
387 << ": *" << std::endl << std::flush;
388 MPI_Comm comm;
389 MPI_Comm_create (MPI_COMM_WORLD, MPI_GROUP_EMPTY, &comm);
390 MPI_Barrier (MPI_COMM_WORLD);
391 //*fixme* Impossible to delete the following object
392 multiProxies[multiProxyId] = true;
393 }
394 }
395 else if (multiConnectors[multiId] == NULL)
396 {
397 multiConnectors[multiId]
398 = new MultiConnector (multiBuffer_, cCache);
399 }
400 cCache.clear ();
401 }
402 schedule.erase (schedule.begin (), comm);
403 createMultiConnNext (self_node,
404 localTime,
405 connectors,
406 multiBuffer_,
407 schedule);
408 }
409
410 if (finalize)
411 delete connsToFinalize;
412
413 }
414 #endif
415
416
417 void
418 Scheduler::reset (int color)
419 {
420 if (color < 0)
421 color = nodes->local_node_color ();
422 color_ = color;
423 reset ();
424 nodes->setTempColor (color);
425 std::vector<SConnection> path;
426 nodes->depthFirst (nodes->at (color), path);
427
428 cur_agent_ = agents_.begin ();
429 iter_node = nodes->begin ();
430 iter_conn = 0;
431 pushForward ();
432
433 }
434
435
436 void
437 Scheduler::reset ()
438 {
439 nodes->reset ();
440 for (SGraph::iterator n = nodes->begin (); n < nodes->end (); n++)
441 {
442 (*n).data ().localTime.reset ();
443 for (SNode::edge_iterator e = (*n).begin_o (); e < (*n).end_o (); e++)
444 {
445 (*e)->data ().reset ();
446 advanceConnection ( (**e).data ());
447 }
448 }
449 }
450
451
452 void
453 Scheduler::tick (Clock& localTime)
454 {
455 // tick is done when at least one of the agent will schedule the future communication
456 // next tick will start from the last successful agent
457 // this algorithm is based on temporally ordered SConnections
458 bool done = false;
459
460 for (; !done; cur_agent_++)
461 {
462 if (cur_agent_ == agents_.end ())
463 cur_agent_ = agents_.begin ();
464
465 if ( (*cur_agent_)->tick (localTime))
466 done = true;
467 }
468 }
469
470
471 void
472 Scheduler::finalize (Clock& localTime, std::vector<Connector*>& connectors)
473 {
474 /* remedius
475 * set of receiver port codes that still has to be finalized
476 */
477
478 std::set<int> cnn_ports;
479 for (std::vector<Connector*>::iterator c = connectors.begin ();
480 c != connectors.end (); ++c)
481 #if 0
482 if (! (*c)->needsMultiCommunication ())
483 #endif
484 {
485 cnn_ports.insert ( (*c)->receiverPortCode ());
486 }
487 #if 0
488 for (std::vector<SchedulerAgent*>::iterator a = agents_.begin ();
489 a != agents_.end (); ++a)
490 (*a)->preFinalize (cnn_ports);
491 #endif
492
493 for (; !cnn_ports.empty (); cur_agent_++)
494 {
495 if (cur_agent_ == agents_.end ())
496 cur_agent_ = agents_.begin ();
497 (*cur_agent_)->finalize (cnn_ports);
498 }
499 }
500
501
502 Clock
503 Scheduler::nextApplicationReceive (SNode &node)
504 {
505 SNode::edge_iterator in_conn;
506 Clock nextTime;
507 nextTime.set(ClockState(LLONG_MAX));
508 for (in_conn = node.begin_i (); in_conn < node.end_i (); ++in_conn)
509 {
510 SConnData &data = (*in_conn)->data ();
511 if (data.nextReceive < nextTime)
512 nextTime = data.nextReceive;
513 }
514 return nextTime;
515 }
516
517
518 void
519 Scheduler::advanceConnection (SConnData &data)
520 {
521 advanceConnectionClocks (data);
522 Clock r = data.nextReceive;
523 Clock s = data.nextSend;
524 advanceConnectionClocks (data);
525 while (data.nextReceive == r)
526 {
527 s = data.nextSend;
528 advanceConnectionClocks (data);
529 }
530 data.nextReceive = r;
531 data.nextSend = s;
532 MUSIC_LOG0 (pre_id << "->"<<post_id << "::"<<nextSend_.time () << "::" << nextReceive_.time ());
533 }
534
535
536 void
537 Scheduler::advanceConnectionClocks (SConnData &data)
538 {
539 ClockState limit = (data.nextSend.integerTime () + data.descr.accLatency
540 - data.nextReceive.tickInterval ());
541 while (data.nextReceive.integerTime () <= limit)
542 data.nextReceive.tick ();
543 data.nextSend.ticks (data.descr.maxBuffered + 1);
544 }
545
546
547 void
548 Scheduler::setApplications (TemporalNegotiatorGraph *appl_graph)
549 {
550 appl_data_ = new SApplData[appl_graph->nNodes ()];
551 int i = 0;
552 TemporalNegotiatorGraph::iterator it;
553 for (it = appl_graph->begin (); it < appl_graph->end (); ++it, ++i)
554 {
555 appl_data_[i].color = (*it).data ().color;
556 appl_data_[i].leader = (*it).data ().leader;
557 appl_data_[i].nProcs = (*it).data ().nProcs;
558 appl_data_[i].localTime.configure ( (*it).data ().timebase,
559 (*it).data ().tickInterval);
560 nodes->addNode (
561 SNode ( (*it).data ().nOutConnections, (*it).data ().nInConnections,
562 appl_data_[i]), (*it).data ().color);
563 }
564
565 }
566
567
568 void
569 Scheduler::setConnections (TemporalNegotiatorGraph *appl_graph,
570 std::vector<Connector*>& connectors)
571 {
572 conn_data_ = new SConnData[appl_graph->getNConnections ()];
573 int k = 0;
574 SGraph::iterator inode;
575 for (inode = nodes->begin (); inode < nodes->end (); inode++)
576 {
577 TemporalNegotiationData &data =
578 appl_graph->at ( (*inode).data ().color).data ();
579 for (int i = 0; i < data.nOutConnections; ++i)
580 {
581 Connector *connector;
582 bool foundLocalConnector = false;
583 ConnectionDescriptor &descr = data.connection[i];
584 SNode &pre = *inode;
585 SNode &post = nodes->at (descr.remoteNode);
586
587 for (std::vector<Connector*>::iterator c = connectors.begin ();
588 c != connectors.end (); ++c)
589 {
590 if (descr.receiverPort == (*c)->receiverPortCode ())
591 {
592 foundLocalConnector = true;
593 connector = *c;
594 (*c)->setInterpolate (descr.interpolate);
595 (*c)->setLatency (descr.accLatency);
596 (*c)->initialize ();
597 break;
598 }
599 }
600 #if 1
601 if (!foundLocalConnector)
602 {
603 if (descr.multiComm)
604 connector = new ProxyConnector (pre.data ().color,
605 pre.data ().leader, pre.data ().nProcs,
606 post.data ().color, post.data ().leader,
607 post.data ().nProcs);
608 else
609 connector = new ProxyConnector ();
610 }
611
612 #endif
613 conn_data_[k].descr = descr;
614 conn_data_[k].connector = connector;
615 conn_data_[k].nextSend.configure (pre.data ().localTime.timebase (),
616 pre.data ().localTime.tickInterval ());
617 conn_data_[k].nextReceive.configure (
618 post.data ().localTime.timebase (),
619 post.data ().localTime.tickInterval ());
620 SConnection &edge = nodes->addEdge (
621 SConnection (pre, post, conn_data_[k++]));
622
623 advanceConnection (edge.data ());
624
625 }
626 }
627
628 }
629
630
631 void
632 Scheduler::SGraph::handleLoop (SNode &x, std::vector<SConnection> & path)
633 {
634 if (&x == &nodes_[tmp_color_])
635 {
636 // Mark connections on path as loop connected to self_node
637 for (std::vector<SConnection>::iterator c = path.begin ();
638 c != path.end (); ++c)
639 (*c).data ().isLoopConnected = true;
640 }
641 }
642
643 }
644
645 #endif