Debian Codegraph @

src/connector.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009, 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/connector.hh"
20
21 #include "music/debug.hh"
22
23 #if MUSIC_USE_MPI
24 // connector.hh needs to be included first since it causes inclusion
25 // of mpi.h (in data_map.hh). mpi.h must be included before other
26 // header files on BG/L
27 #include "music/error.hh"
28 #include "music/communication.hh"
29 #include "music/event_router.hh"
30 #include <cmath>
31 #include <map>
32
33 namespace MUSIC {
34 std::map<unsigned int, Connector*> Connector::flagMap_;
35
36 unsigned int Connector::nextFlag_ = 1;
37
38 unsigned int Connector::nextProxyFlag_ = 1;
39
40 Connector::Connector (ConnectorInfo info_,
41 IndexMap* indices,
42 Index::Type type,
43 MPI_Comm c)
44 : info (info_),
45 indices_ (indices->copy()),
46 type_ (type),
47 comm (c),
48 idFlag_ (0),
49 finalized_ (false)
50 {
51 }
52
53
54 Connector::Connector (ConnectorInfo info_,
55 IndexMap* indices,
56 Index::Type type,
57 MPI_Comm c,
58 MPI_Comm ic)
59 : info (info_),
60 indices_ (indices->copy()),
61 type_ (type),
62 comm (c),
63 intercomm (ic),
64 idFlag_ (0),
65 finalized_ (false)
66 {
67 }
68
69
70 bool
71 Connector::isLeader ()
72 {
73 return mpi_get_rank (comm) == 0;
74 }
75
76
77 void
78 Connector::createIntercomm ()
79 {
80
81 MPI_Intercomm_create (comm,
82 0,
83 MPI_COMM_WORLD,
84 info.remoteLeader (),
85 CREATE_INTERCOMM_MSG,
86 &intercomm);
87 }
88
89
90 void
91 Connector::freeIntercomm ()
92 {
93 MPI_Comm_free (&intercomm);
94 }
95
96
97 void Connector::spatialNegotiation ()
98 {
99 SpatialNegotiator *spatialNegotiator_ = createSpatialNegotiator ();
100 width_ = spatialNegotiator_->getWidth ();
101 maxLocalWidth_ = spatialNegotiator_->maxLocalWidth ();
102 spatialNegotiation (spatialNegotiator_);
103 delete spatialNegotiator_;
104 delete indices_;
105 }
106
107
108 void Connector::spatialNegotiation (SpatialNegotiator * spatialNegotiator_)
109 {
110
111 if (spatialNegotiator_ == nullptr)
112 error ("spatialNegotiator was not initialized");
113 std::map<int, Subconnector*> subconnectors;
114 NegotiationIterator i
115 = spatialNegotiator_->negotiate (info.nProcesses (),
116 this); // only for debugging
117
118 for ( ;!i.end (); ++i)
119 {
120 std::map<int, Subconnector*>::iterator c
121 = subconnectors.find (i->rank ());
122 Subconnector* subconn;
123 if (c != subconnectors.end ())
124 subconn = c->second;
125 else
126 {
127 subconn = makeSubconnector (i->rank ());
128 if(subconn == nullptr)
129 error ("Subconnector was not initialized");
130 subconnectors.insert (std::make_pair (i->rank (), subconn));
131 rsubconn.push_back (subconn);
132
133 }
134 MUSIC_LOG (mpi_get_rank (MPI_COMM_WORLD)
135 << ": ("
136 << i->begin () << ", "
137 << i->end () << ", "
138 << i->local () << ", "
139 << i->displ() << ") -> " << i->rank ());
140 addRoutingInterval (i->interval (), subconn);
141
142
143 }
144
145 }
146
147
148 // The following ordering of subconnectors leads to a reasonably
149 // efficient communication schedule where the number of
150 // communications that weill have to happen sequentially is O(P)
151 // where P is the number of processors.
152 //
153 // Mikael Djurfeldt et al. (2005) "Massively parallel simulation
154 // of brain-scale neuronal network models." Tech. Rep.
155 // TRITA-NA-P0513, CSC, KTH, Stockholm.
156
157 bool
158 lessInputSubconnector (const Subconnector* c1, const Subconnector* c2)
159 {
160 return (c1->remoteRank () < c2->remoteRank ()
161 || (c1->remoteRank () == c2->remoteRank ()
162 && c1->receiverPortCode () < c2->receiverPortCode ()));
163 }
164
165
166 bool
167 lessOutputSubconnector (const Subconnector* c1, const Subconnector* c2)
168 {
169 if ((c1->remoteRank () > c1->localRank ()
170 && c2->remoteRank () <= c1->localRank ())
171 || (c1->remoteRank () <= c1->localRank ()
172 && c2->remoteRank () > c1->localRank ()))
173 return c1->remoteRank () > c2->remoteRank ();
174 else
175 return lessInputSubconnector (c1, c2);
176 }
177
178
179 void Connector::initialize ()
180 {
181 sort (rsubconn.begin (),
182 rsubconn.end (),
183 isInput () ? lessInputSubconnector : lessOutputSubconnector);
184 }
185
186
187 void
188 Connector::finalize ()
189 {
190 bool dataStillFlowing = false;
191 for (std::vector<Subconnector*>::iterator s = rsubconn.begin (); s != rsubconn.end (); ++s)
192 (*s)->flush(dataStillFlowing);
193 finalized_ = !dataStillFlowing;
194 }
195
196
197 void Connector::tick ()
198 {
199 for (std::vector<Subconnector*>::iterator s = rsubconn.begin (); s != rsubconn.end ();
200 ++s){
201 (*s)->maybeCommunicate();
202 }
203 }
204
205
206 SpatialNegotiator *
207 OutputConnector::createSpatialNegotiator ()
208 {
209 return new SpatialOutputNegotiator(indices_, type_, comm, intercomm );
210 }
211
212 #if MUSIC_ISENDWAITALL
213 void
214 OutputConnector::tick ()
215 {
216 std::vector<MPI_Request> requests;
217 for (std::vector<Subconnector*>::iterator s = rsubconn.begin ();
218 s != rsubconn.end ();
219 ++s)
220 {
221 (*s)->maybeCommunicate (requests);
222 }
223 //The spec guarantees vectors store their elements contiguously:
224 MPI_Waitall(requests.size(), (MPI_Request *)&requests[0], MPI_STATUSES_IGNORE);
225 }
226 #endif //MUSIC_ISENDWAITALL
227
228 SpatialNegotiator *
229 InputConnector::createSpatialNegotiator ()
230 {
231 return new SpatialInputNegotiator(indices_, type_, comm, intercomm);
232 }
233
234
235 /********************************************************************
236 *
237 * Cont Connectors
238 *
239 ********************************************************************/
240
241 void
242 ContConnector::initialize ()
243 {
244 Connector::initialize();
245 initialCommunication();
246 }
247
248
249 ClockState
250 ContConnector::remoteTickInterval (ClockState tickInterval)
251 {
252 ClockState::Serialized sRemoteTickInterval;
253 if (isLeader ())
254 {
255 // exchange tickInterval with peer leader
256 sRemoteTickInterval = tickInterval.serialize ();
257 MPI_Sendrecv_replace (&sRemoteTickInterval, 2, MPI_UNSIGNED_LONG,
258 0, TICKINTERVAL_MSG,
259 0, TICKINTERVAL_MSG,
260 intercomm,
261 MPI_STATUS_IGNORE);
262 }
263 // broadcast to peers
264 MPI_Bcast (&sRemoteTickInterval, 2, MPI_UNSIGNED_LONG, 0, comm);
265 return sRemoteTickInterval.deserialize ();
266 }
267
268
269 ContOutputConnector::ContOutputConnector (ConnectorInfo connInfo,
270 IndexMap* indices,
271 Index::Type type,
272 MPI_Comm comm,
273 Sampler& sampler,
274 MPI_Datatype data_type)
275 : Connector (connInfo, indices, type, comm),
276 ContConnector (sampler, data_type),
277 connector(nullptr)
278 {
279 }
280
281 ContOutputConnector::ContOutputConnector ( Sampler& sampler, MPI_Datatype type)
282 : ContConnector (sampler, type),
283 connector(nullptr)
284 {
285 }
286
287
288 ContOutputConnector::~ContOutputConnector()
289 {
290 if (connector != nullptr)
291 delete connector;
292 }
293
294
295 Subconnector*
296 ContOutputConnector::makeSubconnector (int remoteRank)
297 {
298 return new ContOutputSubconnector (//synchronizer (),
299 intercomm,
300 remoteLeader (),
301 remoteRank,
302 receiverPortCode (),
303 data_type_);
304 }
305
306
307 void
308 ContOutputConnector::specialize (Clock& localTime)
309 {
310 ClockState tickInterval = localTime.tickInterval ();
311 ClockState rTickInterval = remoteTickInterval (tickInterval);
312 localTime_ = &localTime;
313 remoteTime_.configure (localTime_->timebase (), rTickInterval);
314 if (tickInterval < rTickInterval)
315 connector = new InterpolatingContOutputConnector (this);
316 else
317 connector = new PlainContOutputConnector (this);
318 }
319
320
321 void
322 ContOutputConnector::addRoutingInterval (IndexInterval i,
323 Subconnector* subconn)
324 {
325 OutputSubconnector*osubconn= dynamic_cast<OutputSubconnector*>(subconn);
326 distributor_.addRoutingInterval (i, osubconn->outputBuffer());
327 }
328
329
330 void ContOutputConnector::initialCommunication()
331 {
332 for (std::vector<Subconnector*>::iterator s = rsubconn.begin (); s != rsubconn.end (); ++s)
333 (*s)->initialCommunication (0.0);
334 }
335
336
337 void
338 PlainContOutputConnector::initialize ()
339 {
340 distributor_.configure (sampler_.dataMap ());
341 distributor_.initialize ();
342 // synch.initialize ();
343
344 // put one element in send buffers
345 distributor_.distribute ();
346 }
347
348
349 void
350 PlainContOutputConnector::preCommunication ()
351 {
352 if (sample ())
353 {
354 // copy application data to send buffers
355 distributor_.distribute ();
356
357 }
358 }
359
360
361 // Start sampling (and fill the output buffers) at a time dependent
362 // on latency and receiver's tick interval. A negative latency can
363 // delay start of sampling beyond time 0. The tickInterval together
364 // with the strict comparison has the purpose of supplying an
365 // interpolating receiver side with samples.
366 bool
367 PlainContOutputConnector::sample ()
368 {
369 return (localTime_->integerTime () + latency + remoteTime_.tickInterval ()
370 > 0);
371 }
372
373
374 void
375 InterpolatingContOutputConnector::initialize ()
376 {
377 // Set the remoteTime which is used to control sampling and
378 // interpolation.
379 //
380 // For positive latencies, the integer part (in terms of receiver
381 // ticks) of the latency is handled by filling up the receiver
382 // buffers using InputSynchronizer::initialBufferedTicks ().
383 // remoteTime then holds the fractional part.
384 if (latency > 0)
385 {
386 ClockState startTime = - latency % remoteTime_.tickInterval ();
387 if (latency >= localTime_->tickInterval ())
388 startTime = startTime + remoteTime_.tickInterval ();
389 remoteTime_.set (startTime);
390 }
391 else
392 remoteTime_.set (- latency);
393 distributor_.configure (sampler_.interpolationDataMap ());
394 distributor_.initialize ();
395 // synch.initialize ();
396
397 // put one element in send buffers
398 sampler_.sample ();
399 sampler_.interpolate (1.0);
400 distributor_.distribute ();
401 }
402
403
404 // After the last sample at 1 localTime will be between remoteTime
405 // and remoteTime + localTime->tickInterval. We trigger on this
406 // situation and forward remoteTime.
407 bool
408 InterpolatingContOutputConnector::sample()
409 {
410 ClockState sampleWindowLow
411 = remoteTime_.integerTime () - localTime_->tickInterval ();
412 ClockState sampleWindowHigh
413 = remoteTime_.integerTime () + localTime_->tickInterval ();
414 return (sampleWindowLow <= localTime_->integerTime ()
415 && localTime_->integerTime () < sampleWindowHigh);
416 }
417
418
419 bool
420 InterpolatingContOutputConnector::interpolate()
421 {
422 ClockState sampleWindowHigh
423 = remoteTime_.integerTime () + localTime_->tickInterval ();
424 return (remoteTime_.integerTime () <= localTime_->integerTime ()
425 && localTime_->integerTime () < sampleWindowHigh);
426 }
427
428
429 double
430 InterpolatingContOutputConnector::interpolationCoefficient()
431 {
432 ClockState prevSampleTime
433 = localTime_->integerTime () - localTime_->tickInterval ();
434 double c = ((double) (remoteTime_.integerTime () - prevSampleTime)
435 / (double) localTime_->tickInterval ());
436
437 MUSIC_LOGR ("interpolationCoefficient = " << c);
438 // NOTE: preliminary implementation which just provides
439 // the functionality specified in the API
440 if (interp)
441 return c;
442 else
443 return round (c);
444 }
445
446
447 void
448 InterpolatingContOutputConnector::preCommunication ()
449 {
450 if (sample ()){
451 // sampling before and after time of receiver tick
452 sampler_.sampleOnce ();
453 }
454 if (interpolate ())
455 {
456 sampler_.interpolate (interpolationCoefficient ());
457 remoteTime_.tick();
458 distributor_.distribute ();
459 }
460 }
461
462
463 ContInputConnector::ContInputConnector (ConnectorInfo connInfo,
464 IndexMap* indices,
465 Index::Type type,
466 MPI_Comm comm,
467 Sampler& sampler,
468 MPI_Datatype data_type,
469 double delay)
470 : Connector (connInfo, indices, type, comm),
471 ContConnector (sampler, data_type),
472 delay_ (delay),
473 connector(nullptr)
474 {
475 }
476
477
478 ContInputConnector::ContInputConnector ( Sampler& sampler, MPI_Datatype type, double delay)
479 : ContConnector (sampler, type),
480 delay_ (delay),
481 connector(nullptr)
482 {
483 }
484
485
486 ContInputConnector::~ContInputConnector()
487 {
488 if (connector != nullptr)
489 delete connector;
490 }
491
492
493 Subconnector*
494 ContInputConnector::makeSubconnector (int remoteRank)
495 {
496 int receiverRank = mpi_get_rank (intercomm);
497 return new ContInputSubconnector (//synchronizer (),
498 intercomm,
499 remoteLeader (),
500 remoteRank,
501 receiverRank,
502 receiverPortCode (),
503 data_type_);
504 }
505
506
507 bool
508 ContInputConnector::divisibleDelay (Clock& localTime)
509 {
510 ClockState delay (delay_, localTime.timebase ());
511 return (delay % localTime.tickInterval ()) == 0;
512 }
513
514
515 void
516 ContInputConnector::specialize (Clock& localTime)
517 {
518 ClockState tickInterval = localTime.tickInterval ();
519 ClockState rTickInterval = remoteTickInterval (tickInterval);
520 localTime_ = &localTime;
521 remoteTime_.configure (localTime_->timebase (), rTickInterval);
522
523 if (tickInterval < rTickInterval
524 || (tickInterval == rTickInterval
525 && !divisibleDelay (localTime)))
526 connector = new InterpolatingContInputConnector(this);
527 else
528 connector = new PlainContInputConnector (this);
529 }
530
531
532 void
533 ContInputConnector::initialCommunication()
534 {
535 double initialBTicks = initialBufferedTicks();
536 for (std::vector<Subconnector*>::iterator s = rsubconn.begin (); s != rsubconn.end (); ++s)
537 (*s)->initialCommunication (initialBTicks);
538 }
539
540
541 // Return the number of copies of the data sampled by the sender
542 // Runtime constructor which should be stored in the receiver
543 // buffers at the first tick () (which occurs at the end of the
544 // Runtime constructor)
545 int
546 ContInputConnector::initialBufferedTicks ()
547 {
548 if (remoteTime_.tickInterval () < localTime_->tickInterval ())
549 {
550 // InterpolatingOutputConnector - PlainInputConnector
551
552 if (delay_ <= 0)
553 return 0;
554 else
555 {
556 // Need to add a sample first when we pass the receiver
557 // tick (=> - 1). If we haven't passed, the interpolator
558 // could simply use an interpolation coefficient of 0.0.
559 // (But this will never happen since that case isn't
560 // handled by an InterpolatingOutputConnector.)
561 int ticks = (delay_ - 1) / localTime_->tickInterval ();
562
563 // Need to add a sample if we go outside of the sender
564 // interpolation window
565 if (delay_ >= remoteTime_.tickInterval ())
566 ticks += 1;
567
568 return ticks;
569 }
570 }
571 else
572 {
573 // PlainOutputConnector - InterpolatingInputConnector
574
575 if (delay_ <= 0)
576 return 0;
577 else
578 // Need to add a sample first when we pass the receiver
579 // tick (=> - 1). If we haven't passed, the interpolator
580 // can simply use an interpolation coefficient of 0.0.
581 return 1 + (delay_ - 1) / localTime_->tickInterval ();
582 }
583 }
584
585
586 void
587 ContInputConnector::addRoutingInterval (IndexInterval i,
588 Subconnector* subconn)
589 {
590 InputSubconnector*isubconn= dynamic_cast<InputSubconnector*>(subconn);
591 collector_.addRoutingInterval (i, isubconn->inputBuffer());
592 }
593
594
595 void
596 PlainContInputConnector::initialize ()
597 {
598 // collector_.configure (sampler_.dataMap (), synch.allowedBuffered () + 1);
599 collector_.configure (sampler_.dataMap (), CONT_BUFFER_MAX);
600 collector_.initialize ();
601 }
602
603
604 void
605 PlainContInputConnector::postCommunication ()
606 {
607 // collect data from input buffers and write to application
608 collector_.collect ();
609 }
610
611
612 void
613 InterpolatingContInputConnector::initialize ()
614 {
615 if (latency > 0)
616 remoteTime_.set (latency % remoteTime_.tickInterval ()
617 - 2 * remoteTime_.tickInterval ());
618 else
619 remoteTime_.set (latency % remoteTime_.tickInterval ()
620 - remoteTime_.tickInterval ());
621 // collector_.configure (sampler_.interpolationDataMap (),
622 // synch.allowedBuffered () + 1);
623
624 collector_.configure (sampler_.interpolationDataMap (), CONT_BUFFER_MAX);
625 collector_.initialize ();
626
627
628 // synch.initialize ();
629 }
630
631
632 bool
633 InterpolatingContInputConnector::sample ()
634 {
635 return localTime_->integerTime () > remoteTime_.integerTime ();
636 }
637
638
639 double
640 InterpolatingContInputConnector::interpolationCoefficient ()
641 {
642 ClockState prevSampleTime
643 = remoteTime_.integerTime () - remoteTime_.tickInterval ();
644 double c = ((double) (localTime_->integerTime () - prevSampleTime)
645 / (double) remoteTime_.tickInterval ());
646
647 MUSIC_LOGR ("interpolationCoefficient = " << c);
648 // NOTE: preliminary implementation which just provides
649 // the functionality specified in the API
650 if (interp)
651 return c;
652 else
653 return round (c);
654 }
655
656
657 void
658 InterpolatingContInputConnector::postCommunication ()
659 {
660 if (first_)
661 {
662 collector_.collect (sampler_.insert ());
663 remoteTime_.tick();
664 first_ = false;
665 }
666 else if (sample ())
667 {
668 collector_.collect (sampler_.insert ());
669 remoteTime_.tick();
670 }
671 sampler_.interpolateToApplication (interpolationCoefficient ());
672 }
673
674
675 /********************************************************************
676 *
677 * Event Connectors
678 *
679 ********************************************************************/
680
681
682 Subconnector*
683 EventOutputConnector::makeSubconnector (int remoteRank)
684 {
685 //////
686 /* int world_size = mpi_get_comm_size (MPI_COMM_WORLD);
687 if (mpi_get_rank (MPI_COMM_WORLD) < world_size/2)
688 std::cout << remoteRank << std::flush << std::endl;*/
689 return new EventOutputSubconnector (//&synch,
690 intercomm,
691 remoteLeader (),
692 remoteRank,
693 receiverPortCode ());
694 }
695
696
697 void
698 EventOutputConnector::addRoutingInterval (IndexInterval i,
699 Subconnector* subconn)
700 {
701 OutputSubconnector* osubconn= dynamic_cast<OutputSubconnector*>(subconn);
702 routingMap_-> insert (i, osubconn->outputBuffer());
703 }
704
705 Subconnector*
706 EventInputConnector::makeSubconnector (int remoteRank)
707 {
708 int receiverRank = mpi_get_rank (intercomm);
709 if (type_ == Index::GLOBAL){
710 EventInputSubconnectorGlobal *subcc =
711 new EventInputSubconnectorGlobal (//&synch,
712 intercomm,
713 remoteLeader (),
714 remoteRank,
715 receiverRank,
716 receiverPortCode (),
717 handleEvent_);
718 rRank2Subconnector[remoteRank] = subcc;
719 flushes++;
720 return subcc;
721 }
722 else
723 {
724 #if MUSIC_ANYSOURCE
725 error( "LOCAL Indices are not supported with MUSIC_ANYSOURCE");
726 #endif
727
728 return new EventInputSubconnectorLocal (//&synch,
729 intercomm,
730 remoteLeader (),
731 remoteRank,
732 receiverRank,
733 receiverPortCode (),
734 handleEvent_);
735 }
736 }
737
738
739
740 void
741 EventInputConnector::addRoutingInterval(IndexInterval i, Subconnector* subconn)
742 {
743 //routingMap_-> insert (i, handleEvent_.global());
744 }
745 #if MUSIC_ANYSOURCE
746 void
747 EventInputConnector::tick ()
748 {
749 //SPIKE_BUFFER_MAX size
750 //MPI_BYTE type
751 //SPIKE_MSG tag
752 int size = rsubconn.size();
753 char data[SPIKE_BUFFER_MAX];
754 MPI_Status status;
755 while(size > 0 && flushes > 0){
756 MPI_Recv (data,
757 SPIKE_BUFFER_MAX,
758 MPI_BYTE,
759 MPI_ANY_SOURCE,
760 SPIKE_MSG,
761 intercomm,
762 &status);
763
764 int msize;
765 MPI_Get_count (&status, MPI_BYTE, &msize);
766 if (rRank2Subconnector[status.MPI_SOURCE]->receive (data, msize))
767 flushes--;
768 if( msize < SPIKE_BUFFER_MAX)
769 size--;
770 }
771 }
772 #endif //MUSIC_ANYSOURCE
773
774
775 /********************************************************************
776 *
777 * Message Connectors
778 *
779 ********************************************************************/
780
781 MessageOutputConnector::MessageOutputConnector (ConnectorInfo connInfo,
782 IndexMap* indices,
783 Index::Type type,
784 MPI_Comm comm,
785 std::vector<FIBO*>& buffers)
786 : Connector (connInfo, indices, type, comm),
787 buffer (1),
788 bufferAdded (false),
789 buffers_ (buffers)
790 {
791 }
792
793
794 Subconnector*
795 MessageOutputConnector::makeSubconnector (int remoteRank)
796 {
797 return new MessageOutputSubconnector (//&synch,
798 intercomm,
799 remoteLeader (),
800 remoteRank,
801 receiverPortCode (),
802 &buffer);
803 }
804
805
806 void
807 MessageOutputConnector::addRoutingInterval (IndexInterval i,
808 Subconnector* subconn)
809 {
810 if (!bufferAdded)
811 {
812 buffers_.push_back (&buffer);
813 bufferAdded = true;
814 }
815 }
816
817
818
819
820
821 void
822 MessageOutputConnector::postCommunication ()
823 {
824 }
825
826
827 MessageInputConnector::MessageInputConnector (ConnectorInfo connInfo,
828 IndexMap* indices,
829 Index::Type type,
830 MessageHandler* handleMessage,
831 MPI_Comm comm)
832 : Connector (connInfo, indices, type, comm),
833 handleMessage_ (handleMessage)
834 {
835 }
836
837
838
839
840 Subconnector*
841 MessageInputConnector::makeSubconnector (int remoteRank)
842 {
843 int receiverRank = mpi_get_rank (intercomm);
844 return new MessageInputSubconnector (//&synch,
845 intercomm,
846 remoteLeader (),
847 remoteRank,
848 receiverRank,
849 receiverPortCode (),
850 handleMessage_);
851 }
852
853 /********************************************************************
854 *
855 * Collective Connector
856 *
857 ********************************************************************/
858
859 CollectiveConnector::CollectiveConnector (bool high)
860 : high_ (high), subconnector_ (nullptr)
861 {
862 //idFlag_ = makeFlag ();
863 }
864
865
866 void
867 CollectiveConnector::createIntercomm ()
868 {
869 Connector::createIntercomm ();
870 MPI_Intercomm_merge (intercomm, high_, &intracomm_);
871 }
872
873
874 void
875 CollectiveConnector::freeIntercomm ()
876 {
877 MPI_Comm_free (&intracomm_);
878 Connector::freeIntercomm ();
879 }
880
881
882 ContCollectiveConnector::ContCollectiveConnector (MPI_Datatype type,
883 bool high):
884 CollectiveConnector (high),
885 data_type (type)
886 {
887
888 }
889
890
891 Subconnector*
892 ContCollectiveConnector::makeSubconnector (void *param)
893 {
894 std::multimap< int, Interval> intrvs
895 = *static_cast< std::multimap< int, Interval>*> (param);
896 if (subconnector_ == nullptr)
897 {
898 subconnector_ = new ContCollectiveSubconnector (intrvs,
899 width (),
900 intracomm_,
901 data_type);
902 }
903 return subconnector_;
904 }
905
906
907 EventCollectiveConnector::EventCollectiveConnector (bool high)
908 : CollectiveConnector (high), router_ (nullptr)
909 {
910 idFlag_ = makeFlag (this);
911 }
912
913
914 Subconnector*
915 EventInputCollectiveConnector::makeSubconnector (void *param)
916 {
917 if (subconnector_ == nullptr)
918 subconnector_ = new EventInputCollectiveSubconnector (router_);
919 return subconnector_;
920 }
921
922
923 EventInputCollectiveConnector::EventInputCollectiveConnector
924 (ConnectorInfo connInfo,
925 IndexMap* indices,
926 Index::Type type,
927 MPI_Comm comm,
928 EventHandlerPtr handleEvent)
929 : Connector (connInfo, indices, type, comm),
930 EventInputConnector (handleEvent),
931 EventCollectiveConnector (true)
932 {
933
934 int procMethod = connInfo.processingMethod ();
935 if (procMethod == ConnectorInfo::TREE)
936 if (handleEvent.getType () == Index::GLOBAL)
937 router_ = new TreeProcessingInputGlobalRouter ();
938 else
939 router_ = new TreeProcessingInputLocalRouter ();
940 else
941 if (handleEvent.getType () == Index::GLOBAL)
942 router_ = new TableProcessingInputGlobalRouter ();
943 else
944 router_ = new TableProcessingInputLocalRouter ();
945 }
946
947
948 EventInputCollectiveConnector::~EventInputCollectiveConnector()
949 {
950 delete router_;
951 }
952
953
954 void
955 EventInputCollectiveConnector::spatialNegotiation (SpatialNegotiator* spatialNegotiator_)
956 {
957 routingMap_input = new InputRoutingMap ();
958 Subconnector* subconn
959 = EventInputCollectiveConnector::makeSubconnector (nullptr);
960 rsubconn.push_back (subconn);
961 for (NegotiationIterator i = spatialNegotiator_->negotiateSimple (); !i.end (); ++i)
962 addRoutingInterval (i->interval (), subconn);
963 routingMap_input->fillRouter (router_);
964 delete routingMap_input;
965 router_->buildTable ();
966 }
967
968
969 void
970 EventInputCollectiveConnector::addRoutingInterval (IndexInterval i,
971 Subconnector* subconn)
972 {
973 routingMap_input->insert (i, &handleEvent_);
974 }
975
976
977 EventOutputCollectiveConnector::EventOutputCollectiveConnector
978 (ConnectorInfo connInfo,
979 IndexMap* indices,
980 Index::Type type,
981 MPI_Comm comm,
982 DirectRouter* router)
983 : Connector (connInfo, indices, type, comm),
984 EventOutputConnector (nullptr),
985 EventCollectiveConnector (false),
986 directRouter_ (router)
987 {
988 }
989
990
991 EventOutputCollectiveConnector::~EventOutputCollectiveConnector()
992 {
993 }
994
995
996 Subconnector*
997 EventOutputCollectiveConnector::makeSubconnector (void *param)
998 {
999 // EventRouter * router_ = static_cast<EventRouter*>(param);
1000 if(subconnector_ == nullptr){
1001 subconnector_ = new EventOutputCollectiveSubconnector ();
1002 }
1003 return subconnector_;
1004 }
1005
1006 void
1007 EventOutputCollectiveConnector::spatialNegotiation (SpatialNegotiator* spatialNegotiator_)
1008 {
1009 EventOutputCollectiveSubconnector* subconn
1010 = dynamic_cast<EventOutputCollectiveSubconnector*>
1011 (EventOutputCollectiveConnector::makeSubconnector (nullptr));
1012 rsubconn.push_back (subconn);
1013 subconn->setRouter (directRouter_);
1014 //spatialNegotiator_->negotiateSimple ();
1015 }
1016
1017
1018 ContInputCollectiveConnector::ContInputCollectiveConnector
1019 (ConnectorInfo connInfo,
1020 IndexMap* indices,
1021 Index::Type type,
1022 MPI_Comm comm,
1023 Sampler& sampler,
1024 MPI_Datatype data_type,
1025 double delay)
1026 : Connector(connInfo, indices,type, comm),
1027 ContInputConnector(sampler, data_type, delay),
1028 ContCollectiveConnector(data_type, true)
1029 {
1030
1031 }
1032
1033
1034 void
1035 ContInputCollectiveConnector::spatialNegotiation (SpatialNegotiator* spatialNegotiator_)
1036 {
1037 std::multimap< int, Interval> receiver_intrvs;
1038 std::vector<IndexInterval> intrvs;
1039 std::map<int,int> remoteToCollectiveRankMap;
1040 receiveRemoteCommRankID(remoteToCollectiveRankMap);
1041
1042 for (NegotiationIterator i = spatialNegotiator_->negotiate (info.nProcesses (), this); !i.end (); ++i)
1043 {
1044 int begin = (i->displ()) * mpi_get_type_size (data_type_);
1045 // length field is stored overlapping the end field
1046 int end = (i->end() - i->begin()) * mpi_get_type_size (data_type_);
1047 receiver_intrvs.insert (std::make_pair ( remoteToCollectiveRankMap[i->rank ()], Interval(begin, end)));
1048 intrvs.push_back(i->interval());
1049 }
1050
1051 Subconnector *subconn = ContCollectiveConnector::makeSubconnector(&receiver_intrvs);
1052 rsubconn.push_back (subconn);
1053 for (std::vector<IndexInterval>::iterator it=intrvs.begin() ; it < intrvs.end(); it++ )
1054 addRoutingInterval ((*it), subconn);
1055 }
1056
1057
1058 void
1059 ContInputCollectiveConnector::receiveRemoteCommRankID(std::map<int,int> &remoteToCollectiveRankMap)
1060 {
1061 int nProcesses, intra_rank;
1062 MPI_Comm_remote_size (intercomm, &nProcesses);
1063
1064 for (int i =0; i < nProcesses; ++i)
1065 {
1066 MPI_Recv (&intra_rank,
1067 1,
1068 MPI_INT,
1069 i,
1070 SPATIAL_NEGOTIATION_MSG,
1071 intercomm,
1072 MPI_STATUS_IGNORE);
1073 remoteToCollectiveRankMap.insert(std::make_pair(i,intra_rank));
1074 MUSIC_LOG0( "Remote Communication Rank:" << i << "is mapped to Collective Communication Rank:" << intra_rank );
1075 }
1076
1077 }
1078
1079
1080 ContOutputCollectiveConnector::ContOutputCollectiveConnector(ConnectorInfo connInfo,
1081 IndexMap* indices,
1082 Index::Type type,
1083 MPI_Comm comm,
1084 Sampler& sampler,
1085 MPI_Datatype data_type):
1086 Connector(connInfo, indices,type, comm),
1087 ContOutputConnector( sampler, data_type),
1088 ContCollectiveConnector(data_type, false)
1089 {
1090
1091 }
1092
1093
1094 void
1095 ContOutputCollectiveConnector::spatialNegotiation ( SpatialNegotiator* spatialNegotiator_)
1096 {
1097 sendLocalCommRankID();
1098 std::map<Interval, int> empty_intrvs;
1099 Subconnector *subconn = ContCollectiveConnector::makeSubconnector(&empty_intrvs);
1100 rsubconn.push_back (subconn);
1101 spatialNegotiator_->negotiate (info.nProcesses (), this);
1102 for (NegotiationIterator i = spatialNegotiator_->negotiateSimple (); !i.end (); ++i)
1103 addRoutingInterval (i->interval(), subconn);
1104 }
1105
1106
1107 void
1108 ContOutputCollectiveConnector::sendLocalCommRankID()
1109 {
1110 int nProcesses, intra_rank;
1111 MPI_Comm_remote_size (intercomm, &nProcesses);
1112 intra_rank = mpi_get_rank (intracomm_);
1113 std::map<int,int> rCommToCollCommRankMap;
1114 for (int i =0; i < nProcesses; ++i){
1115 MPI_Ssend (&intra_rank,
1116 1,
1117 MPI_INT,
1118 i,
1119 SPATIAL_NEGOTIATION_MSG,
1120 intercomm);
1121 }
1122 }
1123 }
1124 #endif