SST  14.1.0
StructuralSimulationToolkit
exit.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_CORE_EXIT_H
13 #define SST_CORE_CORE_EXIT_H
14 
15 #include "sst/core/action.h"
16 #include "sst/core/sst_types.h"
17 #include "sst/core/threadsafe.h"
18 
19 #include <cinttypes>
20 #include <unordered_set>
21 
22 namespace SST {
23 
24 #define _EXIT_DBG(fmt, args...) __DBG(DBG_EXIT, Exit, fmt, ##args)
25 
26 class Simulation;
27 class TimeConverter;
28 
29 /**
30  * Exit Event Action
31  *
32  * Causes the simulation to halt
33  */
34 class Exit : public Action
35 {
36 public:
37  /**
38  * Create a new ExitEvent
39  * @param sim Simulation object
40  * @param single_rank True if there are no parallel ranks
41  *
42  * Exit needs to register a handler during constructor time, which
43  * requires a simulation object. But the simulation class creates
44  * an Exit object during it's construction, meaning that
45  * Simulation::getSimulation() won't work yet. So Exit is the one
46  * exception to the "constructors shouldn't take simulation
47  * pointers" rule. However, it still needs to follow the "classes
48  * shouldn't contain pointers back to Simulation" rule.
49  */
50  Exit(int num_threads, bool single_rank);
51  ~Exit();
52 
53  /** Increment Reference Count for a given Component ID */
54  bool refInc(ComponentId_t, uint32_t thread);
55  /** Decrement Reference Count for a given Component ID */
56  bool refDec(ComponentId_t, uint32_t thread);
57 
58  unsigned int getRefCount();
59 
60  /** Gets the end time of the simulation
61  * @return Time when simulation ends
62  */
63  SimTime_t getEndTime() { return end_time; }
64 
65  /** Stores the time the simulation has ended
66  * @param time Current simulation time
67  * @return void
68  */
69  void setEndTime(SimTime_t time) { end_time = time; }
70 
71  /** Computes the end time of the simulation
72  * @return End time of the simulation
73  */
74  SimTime_t computeEndTime();
75  void execute(void) override;
76  void check();
77 
78  /**
79  * @param header String to preface the exit action log
80  * @param out SST Output logger object
81  * @return void
82  */
83  void print(const std::string& header, Output& out) const override
84  {
85  out.output(
86  "%s Exit Action to be delivered at %" PRIu64 " with priority %d\n", header.c_str(), getDeliveryTime(),
87  getPriority());
88  }
89 
90 
91  unsigned int getGlobalCount() { return global_count; }
92 
93  /**
94  *
95  * TODO to enable different partitioning on restart, will need to associate m_thread_counts and
96  * m_idSet back to components so that a new Exit event can be generated on restart
97  */
99  ImplementSerializable(SST::Exit)
100 private:
101  Exit() {} // for serialization only
102  Exit(const Exit&); // Don't implement
103  void operator=(Exit const&); // Don't implement
104 
105  // bool handler( Event* );
106 
107  // EventHandler< Exit, bool, Event* >* m_functor;
108  int num_threads;
109  unsigned int m_refCount;
110  unsigned int* m_thread_counts;
111  unsigned int global_count;
112  std::unordered_set<ComponentId_t> m_idSet;
113  SimTime_t end_time;
114 
115  Core::ThreadSafe::Spinlock slock;
116 
117  bool single_rank;
118 };
119 
120 } // namespace SST
121 
122 #endif // SST_CORE_EXIT_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:53
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
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
bool refInc(ComponentId_t, uint32_t thread)
Increment Reference Count for a given Component ID.
Definition: exit.cc:52
bool refDec(ComponentId_t, uint32_t thread)
Decrement Reference Count for a given Component ID.
Definition: exit.cc:87
void print(const std::string &header, Output &out) const override
Definition: exit.h:83
Definition: action.cc:18
Exit Event Action.
Definition: exit.h:34
void setEndTime(SimTime_t time)
Stores the time the simulation has ended.
Definition: exit.h:69
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
SimTime_t getDeliveryTime() const
Return the time at which this Activity will be delivered.
Definition: activity.h:145
Exit(int num_threads, bool single_rank)
Create a new ExitEvent.
Definition: exit.cc:31
void serialize_order(SST::Core::Serialization::serializer &ser) override
TODO to enable different partitioning on restart, will need to associate m_thread_counts and m_idSet ...
Definition: exit.cc:188
void execute(void) override
Function which will be called when the time for this Activity comes to pass.
Definition: exit.cc:133
SimTime_t getEndTime()
Gets the end time of the simulation.
Definition: exit.h:63
SimTime_t computeEndTime()
Computes the end time of the simulation.
Definition: exit.cc:143