SST  7.1.0
StructuralSimulationToolkit
event.h
1 // Copyright 2009-2017 Sandia Corporation. Under the terms
2 // of Contract DE-NA0003525 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2017, Sandia Corporation
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  /** Constatn, 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  inline void setDeliveryLink(LinkId_t id, Link *link) {
57 #ifdef SST_ENFORCE_EVENT_ORDERING
58  enforce_link_order = id;
59 #else
60  link_id = id;
61 #endif
62  delivery_link = link;
63  }
64 
65  /** Gets the link id used for delivery. For use by SST Core only */
66  inline Link* getDeliveryLink() {
67  return delivery_link;
68  }
69 
70  /** For use by SST Core only */
71  inline void setRemoteEvent() {
72  delivery_link = NULL;
73  }
74 
75  /** Gets the link id associated with this event. For use by SST Core only */
76  inline LinkId_t getLinkId(void) const {
77 #ifdef SST_ENFORCE_EVENT_ORDERING
78  return enforce_link_order;
79 #else
80  return link_id;
81 #endif
82  }
83 
84 
85  /// Functor classes for Event handling
86  class HandlerBase {
87  public:
88  /** Handler function */
89  virtual void operator()(Event*) = 0;
90  virtual ~HandlerBase() {}
91  };
92 
93 
94  /**
95  * Event Handler class with user-data argument
96  */
97  template <typename classT, typename argT = void>
98  class Handler : public HandlerBase {
99  private:
100  typedef void (classT::*PtrMember)(Event*, argT);
101  classT* object;
102  const PtrMember member;
103  argT data;
104 
105  public:
106  /** Constructor
107  * @param object - Pointer to Object upon which to call the handler
108  * @param member - Member function to call as the handler
109  * @param data - Additional argument to pass to handler
110  */
111  Handler( classT* const object, PtrMember member, argT data ) :
112  object(object),
113  member(member),
114  data(data)
115  {}
116 
117  void operator()(Event* event) {
118  (object->*member)(event,data);
119  }
120  };
121 
122 
123  /**
124  * Event Handler class with no user-data.
125  */
126  template <typename classT>
127  class Handler<classT, void> : public HandlerBase {
128  private:
129  typedef void (classT::*PtrMember)(Event*);
130  const PtrMember member;
131  classT* object;
132 
133  public:
134  /** Constructor
135  * @param object - Pointer to Object upon which to call the handler
136  * @param member - Member function to call as the handler
137  */
138  Handler( classT* const object, PtrMember member ) :
139  member(member),
140  object(object)
141  {}
142 
143  void operator()(Event* event) {
144  (object->*member)(event);
145  }
146  };
147 
148  /** Virtual function to "pretty-print" this event. Should be implemented by subclasses. */
149  virtual void print(const std::string& header, Output &out) const override {
150  out.output("%s Generic Event to be delivered at %" PRIu64 " with priority %d\n",
151  header.c_str(), getDeliveryTime(), getPriority());
152  }
153 
154 #ifdef __SST_DEBUG_EVENT_TRACKING__
155 
156  virtual void printTrackingInfo(const std::string& header, Output &out) const {
157  out.output("%s Event first sent from: %s:%s (type: %s) and last received by %s:%s (type: %s)\n", header.c_str(),
158  first_comp.c_str(),first_port.c_str(),first_type.c_str(),
159  last_comp.c_str(),last_port.c_str(),last_type.c_str());
160  }
161 
162  const std::string& getFirstComponentName() { return first_comp; }
163  const std::string& getFirstComponentType() { return first_type; }
164  const std::string& getFirstPort() { return first_port; }
165  const std::string& getLastComponentName() { return last_comp; }
166  const std::string& getLastComponentType() { return last_type; }
167  const std::string& getLastPort() { return last_port; }
168 
169  void addSendComponent(const std::string& comp, const std::string& type, const std::string& port) {
170  if ( first_comp == "" ) {
171  first_comp = comp;
172  first_type = type;
173  first_port = port;
174  }
175  }
176  void addRecvComponent(const std::string& comp, const std::string& type, const std::string& port) {
177  last_comp = comp;
178  last_type = type;
179  last_port = port;
180  }
181 
182 #endif
183 
184  void serialize_order(SST::Core::Serialization::serializer &ser) override{
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 
200 protected:
201  /** Link used for delivery */
203 
204  /**
205  * Generates an ID that is unique across ranks, components and events.
206  */
207  id_type generateUniqueId();
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 };
227 
228 
229 /**
230  * Null Event. Does nothing.
231  */
233 public:
234  NullEvent() : Event() {}
235  ~NullEvent() {}
236 
237  void execute(void) override;
238 
239  virtual void print(const std::string& header, Output &out) const override {
240  out.output("%s NullEvent to be delivered at %" PRIu64 " with priority %d\n",
241  header.c_str(), getDeliveryTime(), getPriority());
242  }
243 
244 
245 
246 private:
247  ImplementSerializable(SST::NullEvent)
248 
249 };
250 } //namespace SST
251 
252 #endif // SST_CORE_EVENT_H
Output object provides consistant method for outputing 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
int getPriority() const
Return the Priority of this Activity.
Definition: activity.h:212
void operator()(Event *event)
Handler function.
Definition: event.h:143
Base class for all Activities in the SST Event Queue.
Definition: activity.h:54
Handler(classT *const object, PtrMember member)
Constructor.
Definition: event.h:138
void operator()(Event *event)
Handler function.
Definition: event.h:117
virtual void operator()(Event *)=0
Handler function.
LinkId_t getLinkId(void) const
Gets the link id associated with this event.
Definition: event.h:76
Null Event.
Definition: event.h:232
Definition: action.cc:17
Link * delivery_link
Link used for delivery.
Definition: event.h:202
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:66
Event Handler class with user-data argument.
Definition: event.h:98
std::pair< uint64_t, int > id_type
Type definition of unique identifiers.
Definition: event.h:35
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:184
void setPriority(int priority)
Set the priority of the Activity.
Definition: activity.h:340
static const id_type NO_ID
Constatn, 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:207
void setDeliveryLink(LinkId_t id, Link *link)
Sets the link id used for delivery.
Definition: event.h:56
virtual void print(const std::string &header, Output &out) const override
Virtual function to "pretty-print" this event.
Definition: event.h:239
void setRemoteEvent()
For use by SST Core only.
Definition: event.h:71
Functor classes for Event handling.
Definition: event.h:86
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 "pretty-print" this event.
Definition: event.h:149
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:111
virtual Event * clone()
Clones the event in for the case of a broadcast.
Definition: event.cc:33
Definition: serializable.h:130