Debian Codegraph @

src/port.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 #include "music/port.hh"
19
20 #if MUSIC_USE_MPI
21 #include "music/event_router.hh"
22
23 #include "music/setup.hh" // Must be included first on BG/L
24 #include "music/error.hh"
25
26 namespace MUSIC {
27
28 Port::Port (Setup* s, std::string identifier)
29 : portName_ (identifier), setup_ (s), isMapped_ (false)
30 {
31 s->maybePostponedSetup ();
32 ConnectivityInfo_ = s->portConnectivity (portName_);
33 setup_->addPort (this);
34 }
35
36
37 bool
38 Port::isConnected ()
39 {
40 return ConnectivityInfo_ != Connectivity::NO_CONNECTIVITY;
41 }
42
43
44 void
45 Port::checkConnected (std::string action)
46 {
47 if (!isConnected ())
48 {
49 std::ostringstream msg;
50 msg << "attempt to " << action << " port `" << portName_
51 << "' which is unconnected";
52 error (msg);
53 }
54 }
55
56
57 void
58 Port::assertOutput ()
59 {
60 checkCalledOnce (isMapped_,
61 "OutputPort::map (...)",
62 " for port " + portName_);
63 checkConnected ("map");
64 if (ConnectivityInfo_->direction () != ConnectivityInfo::OUTPUT)
65 {
66 std::ostringstream msg;
67 msg << "output port `" << ConnectivityInfo_->portName ()
68 << "' connected as input";
69 error (msg);
70 }
71 }
72
73
74 void
75 Port::assertInput ()
76 {
77 checkCalledOnce (isMapped_,
78 "InputPort::map (...)",
79 " for port " + portName_);
80 checkConnected ("map");
81 if (ConnectivityInfo_->direction () != ConnectivityInfo::INPUT)
82 {
83 std::ostringstream msg;
84 msg << "input port `" << ConnectivityInfo_->portName ()
85 << "' connected as output";
86 error (msg);
87 }
88 }
89
90
91 bool
92 Port::hasWidth ()
93 {
94 checkConnected ("ask for width of");
95 return ConnectivityInfo_->width () != ConnectivityInfo::NO_WIDTH;
96 }
97
98
99 int
100 Port::width ()
101 {
102 checkConnected ("ask for width of");
103 int w = ConnectivityInfo_->width ();
104 if (w == ConnectivityInfo::NO_WIDTH)
105 {
106 std::ostringstream msg;
107 msg << "width requested for port `" << ConnectivityInfo_->portName ()
108 << "' which has unspecified width";
109 error (msg);
110 }
111 return w;
112 }
113
114
115 void
116 Port::checkIndexMap (IndexMap* indexMap)
117 {
118 if (indexMap->begin () != indexMap->end ())
119 {
120 IndexMap::iterator i = indexMap->begin ();
121 IndexInterval last = *i;
122 ++i;
123 for (; i != indexMap->end (); ++i)
124 {
125 if (i->begin () < last.end ())
126 {
127 std::ostringstream os;
128 os << "Mapping port " << portName_
129 << " using index map with overlaps: ["
130 << last.begin () << ", " << last.end () << "), ["
131 << i->begin () << ", " << i->end () << ")";
132 error (os);
133 }
134 last = *i;
135 }
136 }
137 }
138
139
140 void
141 OutputPort::mapImpl (IndexMap* indices,
142 Index::Type type,
143 int maxBuffered,
144 int dataSize)
145 {
146 checkIndexMap (indices);
147 // The timing algorithm in uses a different definition of
148 // maxBuffered. While the interface in port.hh counts the number
149 // of "ticks of data" which must be stored, the timing algorithm
150 // counts the offset in time between the processes in sender
151 // ticks.
152 if (maxBuffered != MAX_BUFFERED_NO_VALUE)
153 maxBuffered -= 1;
154
155 // Retrieve info about all remote connectors of this port
156 PortConnectorInfo portConnections
157 = ConnectivityInfo_->connections ();
158 indices_ = indices;
159 index_type_ = type;
160 for (PortConnectorInfo::iterator info = portConnections.begin ();
161 info != portConnections.end ();
162 ++info)
163 {
164 // Create connector
165 Connector* connector = makeConnector (*info);
166 setup_->addConnection (new OutputConnection (connector,
167 maxBuffered,
168 dataSize));
169 }
170 }
171
172
173
174
175 void
176 InputPort::mapImpl (IndexMap* indices,
177 Index::Type type,
178 double accLatency,
179 int maxBuffered,
180 bool interpolate)
181 {
182 checkIndexMap (indices);
183 // The timing algorithm in uses a different definition of
184 // maxBuffered. While the interface in port.hh counts the number
185 // of "ticks of data" which must be stored, the timing algorithm
186 // counts the offset in time between the processes in sender
187 // ticks.
188 if (maxBuffered != MAX_BUFFERED_NO_VALUE)
189 maxBuffered -= 1;
190
191 // Retrieve info about all remote connectors of this port
192 PortConnectorInfo portConnections
193 = ConnectivityInfo_->connections ();
194 PortConnectorInfo::iterator info = portConnections.begin ();
195 indices_ = indices;
196 index_type_ = type;
197 Connector* connector = makeConnector (*info);
198 ClockState integerLatency (accLatency, setup_->timebase ());
199 setup_->addConnection (new InputConnection (connector,
200 maxBuffered,
201 integerLatency,
202 interpolate));
203 }
204
205
206 /********************************************************************
207 *
208 * Cont Ports
209 *
210 ********************************************************************/
211
212 void
213 ContOutputPort::map (DataMap* dmap)
214 {
215 assertOutput ();
216 int maxBuffered = MAX_BUFFERED_NO_VALUE;
217 mapImpl (dmap, maxBuffered);
218 }
219
220
221 void
222 ContOutputPort::map (DataMap* dmap, int maxBuffered)
223 {
224 assertOutput ();
225 if (maxBuffered <= 0)
226 {
227 error ("ContOutputPort::map: maxBuffered should be a positive integer");
228 }
229 mapImpl (dmap, maxBuffered);
230 }
231
232
233 void
234 ContOutputPort::mapImpl (DataMap* dmap,
235 int maxBuffered)
236 {
237 sampler.configure (dmap);
238 type_ = dmap->type ();
239 OutputPort::mapImpl (dmap->indexMap (),
240 Index::GLOBAL,
241 maxBuffered,
242 0);
243 }
244
245
246 Connector*
247 ContOutputPort::makeConnector (ConnectorInfo connInfo)
248 {
249 Connector * conn;
250 if(connInfo.communicationType() == ConnectorInfo::POINTTOPOINT){
251 conn = new ContOutputConnector (connInfo,
252 indices_,
253 index_type_,
254 setup_->communicator (),
255 sampler,
256 type_);
257 }
258 else
259 conn = new ContOutputCollectiveConnector(connInfo,
260 indices_,
261 index_type_,
262 setup_->communicator (),
263 sampler,
264 type_);
265
266 return conn;
267 }
268
269
270 void
271 ContOutputPort::tick ()
272 {
273 sampler.newSample ();
274 }
275
276
277 void
278 ContInputPort::map (DataMap* dmap, double delay, bool interpolate)
279 {
280 assertInput ();
281 int maxBuffered = MAX_BUFFERED_NO_VALUE;
282 mapImpl (dmap,
283 delay,
284 maxBuffered,
285 interpolate);
286 }
287
288
289 void
290 ContInputPort::map (DataMap* dmap,
291 int maxBuffered,
292 bool interpolate)
293 {
294 assertInput ();
295 mapImpl (dmap,
296 0.0,
297 maxBuffered,
298 interpolate);
299 }
300
301
302 void
303 ContInputPort::map (DataMap* dmap,
304 double delay,
305 int maxBuffered,
306 bool interpolate)
307 {
308 assertInput ();
309 mapImpl (dmap,
310 delay,
311 maxBuffered,
312 interpolate);
313 }
314
315
316 void
317 ContInputPort::mapImpl (DataMap* dmap,
318 double delay,
319 int maxBuffered,
320 bool interpolate)
321 {
322 sampler.configure (dmap);
323 delay_ = delay;
324 type_ = dmap->type ();
325 InputPort::mapImpl (dmap->indexMap (),
326 Index::GLOBAL,
327 delay,
328 maxBuffered,
329 interpolate);
330 }
331
332
333 Connector*
334 ContInputPort::makeConnector (ConnectorInfo connInfo)
335 {
336 Connector * conn;
337 if(connInfo.communicationType() == ConnectorInfo::POINTTOPOINT){
338 conn = new ContInputConnector (connInfo,
339 indices_,
340 index_type_,
341 setup_->communicator (),
342 sampler,
343 type_,
344 delay_);
345 }
346 else
347 conn = new ContInputCollectiveConnector (connInfo,
348 indices_,
349 index_type_,
350 setup_->communicator (),
351 sampler,
352 type_,
353 delay_);
354 return conn;
355 }
356
357
358 /********************************************************************
359 *
360 * Event Ports
361 *
362 ********************************************************************/
363
364 EventOutputPort::EventOutputPort (Setup* s, std::string id)
365 : Port (s, id), routingMap (new OutputRoutingMap () /* deleted in buildTable */)
366 {
367 /* remedius
368 * Depending on the communication type (<commType>) and
369 * processing method (<procMethod>) that was introduced as
370 * runtime configuration options,
371 * particular processing router should be created on the output side.
372 * When collective communication type is used,
373 * then the processing method has to be TABLE on the output side, as in this case
374 * the processing on the output side means just an insertion of the event to the buffer.
375 * The difference between point-to-point and collective communication types is that on the output side
376 * in the latest case we have only one common buffer (one CollectiveSubconnector) and processing is happening on the receiver side.
377 */
378 if (isConnected ())
379 {
380 bool mixed = false;
381 PortConnectorInfo::iterator c = ConnectivityInfo_->connections ().begin ();
382 int commType = c->communicationType ();
383 int procMethod = c->processingMethod ();
384 for (++c; c != ConnectivityInfo_->connections ().end (); ++c)
385 {
386 if (c->processingMethod () != procMethod)
387 error ("MUSIC: can't use both tree and table for same port");
388 if (c->communicationType () != commType)
389 mixed = true;
390 }
391 if (!mixed)
392 {
393 if (commType == ConnectorInfo::COLLECTIVE)
394 router = new DirectRouter ();
395 else if (procMethod == ConnectorInfo::TREE)
396 router = new TreeProcessingOutputRouter ();
397 else
398 router = new TableProcessingOutputRouter ();
399 }
400 else
401 {
402 if (procMethod == ConnectorInfo::TREE)
403 router = new HybridTreeProcessingOutputRouter ();
404 else
405 router = new HybridTableProcessingOutputRouter ();
406 }
407 }
408 }
409
410
411 EventOutputPort::~EventOutputPort()
412 {
413 if (router != NULL)
414 delete router;
415 }
416
417 void
418 EventOutputPort::mapImpl (IndexMap* indices,
419 Index::Type type,
420 int maxBuffered)
421 {
422 OutputPort::mapImpl (indices, type, maxBuffered, (int) sizeof (Event));
423 }
424
425 void
426 EventOutputPort::map (IndexMap* indices, Index::Type type)
427 {
428 assertOutput ();
429 EventOutputPort::mapImpl (indices, type, MAX_BUFFERED_NO_VALUE);
430 }
431
432
433 void
434 EventOutputPort::map (IndexMap* indices,
435 Index::Type type,
436 int maxBuffered)
437 {
438 assertOutput ();
439 if (maxBuffered <= 0)
440 {
441 error ("EventOutputPort::map: maxBuffered should be a positive integer");
442 }
443 EventOutputPort::mapImpl (indices, type, maxBuffered);
444 }
445
446
447 Connector*
448 EventOutputPort::makeConnector (ConnectorInfo connInfo)
449 {
450 Connector *conn;
451 // we need to choose a right connector according to the communication type
452 if (connInfo.communicationType () == ConnectorInfo::POINTTOPOINT)
453 conn = new EventOutputConnector (connInfo,
454 indices_,
455 index_type_,
456 setup_->communicator (),
457 routingMap);
458 else
459 conn = new EventOutputCollectiveConnector
460 (connInfo,
461 indices_,
462 index_type_,
463 setup_->communicator (),
464 router->directRouter ());
465
466 return conn;
467 }
468
469
470 void
471 EventOutputPort::insertEventImpl (double t, int id)
472 {
473 router->processEvent(t, id);
474 }
475
476 void
477 EventOutputPort::insertEvent (double t, GlobalIndex id)
478 {
479 insertEventImpl(t, id);
480 }
481
482
483 void
484 EventOutputPort::insertEvent (double t, LocalIndex id)
485 {
486 insertEventImpl(t, id);
487 }
488
489 void
490 EventOutputPort::buildTable ()
491 {
492 routingMap->fillRouter (router);
493 delete routingMap;
494 router->buildTable ();
495 }
496
497
498 EventInputPort::EventInputPort (Setup* s, std::string id)
499 : Port (s, id)
500 {
501
502 }
503
504
505 void
506 EventInputPort::map (IndexMap* indices,
507 EventHandlerGlobalIndex* handleEvent,
508 double accLatency)
509 {
510 assertInput ();
511 int maxBuffered = MAX_BUFFERED_NO_VALUE;
512 mapImpl (indices,
513 Index::GLOBAL,
514 EventHandlerPtr (handleEvent),
515 accLatency,
516 maxBuffered);
517 }
518
519
520 void
521 EventInputPort::map (IndexMap* indices,
522 EventHandlerLocalIndex* handleEvent,
523 double accLatency)
524 {
525 assertInput ();
526 int maxBuffered = MAX_BUFFERED_NO_VALUE;
527 mapImpl (indices,
528 Index::LOCAL,
529 EventHandlerPtr (handleEvent),
530 accLatency,
531 maxBuffered);
532 }
533
534
535 void
536 EventInputPort::map (IndexMap* indices,
537 EventHandlerGlobalIndex* handleEvent,
538 double accLatency,
539 int maxBuffered)
540 {
541 assertInput ();
542 if (maxBuffered <= 0)
543 {
544 error ("EventInputPort::map: maxBuffered should be a positive integer");
545 }
546 mapImpl (indices,
547 Index::GLOBAL,
548 EventHandlerPtr (handleEvent),
549 accLatency,
550 maxBuffered);
551 }
552
553
554 void
555 EventInputPort::map (IndexMap* indices,
556 EventHandlerLocalIndex* handleEvent,
557 double accLatency,
558 int maxBuffered)
559 {
560 assertInput ();
561 if (maxBuffered <= 0)
562 {
563 error ("EventInputPort::map: maxBuffered should be a positive integer");
564 }
565 mapImpl (indices,
566 Index::LOCAL,
567 EventHandlerPtr (handleEvent),
568 accLatency,
569 maxBuffered);
570 }
571
572
573 void
574 EventInputPort::mapImpl (IndexMap* indices,
575 Index::Type type,
576 EventHandlerPtr handleEvent,
577 double accLatency,
578 int maxBuffered)
579 {
580 type_ = type;
581 handleEvent_ = handleEvent;
582 InputPort::mapImpl (indices,
583 type,
584 accLatency,
585 maxBuffered,
586 false);
587 }
588
589
590 Connector*
591 EventInputPort::makeConnector (ConnectorInfo connInfo)
592 {
593 // we need to choose a right connector according to the communication type
594 Connector *conn;
595 if(connInfo.communicationType() == ConnectorInfo::POINTTOPOINT)
596 conn = new EventInputConnector (connInfo,
597 indices_,
598 index_type_,
599 setup_->communicator (),
600 handleEvent_);
601 else
602 conn = new EventInputCollectiveConnector(connInfo,
603 indices_,
604 index_type_,
605 setup_->communicator (),
606 handleEvent_);
607
608 return conn;
609 }
610
611
612 EventHandlerGlobalIndexProxy*
613 EventInputPort::allocEventHandlerGlobalIndexProxy (void (*eh) (double, int))
614 {
615 cEventHandlerGlobalIndex = EventHandlerGlobalIndexProxy (eh);
616 return &cEventHandlerGlobalIndex;
617 }
618
619
620 EventHandlerLocalIndexProxy*
621 EventInputPort::allocEventHandlerLocalIndexProxy (void (*eh) (double, int))
622 {
623 cEventHandlerLocalIndex = EventHandlerLocalIndexProxy (eh);
624 return &cEventHandlerLocalIndex;
625 }
626
627 /********************************************************************
628 *
629 * Message Ports
630 *
631 ********************************************************************/
632
633 MessagePort::MessagePort (Setup* s)
634 : rank_ (mpi_get_rank (s->communicator ()))
635 {
636 }
637
638
639 MessageOutputPort::MessageOutputPort (Setup* s, std::string id)
640 : Port (s, id), MessagePort (s)
641 {
642 }
643
644
645 void
646 MessageOutputPort::map ()
647 {
648 assertOutput ();
649 int maxBuffered = MAX_BUFFERED_NO_VALUE;
650 mapImpl (maxBuffered);
651 }
652
653
654 void
655 MessageOutputPort::map (int maxBuffered)
656 {
657 assertOutput ();
658 if (maxBuffered <= 0)
659 {
660 error ("MessageOutputPort::map: maxBuffered should be a positive integer");
661 }
662 mapImpl (maxBuffered);
663 }
664
665
666 void
667 MessageOutputPort::mapImpl (int maxBuffered)
668 {
669 // Identify ourselves
670 LinearIndex indices (rank_, 1);
671 OutputPort::mapImpl (&indices,
672 Index::GLOBAL,
673 maxBuffered,
674 1);
675 }
676
677
678 Connector*
679 MessageOutputPort::makeConnector (ConnectorInfo connInfo)
680 {
681 return new MessageOutputConnector (connInfo,
682 indices_,
683 index_type_,
684 setup_->communicator (),
685 buffers);
686 }
687
688
689 void
690 MessageOutputPort::insertMessage (double t, void* msg, size_t size)
691 {
692 // One output buffer per OutputConnector (since different
693 // connectors may need to send at different times)
694 for (std::vector<FIBO*>::iterator b = buffers.begin ();
695 b != buffers.end ();
696 ++b)
697 {
698 MessageHeader header (t, size);
699 (*b)->insert (header.data (), sizeof (MessageHeader));
700 (*b)->insert (msg, size);
701 }
702 }
703
704
705 MessageInputPort::MessageInputPort (Setup* s, std::string id)
706 : Port (s, id), MessagePort (s)
707 {
708 }
709
710
711 void
712 MessageInputPort::map (MessageHandler* handleMessage,
713 double accLatency)
714 {
715 assertInput ();
716 int maxBuffered = MAX_BUFFERED_NO_VALUE;
717 mapImpl (handleMessage,
718 accLatency,
719 maxBuffered);
720 }
721
722
723 void
724 MessageInputPort::map (int maxBuffered)
725 {
726 assertInput ();
727 if (maxBuffered <= 0)
728 {
729 error ("MessageInputPort::map: maxBuffered should be a positive integer");
730 }
731 mapImpl (0,
732 0.0,
733 maxBuffered);
734 }
735
736
737 void
738 MessageInputPort::map (double accLatency,
739 int maxBuffered)
740 {
741 assertInput ();
742 if (maxBuffered <= 0)
743 {
744 error ("MessageInputPort::map: maxBuffered should be a positive integer");
745 }
746 mapImpl (0,
747 accLatency,
748 maxBuffered);
749 }
750
751
752 void
753 MessageInputPort::map (MessageHandler* handleMessage,
754 int maxBuffered)
755 {
756 assertInput ();
757 if (maxBuffered <= 0)
758 {
759 error ("MessageInputPort::map: maxBuffered should be a positive integer");
760 }
761 mapImpl (handleMessage,
762 0.0,
763 maxBuffered);
764 }
765
766
767 void
768 MessageInputPort::map (MessageHandler* handleMessage,
769 double accLatency,
770 int maxBuffered)
771 {
772 assertInput ();
773 if (maxBuffered <= 0)
774 {
775 error ("MessageInputPort::map: maxBuffered should be a positive integer");
776 }
777 mapImpl (handleMessage,
778 accLatency,
779 maxBuffered);
780 }
781
782
783 void
784 MessageInputPort::mapImpl (MessageHandler* handleMessage,
785 double accLatency,
786 int maxBuffered)
787 {
788 handleMessage_ = handleMessage;
789 // Receive from everybody
790 LinearIndex indices (0, handleMessage ? Index::WILDCARD_MAX : 0);
791 InputPort::mapImpl (&indices,
792 Index::GLOBAL,
793 accLatency,
794 maxBuffered,
795 false);
796 }
797
798
799 Connector*
800 MessageInputPort::makeConnector (ConnectorInfo connInfo)
801 {
802 return new MessageInputConnector (connInfo,
803 indices_,
804 index_type_,
805 handleMessage_,
806 setup_->communicator ());
807 }
808
809
810 MessageHandlerProxy*
811 MessageInputPort::allocMessageHandlerProxy (void (*mh) (double,
812 void*,
813 size_t))
814 {
815 cMessageHandler = MessageHandlerProxy (mh);
816 return &cMessageHandler;
817 }
818
819
820 }
821 #endif