SST  11.1.0
StructuralSimulationToolkit
event.h
1 // Copyright 2009-2021 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-2021, 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_EVENT_H
13 #define SST_CORE_EVENT_H
14 
15 #include "sst/core/activity.h"
16 #include "sst/core/sst_types.h"
17 #include "sst/core/ssthandler.h"
18 
19 #include <atomic>
20 #include <cinttypes>
21 #include <string>
22 
23 namespace SST {
24 
25 class Link;
26 
27 /**
28  * Base class for Events - Items sent across links to communicate between
29  * components.
30  */
31 class Event : public Activity
32 {
33 public:
34  /**
35  Base handler for event delivery.
36  */
38 
39  /**
40  Used to create handlers for event delivery. The callback
41  function is expected to be in the form of:
42 
43  void func(Event* event)
44 
45  In which case, the class is created with:
46 
47  new Event::Handler<classname>(this, &classname::function_name)
48 
49  Or, to add static data, the callback function is:
50 
51  void func(Event* event, dataT data)
52 
53  and the class is created with:
54 
55  new Event::Handler<classname, dataT>(this, &classname::function_name, data)
56  */
57  template <typename classT, typename dataT = void>
59 
60  /** Type definition of unique identifiers */
61  typedef std::pair<uint64_t, int> id_type;
62  /** Constant, default value for id_types */
63  static const id_type NO_ID;
64 
65  Event() : Activity()
66  {
67  setPriority(EVENTPRIORITY);
68 #if __SST_DEBUG_EVENT_TRACKING__
69  first_comp = "";
70  last_comp = "";
71 #endif
72  }
73  virtual ~Event() = 0;
74 
75  /** Cause this event to fire */
76  void execute(void) override;
77 
78  /** Clones the event in for the case of a broadcast */
79  virtual Event* clone();
80 
81  inline void setDeliveryInfo(LinkId_t id, HandlerBase* f)
82  {
83 #ifdef SST_ENFORCE_EVENT_ORDERING
84  enforce_link_order = id;
85 #else
86  link_id = id;
87 #endif
88  functor = f;
89  }
90 
91  /** Sets the link id used for delivery. For use by SST Core only */
92 #if !SST_BUILDING_CORE
93  inline void setDeliveryLink(LinkId_t id, Link* link) __attribute__((
94  deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12.")))
95  {
96 #else
97  inline void setDeliveryLink(LinkId_t id, Link* link)
98  {
99 #endif
100 #ifdef SST_ENFORCE_EVENT_ORDERING
101  enforce_link_order = id;
102 #else
103  link_id = id;
104 #endif
105  delivery_link = link;
106  }
107 
108  /** Gets the link id used for delivery. For use by SST Core only */
109 #if !SST_BUILDING_CORE
110  inline Link* getDeliveryLink() __attribute__((
111  deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12.")))
112  {
113 #else
114  inline Link* getDeliveryLink()
115  {
116 #endif
117  return delivery_link;
118  }
119 
120  /** For use by SST Core only */
121 #if !SST_BUILDING_CORE
122  inline void setRemoteEvent() __attribute__((
123  deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12.")))
124  {
125 #else
126  inline void setRemoteEvent()
127  {
128 #endif
129  delivery_link = nullptr;
130  }
131 
132  /** Gets the link id associated with this event. For use by SST Core only */
133 #if !SST_BUILDING_CORE
134  inline LinkId_t getLinkId(void) const __attribute__((
135  deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12.")))
136  {
137 #else
138  inline LinkId_t getLinkId(void) const
139  {
140 #endif
141 #ifdef SST_ENFORCE_EVENT_ORDERING
142  return enforce_link_order;
143 #else
144  return link_id;
145 #endif
146  }
147 
148 
149 #ifdef __SST_DEBUG_EVENT_TRACKING__
150 
151  virtual void printTrackingInfo(const std::string& header, Output& out) const
152  {
153  out.output(
154  "%s Event first sent from: %s:%s (type: %s) and last received by %s:%s (type: %s)\n", header.c_str(),
155  first_comp.c_str(), first_port.c_str(), first_type.c_str(), last_comp.c_str(), last_port.c_str(),
156  last_type.c_str());
157  }
158 
159  const std::string& getFirstComponentName() { return first_comp; }
160  const std::string& getFirstComponentType() { return first_type; }
161  const std::string& getFirstPort() { return first_port; }
162  const std::string& getLastComponentName() { return last_comp; }
163  const std::string& getLastComponentType() { return last_type; }
164  const std::string& getLastPort() { return last_port; }
165 
166  void addSendComponent(const std::string& comp, const std::string& type, const std::string& port)
167  {
168  if ( first_comp == "" ) {
169  first_comp = comp;
170  first_type = type;
171  first_port = port;
172  }
173  }
174  void addRecvComponent(const std::string& comp, const std::string& type, const std::string& port)
175  {
176  last_comp = comp;
177  last_type = type;
178  last_port = port;
179  }
180 
181 #endif
182 
183  void serialize_order(SST::Core::Serialization::serializer& ser) override
184  {
185  Activity::serialize_order(ser);
186 #ifndef SST_ENFORCE_EVENT_ORDERING
187  ser& link_id;
188 #endif
189 #ifdef __SST_DEBUG_EVENT_TRACKING__
190  ser& first_comp;
191  ser& first_type;
192  ser& first_port;
193  ser& last_comp;
194  ser& last_type;
195  ser& last_port;
196 #endif
197  }
198 
199 protected:
200  /** Link used for delivery */
202  HandlerBase* functor;
203 
204  /**
205  * Generates an ID that is unique across ranks, components and events.
206  */
208 
209 private:
210  static std::atomic<uint64_t> id_counter;
211  // If SST_ENFORCE_EVENT_ORDERING is turned on, then we'll use
212  // Activity::enforce_link_order as the link_id
213 #ifndef SST_ENFORCE_EVENT_ORDERING
214  LinkId_t link_id;
215 #endif
216 
217 #ifdef __SST_DEBUG_EVENT_TRACKING__
218  std::string first_comp;
219  std::string first_type;
220  std::string first_port;
221  std::string last_comp;
222  std::string last_type;
223  std::string last_port;
224 #endif
225 
226  ImplementVirtualSerializable(SST::Action)
227 };
228 
229 /**
230  * Null Event. Does nothing.
231  */
233 {
234 public:
235  NullEvent() : Event() {}
236  ~NullEvent() {}
237 
238  void execute(void) override;
239 
240 private:
241  ImplementSerializable(SST::NullEvent)
242 };
243 } // namespace SST
244 
245 #endif // SST_CORE_EVENT_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:51
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:34
Definition: ssthandler.h:100
LinkId_t getLinkId(void) const
Gets the link id associated with this event.
Definition: event.h:134
Base class for all Activities in the SST Event Queue.
Definition: activity.h:48
Event Handler class with user-data argument.
Definition: ssthandler.h:115
Null Event.
Definition: event.h:232
Link * delivery_link
Link used for delivery.
Definition: event.h:201
SSTHandlerBase< void, Event *, false > HandlerBase
Base handler for event delivery.
Definition: event.h:37
void execute(void) override
Cause this event to fire.
Definition: event.cc:27
Link * getDeliveryLink()
Gets the link id used for delivery.
Definition: event.h:110
std::pair< uint64_t, int > id_type
Type definition of unique identifiers.
Definition: event.h:61
void setPriority(int priority)
Set the priority of the Activity.
Definition: activity.h:483
static const id_type NO_ID
Constant, default value for id_types.
Definition: event.h:63
void setDeliveryLink(LinkId_t id, Link *link)
Sets the link id used for delivery.
Definition: event.h:93
void output(uint32_t line, const char *file, const char *func, const char *format,...) const
Output the message with formatting as specified by the format parameter.
Definition: output.h:182
void setRemoteEvent()
For use by SST Core only.
Definition: event.h:122
id_type generateUniqueId()
Generates an ID that is unique across ranks, components and events.
Definition: event.cc:43
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:31
virtual Event * clone()
Clones the event in for the case of a broadcast.
Definition: event.cc:33
Definition: serializable.h:138