SST  14.0.0
StructuralSimulationToolkit
link.h
1 // Copyright 2009-2024 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2024, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_LINK_H
13 #define SST_CORE_LINK_H
14 
15 #include "sst/core/event.h"
16 #include "sst/core/serialization/serialize_impl_fwd.h"
17 #include "sst/core/sst_types.h"
18 #include "sst/core/timeConverter.h"
19 
20 namespace SST {
21 
22 #define _LINK_DBG(fmt, args...) __DBG(DBG_LINK, Link, fmt, ##args)
23 
24 class ActivityQueue;
25 class BaseComponent;
26 class TimeConverter;
27 class LinkPair;
28 class Simulation_impl;
29 
30 class UnitAlgebra;
31 class LinkSendProfileToolList;
32 
33 namespace Profile {
34 class EventHandlerProfileTool;
35 }
36 
37 class Link;
38 
39 template <>
41 {
42  template <class A>
43  friend class serialize;
44  // Function implemented in link.cc
45  void operator()(Link*& s, SST::Core::Serialization::serializer& ser);
46 };
47 
48 
49 /** Link between two components. Carries events */
50 class alignas(64) Link
51 {
52  enum Type_t : uint16_t { POLL, HANDLER, SYNC, UNINITIALIZED };
53  enum Mode_t : uint16_t { INIT, RUN, COMPLETE };
54 
56 
57 public:
58  friend class LinkPair;
59  friend class RankSync;
60  friend class ThreadSync;
61  friend class Simulation_impl;
62  friend class SyncManager;
63  friend class ComponentInfo;
64 
65  ~Link();
66 
67  /** Set additional Latency to be added to events being sent out of this link
68  * @param cycles Number of Cycles to be added
69  * @param timebase Base Units of cycles
70  */
71  void addSendLatency(int cycles, const std::string& timebase);
72 
73  /** Set additional Latency to be added to events being sent out of this link
74  * @param cycles Number of Cycles to be added
75  * @param timebase Base Units of cycles
76  */
77  void addSendLatency(SimTime_t cycles, TimeConverter* timebase);
78 
79  /** Set additional Latency to be added on to events coming in on this link.
80  * @param cycles Number of Cycles to be added
81  * @param timebase Base Units of cycles
82  */
83  void addRecvLatency(int cycles, const std::string& timebase);
84 
85  /** Set additional Latency to be added on to events coming in on this link.
86  * @param cycles Number of Cycles to be added
87  * @param timebase Base Units of cycles
88  */
89  void addRecvLatency(SimTime_t cycles, TimeConverter* timebase);
90 
91  /** Set the callback function to be called when a message is
92  * delivered. Not available for Polling links.
93  * @param functor Functor to call when message is delivered
94  */
95  void setFunctor(Event::HandlerBase* functor);
96 
97  /** Replace the callback function to be called when a message is
98  * delivered. Any previous handler will be deleted.
99  * Not available for Polling links.
100  * @param functor Functor to call when message is delivered
101  */
102  void replaceFunctor(Event::HandlerBase* functor);
103 
104  /** Send an event over the link with additional delay. Sends an event
105  * over a link with an additional delay specified with a
106  * TimeConverter. I.e. the total delay is the link's delay + the
107  * additional specified delay.
108  * @param delay - additional delay
109  * @param tc - time converter to specify units for the additional delay
110  * @param event - the Event to send
111  */
112  inline void send(SimTime_t delay, TimeConverter* tc, Event* event)
113  {
114  send_impl(tc->convertToCoreTime(delay), event);
115  }
116 
117  /** Send an event with additional delay. Sends an event over a link
118  * with additional delay specified by the Link's default
119  * timebase.
120  * @param delay The additional delay, in units of the default Link timebase
121  * @param event The event to send
122  */
123  inline void send(SimTime_t delay, Event* event) { send_impl(delay * defaultTimeBase, event); }
124 
125  /** Send an event with the Link's default delay
126  * @param event The event to send
127  */
128  inline void send(Event* event) { send_impl(0, event); }
129 
130 
131  /** Retrieve a pending event from the Link. For links which do not
132  * have a set event handler, they can be polled with this function.
133  * Returns nullptr if there is no pending event.
134  * Not available for HANDLER-type links.
135  * @return Event if one is available
136  * @return nullptr if no Event is available
137  */
138  Event* recv();
139 
140  /** Manually set the default detaulTimeBase
141  * @param tc TimeConverter object for the timebase
142  */
144 
145  /** Return the default Time Base for this link
146  * @return the default Time Base for this link
147  */
149 
150  /** Return the default Time Base for this link
151  * @return the default Time Base for this link
152  */
153  const TimeConverter* getDefaultTimeBase() const;
154 
155  /** Return the ID of this link
156  * @return the unique ID for this link
157  */
158  LinkId_t getId() { return tag; }
159 
160  /** Send data during the init() or complete() phase.
161  * @param data event to send
162  */
163  void sendUntimedData(Event* data);
164 
165  /** Receive an event (if any) during the init() or complete() phase.
166  * @return Event if one is available
167  * @return nullptr if no Event is available
168  */
170 
171  /** Return whether link has been configured
172  * @return whether link is configured
173  */
174  bool isConfigured() { return type != UNINITIALIZED; }
175 
176 #ifdef __SST_DEBUG_EVENT_TRACKING__
177  void setSendingComponentInfo(const std::string& comp_in, const std::string& type_in, const std::string& port_in)
178  {
179  comp = comp_in;
180  ctype = type_in;
181  port = port_in;
182  }
183 
184  const std::string& getSendingComponentName() { return comp; }
185  const std::string& getSendingComponentType() { return ctype; }
186  const std::string& getSendingPort() { return port; }
187 
188 #endif
189 
190 protected:
191  Link();
192 
193  void setAsSyncLink() { type = SYNC; }
194 
195  /**
196  Set the delivery_info for the link
197  */
198  void setDeliveryInfo(uintptr_t info) { delivery_info = info; }
199 
200  /** Send an event over the link with additional delay. Sends an event
201  * over a link with an additional delay specified with a
202  * TimeConverter. I.e. the total delay is the link's delay + the
203  * additional specified delay.
204  * @param delay - additional total delay to add
205  * @param event - the Event to send
206  */
207  void send_impl(SimTime_t delay, Event* event);
208 
209  // Since Links are found in pairs, I will keep all the information
210  // needed for me to send and deliver an event to the other side of
211  // the link. That means, that I mostly keep my pair's
212  // information. The one consequence, is that polling links will
213  // have to pull the data from the pair, but since this is a less
214  // common case, that's okay (this decision makes the common case
215  // faster and the less common case slower).
216 
217  /** Queue of events to be received by the owning component */
219 
220  /** Holds the delivery information. This is stored as a
221  uintptr_t, but is actually a pointer converted using
222  reinterpret_cast. For links connected to a
223  Component/SubComponent, this holds a pointer to the delivery
224  functor. For links connected to a Sync object, this holds a
225  pointer to the remote link to send the event on after
226  synchronization.
227  */
228  uintptr_t delivery_info;
229 
230  /** Timebase used if no other timebase is specified. Used to specify
231  the units for added delays when sending, such as in
232  Link::send(). Often set by the Component::registerClock()
233  function if the regAll argument is true.
234  */
235  SimTime_t defaultTimeBase;
236 
237  /** Latency of the link. It is used by the partitioner as the
238  weight. This latency is added to the delay put on the event by
239  the component.
240  */
241  SimTime_t latency;
242 
243  /** Pointer to the opposite side of this link */
245 
246 private:
247  friend class BaseComponent;
248 
249  SimTime_t& current_time;
250  Type_t type;
251  Mode_t mode;
252  LinkId_t tag;
253 
254  /** Create a new link with a given tag
255 
256  The tag is used for two different things depending on where
257  this link sends data:
258 
259  If it sends it to a Sync object, then it represents the
260  remote_tag used to lookup the correct link on the other side.
261 
262  If it sends to a TimeVortex (or DirectLinkQueue), it is the
263  value used for enforce_link_order (if that feature is
264  enabled).
265  */
266  Link(LinkId_t tag);
267 
268  Link(const Link& l);
269 
270  /** Specifies that this link has no callback, and is poll-based only */
271  void setPolling();
272 
273  /** Causes an event to be delivered to the registered callback */
274  inline void deliverEvent(Event* event) const { (*reinterpret_cast<Event::HandlerBase*>(delivery_info))(event); }
275 
276  /** Set minimum link latency */
277  void setLatency(Cycle_t lat);
278 
279  void sendUntimedData_sync(Event* data);
280  void finalizeConfiguration();
281  void prepareForComplete();
282 
283  void addProfileTool(SST::Profile::EventHandlerProfileTool* tool, const EventHandlerMetaData& mdata);
284 
285 
286  LinkSendProfileToolList* profile_tools;
287 
288 
289 #ifdef __SST_DEBUG_EVENT_TRACKING__
290  std::string comp;
291  std::string ctype;
292  std::string port;
293 #endif
294 };
295 
296 /** Self Links are links from a component to itself */
297 class SelfLink : public Link
298 {
299 public:
300  SelfLink() : Link()
301  {
302  pair_link = this;
303  latency = 0;
304  }
305 };
306 
307 
308 } // namespace SST
309 
310 #endif // SST_CORE_LINK_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Definition: syncManager.h:134
Handlers with 1 handler defined argument to callback from caller.
Definition: ssthandler.h:210
Definition: event.h:218
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:27
Definition: syncManager.h:37
Base serialize class.
Definition: serialize.h:32
Definition: action.cc:18
Definition: eventHandlerProfileTool.h:29
Definition: syncManager.h:87
Serialization "gateway" object.
Definition: serialize.h:110
Main control class for a SST Simulation.
Definition: simulation_impl.h:70
Definition: link.cc:305
Main component object for the simulation.
Definition: baseComponent.h:51
Defines a pair of links (to define a connected link)
Definition: linkPair.h:23
SimTime_t convertToCoreTime(SimTime_t time) const
Converts from the component&#39;s view to the core&#39;s view of time.
Definition: timeConverter.h:37
Definition: componentInfo.h:40
Base Class for a queue of Activities.
Definition: activityQueue.h:21
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:34