SST  11.0.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_CORE_EVENT_H
13 #define SST_CORE_CORE_EVENT_H
14 
15 #include "sst/core/sst_types.h"
16 
17 #include <atomic>
18 #include <string>
19 #include <cinttypes>
20 
21 #include "sst/core/activity.h"
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 public:
33 
34  /** Type definition of unique identifiers */
35  typedef std::pair<uint64_t, int> id_type;
36  /** Constant, default value for id_types */
37  static const id_type NO_ID;
38 
39 
40  Event() : Activity() {
41  setPriority(EVENTPRIORITY);
42 #if __SST_DEBUG_EVENT_TRACKING__
43  first_comp = "";
44  last_comp = "";
45 #endif
46  }
47  virtual ~Event() = 0;
48 
49  /** Cause this event to fire */
50  void execute(void) override;
51 
52  /** Clones the event in for the case of a broadcast */
53  virtual Event* clone();
54 
55  /** Sets the link id used for delivery. For use by SST Core only */
56 #if !SST_BUILDING_CORE
57  inline void setDeliveryLink(LinkId_t id, Link *link) __attribute__ ((deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12."))) {
58 #else
59  inline void setDeliveryLink(LinkId_t id, Link *link) {
60 #endif
61 #ifdef SST_ENFORCE_EVENT_ORDERING
62  enforce_link_order = id;
63 #else
64  link_id = id;
65 #endif
66  delivery_link = link;
67  }
68 
69  /** Gets the link id used for delivery. For use by SST Core only */
70 #if !SST_BUILDING_CORE
71  inline Link* getDeliveryLink() __attribute__ ((deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12."))) {
72 #else
73  inline Link* getDeliveryLink() {
74 #endif
75  return delivery_link;
76  }
77 
78  /** For use by SST Core only */
79 #if !SST_BUILDING_CORE
80  inline void setRemoteEvent() __attribute__ ((deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12."))) {
81 #else
82  inline void setRemoteEvent() {
83 #endif
84  delivery_link = nullptr;
85  }
86 
87  /** Gets the link id associated with this event. For use by SST Core only */
88 #if !SST_BUILDING_CORE
89  inline LinkId_t getLinkId(void) const __attribute__ ((deprecated("this function was not intended to be used outside of SST Core and will be removed in SST 12."))) {
90 #else
91  inline LinkId_t getLinkId(void) const {
92 #endif
93 #ifdef SST_ENFORCE_EVENT_ORDERING
94  return enforce_link_order;
95 #else
96  return link_id;
97 #endif
98  }
99 
100 
101  /// Functor classes for Event handling
102  class HandlerBase {
103  public:
104  /** Handler function */
105  virtual void operator()(Event*) = 0;
106  virtual ~HandlerBase() {}
107  };
108 
109 
110  /**
111  * Event Handler class with user-data argument
112  */
113  template <typename classT, typename argT = void>
114  class Handler : public HandlerBase {
115  private:
116  typedef void (classT::*PtrMember)(Event*, argT);
117  classT* object;
118  const PtrMember member;
119  argT data;
120 
121  public:
122  /** Constructor
123  * @param object - Pointer to Object upon which to call the handler
124  * @param member - Member function to call as the handler
125  * @param data - Additional argument to pass to handler
126  */
127  Handler( classT* const object, PtrMember member, argT data ) :
128  object(object),
129  member(member),
130  data(data)
131  {}
132 
133  void operator()(Event* event) {
134  (object->*member)(event,data);
135  }
136  };
137 
138 
139  /**
140  * Event Handler class with no user-data.
141  */
142  template <typename classT>
143  class Handler<classT, void> : public HandlerBase {
144  private:
145  typedef void (classT::*PtrMember)(Event*);
146  const PtrMember member;
147  classT* object;
148 
149  public:
150  /** Constructor
151  * @param object - Pointer to Object upon which to call the handler
152  * @param member - Member function to call as the handler
153  */
154  Handler( classT* const object, PtrMember member ) :
155  member(member),
156  object(object)
157  {}
158 
159  void operator()(Event* event) {
160  (object->*member)(event);
161  }
162  };
163 
164  /** Virtual function to "pretty-print" this event. Should be implemented by subclasses. */
165  virtual void print(const std::string& header, Output &out) const override {
166  out.output("%s Generic Event to be delivered at %" PRIu64 " with priority %d\n",
167  header.c_str(), getDeliveryTime(), getPriority());
168  }
169 
170 #ifdef __SST_DEBUG_EVENT_TRACKING__
171 
172  virtual void printTrackingInfo(const std::string& header, Output &out) const {
173  out.output("%s Event first sent from: %s:%s (type: %s) and last received by %s:%s (type: %s)\n", header.c_str(),
174  first_comp.c_str(),first_port.c_str(),first_type.c_str(),
175  last_comp.c_str(),last_port.c_str(),last_type.c_str());
176  }
177 
178  const std::string& getFirstComponentName() { return first_comp; }
179  const std::string& getFirstComponentType() { return first_type; }
180  const std::string& getFirstPort() { return first_port; }
181  const std::string& getLastComponentName() { return last_comp; }
182  const std::string& getLastComponentType() { return last_type; }
183  const std::string& getLastPort() { return last_port; }
184 
185  void addSendComponent(const std::string& comp, const std::string& type, const std::string& port) {
186  if ( first_comp == "" ) {
187  first_comp = comp;
188  first_type = type;
189  first_port = port;
190  }
191  }
192  void addRecvComponent(const std::string& comp, const std::string& type, const std::string& port) {
193  last_comp = comp;
194  last_type = type;
195  last_port = port;
196  }
197 
198 #endif
199 
200  void serialize_order(SST::Core::Serialization::serializer &ser) override{
201  Activity::serialize_order(ser);
202 #ifndef SST_ENFORCE_EVENT_ORDERING
203  ser & link_id;
204 #endif
205 #ifdef __SST_DEBUG_EVENT_TRACKING__
206  ser & first_comp;
207  ser & first_type;
208  ser & first_port;
209  ser & last_comp;
210  ser & last_type;
211  ser & last_port;
212 #endif
213 
214  }
215 
216 protected:
217  /** Link used for delivery */
219 
220  /**
221  * Generates an ID that is unique across ranks, components and events.
222  */
224 
225 private:
226  static std::atomic<uint64_t> id_counter;
227  // If SST_ENFORCE_EVENT_ORDERING is turned on, then we'll use
228  // Activity::enforce_link_order as the link_id
229 #ifndef SST_ENFORCE_EVENT_ORDERING
230  LinkId_t link_id;
231 #endif
232 
233 #ifdef __SST_DEBUG_EVENT_TRACKING__
234  std::string first_comp;
235  std::string first_type;
236  std::string first_port;
237  std::string last_comp;
238  std::string last_type;
239  std::string last_port;
240 #endif
241 
242 };
243 
244 
245 /**
246  * Null Event. Does nothing.
247  */
249 public:
250  NullEvent() : Event() {}
251  ~NullEvent() {}
252 
253  void execute(void) override;
254 
255  virtual void print(const std::string& header, Output &out) const override {
256  out.output("%s NullEvent to be delivered at %" PRIu64 " with priority %d\n",
257  header.c_str(), getDeliveryTime(), getPriority());
258  }
259 
260 
261 
262 private:
263  ImplementSerializable(SST::NullEvent)
264 
265 };
266 } //namespace SST
267 
268 #endif // SST_CORE_EVENT_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:54
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
void operator()(Event *event)
Handler function.
Definition: event.h:159
LinkId_t getLinkId(void) const
Gets the link id associated with this event.
Definition: event.h:89
Base class for all Activities in the SST Event Queue.
Definition: activity.h:52
Handler(classT *const object, PtrMember member)
Constructor.
Definition: event.h:154
void operator()(Event *event)
Handler function.
Definition: event.h:133
virtual void operator()(Event *)=0
Handler function.
Null Event.
Definition: event.h:248
Link * delivery_link
Link used for delivery.
Definition: event.h:218
void execute(void) override
Cause this event to fire.
Definition: event.cc:28
Link * getDeliveryLink()
Gets the link id used for delivery.
Definition: event.h:71
Event Handler class with user-data argument.
Definition: event.h:114
std::pair< uint64_t, int > id_type
Type definition of unique identifiers.
Definition: event.h:35
void setPriority(int priority)
Set the priority of the Activity.
Definition: activity.h:338
static const id_type NO_ID
Constant, default value for id_types.
Definition: event.h:37
SimTime_t getDeliveryTime() const
Return the time at which this Activity will be delivered.
Definition: activity.h:205
void setDeliveryLink(LinkId_t id, Link *link)
Sets the link id used for delivery.
Definition: event.h:57
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:186
virtual void print(const std::string &header, Output &out) const override
Virtual function to &quot;pretty-print&quot; this event.
Definition: event.h:255
int getPriority() const
Return the Priority of this Activity.
Definition: activity.h:210
void setRemoteEvent()
For use by SST Core only.
Definition: event.h:80
Functor classes for Event handling.
Definition: event.h:102
id_type generateUniqueId()
Generates an ID that is unique across ranks, components and events.
Definition: event.cc:40
virtual void print(const std::string &header, Output &out) const override
Virtual function to &quot;pretty-print&quot; this event.
Definition: event.h:165
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:31
Handler(classT *const object, PtrMember member, argT data)
Constructor.
Definition: event.h:127
virtual Event * clone()
Clones the event in for the case of a broadcast.
Definition: event.cc:33
Definition: serializable.h:132