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