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