SST  14.1.0
StructuralSimulationToolkit
activity.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_ACTIVITY_H
13 #define SST_CORE_ACTIVITY_H
14 
15 #include "sst/core/mempool.h"
16 #include "sst/core/output.h"
17 #include "sst/core/serialization/serializable.h"
18 #include "sst/core/sst_types.h"
19 #include "sst/core/warnmacros.h"
20 
21 #include <cinttypes>
22 #include <cstring>
23 #include <errno.h>
24 #include <sstream>
25 #include <unordered_map>
26 
27 // Default Priority Settings
28 #define INTERACTIVEPRIOIRTY 0
29 #define THREADSYNCPRIORITY 20
30 #define SYNCPRIORITY 25
31 #define STOPACTIONPRIORITY 30
32 #define CLOCKPRIORITY 40
33 #define EVENTPRIORITY 50
34 #define MEMEVENTPRIORITY 50
35 #define BARRIERPRIORITY 75
36 #define ONESHOTPRIORITY 80
37 #define STATISTICCLOCKPRIORITY 85
38 #define FINALEVENTPRIORITY 98
39 #define EXITPRIORITY 99
40 
41 extern int main(int argc, char** argv);
42 
43 namespace SST {
44 
45 /** Base class for all Activities in the SST Event Queue */
47 {
48 public:
49  Activity() : delivery_time(0), priority_order(0), queue_order(0) {}
50  virtual ~Activity() {}
51 
52  /**
53  Class to use as the less than operator for STL functions or
54  sorting algorithms. If a template parameter is set to true,
55  then that variable will be included in the comparison. The
56  parameters are: T - delivery time, P - priority and order tag,
57  Q - queue order.
58  */
59  template <bool T, bool P, bool Q>
60  class less
61  {
62  public:
63  inline bool operator()(const Activity* lhs, const Activity* rhs) const
64  {
65  if ( T && lhs->delivery_time != rhs->delivery_time ) return lhs->delivery_time < rhs->delivery_time;
66  if ( P && lhs->priority_order != rhs->priority_order ) return lhs->priority_order < rhs->priority_order;
67  return Q && lhs->queue_order < rhs->queue_order;
68  }
69 
70  // // Version without branching. Still need to test to see if
71  // // this is faster than the above implementation for "real"
72  // // simulations. For the sst-benchmark, the above is slightly
73  // // faster. Uses the bitwise operator because there are no
74  // // early outs. For bools, this is logically equivalent to the
75  // // logical operators
76  // inline bool operator()(const Activity* lhs, const Activity* rhs) const
77  // {
78  // return ( T & lhs->delivery_time < rhs->delivery_time ) |
79  // (P & lhs->delivery_time == rhs->delivery_time & lhs->priority_order < rhs->priority_order) |
80  // (Q & lhs->delivery_time == rhs->delivery_time & lhs->priority_order == rhs->priority_order &
81  // lhs->queue_order < rhs->queue_order);
82  // }
83 
84  // // Version without ifs, but with early outs in the logic.
85  // inline bool operator()(const Activity* lhs, const Activity* rhs) const
86  // {
87  // return ( T && lhs->delivery_time < rhs->delivery_time ) |
88  // (P && lhs->delivery_time == rhs->delivery_time && lhs->priority_order < rhs->priority_order) |
89  // (Q && lhs->delivery_time == rhs->delivery_time && lhs->priority_order == rhs->priority_order &&
90  // lhs->queue_order < rhs->queue_order);
91  // }
92  };
93 
94  /**
95  Class to use as the greater than operator for STL functions or
96  sorting algorithms (used if you want to sort opposite the
97  natural soring order). If a template parameter is set to true,
98  then that variable will be included in the comparison. The
99  parameters are: T - delivery time, P - priority and order tag,
100  Q - queue order.
101  */
102  template <bool T, bool P, bool Q>
103  class greater
104  {
105  public:
106  inline bool operator()(const Activity* lhs, const Activity* rhs) const
107  {
108  if ( T && lhs->delivery_time != rhs->delivery_time ) return lhs->delivery_time > rhs->delivery_time;
109  if ( P && lhs->priority_order != rhs->priority_order ) return lhs->priority_order > rhs->priority_order;
110  return Q && lhs->queue_order > rhs->queue_order;
111  }
112 
113  // // Version without branching. Still need to test to see if
114  // // this is faster than the above implementation for "real"
115  // // simulations. For the sst-benchmark, the above is slightly
116  // // faster. Uses the bitwise operator because there are no
117  // // early outs. For bools, this is logically equivalent to the
118  // // logical operators
119  // inline bool operator()(const Activity* lhs, const Activity* rhs) const
120  // {
121  // return ( T & lhs->delivery_time > rhs->delivery_time ) |
122  // (P & lhs->delivery_time == rhs->delivery_time & lhs->priority_order > rhs->priority_order) |
123  // (Q & lhs->delivery_time == rhs->delivery_time & lhs->priority_order == rhs->priority_order &
124  // lhs->queue_order > rhs->queue_order);
125  // }
126 
127  // // Version without ifs, but with early outs in the logic.
128  // inline bool operator()(const Activity* lhs, const Activity* rhs) const
129  // {
130  // return ( T && lhs->delivery_time > rhs->delivery_time ) |
131  // (P && lhs->delivery_time == rhs->delivery_time && lhs->priority_order > rhs->priority_order) |
132  // (Q && lhs->delivery_time == rhs->delivery_time && lhs->priority_order == rhs->priority_order &&
133  // lhs->queue_order > rhs->queue_order);
134  // }
135  };
136 
137 
138  /** Function which will be called when the time for this Activity comes to pass. */
139  virtual void execute(void) = 0;
140 
141  /** Set the time for which this Activity should be delivered */
142  inline void setDeliveryTime(SimTime_t time) { delivery_time = time; }
143 
144  /** Return the time at which this Activity will be delivered */
145  inline SimTime_t getDeliveryTime() const { return delivery_time; }
146 
147  /** Return the Priority of this Activity */
148  inline int getPriority() const { return (int)(priority_order >> 32); }
149 
150  /** Sets the order tag */
151  inline void setOrderTag(uint32_t tag) { priority_order = (priority_order & 0xFFFFFFFF00000000ul) | (uint64_t)tag; }
152 
153  /** Return the order tag associated with this activity */
154  inline uint32_t getOrderTag() const { return (uint32_t)(priority_order & 0xFFFFFFFFul); }
155 
156  /** Returns the queue order associated with this activity */
157  inline uint64_t getQueueOrder() const { return queue_order; }
158 
159  virtual bool isEvent() { return false; }
160  virtual bool isAction() { return false; }
161 
162  /** Get a string represenation of the event. The default version
163  * will just use the name of the class, retrieved through the
164  * cls_name() function inherited from the serialzable class, which
165  * will return the name of the last class to call one of the
166  * serialization macros (ImplementSerializable(),
167  * ImplementVirtualSerializable(), or NotSerializable()).
168  * Subclasses can override this function if they want to add
169  * additional information.
170  */
171  std::string toString() const override
172  {
173  std::stringstream buf;
174 
175  buf << cls_name() << " to be delivered at " << getDeliveryTimeInfo();
176  return buf.str();
177  }
178 
179 #ifdef __SST_DEBUG_EVENT_TRACKING__
180  virtual void printTrackingInfo(const std::string& UNUSED(header), Output& UNUSED(out)) const {}
181 #endif
182 
183 protected:
184  /** Set the priority of the Activity */
185  void setPriority(uint64_t priority) { priority_order = (priority_order & 0x00000000FFFFFFFFul) | (priority << 32); }
186 
187  /**
188  Gets the delivery time info as a string. To be used in
189  inherited classes if they'd like to overwrite the default print
190  or toString()
191  */
192  std::string getDeliveryTimeInfo() const
193  {
194  std::stringstream buf;
195  buf << "time: " << delivery_time << ", priority: " << getPriority() << ", order tag: " << getOrderTag()
196  << ", queue order: " << getQueueOrder();
197  return buf.str();
198  }
199 
200  // Function used by derived classes to serialize data members.
201  // This class is not serializable, because not all class that
202  // inherit from it need to be serializable.
203  void serialize_order(SST::Core::Serialization::serializer& ser) override
204  {
205  ser& delivery_time;
206  ser& priority_order;
207  ser& queue_order;
208  }
210 
211 
212  /** Set a new Queue order */
213  void setQueueOrder(uint64_t order)
214  {
215  queue_order = order;
216  }
217 
218 private:
219  // Data members
220  SimTime_t delivery_time;
221  // This will hold both the priority (high bits) and the link order
222  // (low_bits)
223  uint64_t priority_order;
224  // Used for TimeVortex implementations that don't naturally keep
225  // the insertion order
226  uint64_t queue_order;
227 };
228 
229 } // namespace SST
230 
231 #endif // SST_CORE_ACTIVITY_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:53
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:43
int getPriority() const
Return the Priority of this Activity.
Definition: activity.h:148
void setDeliveryTime(SimTime_t time)
Set the time for which this Activity should be delivered.
Definition: activity.h:142
uint64_t getQueueOrder() const
Returns the queue order associated with this activity.
Definition: activity.h:157
Base class for all Activities in the SST Event Queue.
Definition: activity.h:46
void setOrderTag(uint32_t tag)
Sets the order tag.
Definition: activity.h:151
virtual void execute(void)=0
Function which will be called when the time for this Activity comes to pass.
void setPriority(uint64_t priority)
Set the priority of the Activity.
Definition: activity.h:185
Definition: action.cc:18
SimTime_t getDeliveryTime() const
Return the time at which this Activity will be delivered.
Definition: activity.h:145
ImplementVirtualSerializable(SST::Activity) void setQueueOrder(uint64_t order)
Set a new Queue order.
Definition: activity.h:209
std::string toString() const override
Get a string represenation of the event.
Definition: activity.h:171
Class to use as the greater than operator for STL functions or sorting algorithms (used if you want t...
Definition: activity.h:103
Definition: mempool.h:27
Class to use as the less than operator for STL functions or sorting algorithms.
Definition: activity.h:60
std::string getDeliveryTimeInfo() const
Gets the delivery time info as a string.
Definition: activity.h:192
uint32_t getOrderTag() const
Return the order tag associated with this activity.
Definition: activity.h:154