SST  15.1.0
StructuralSimulationToolkit
exit.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_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 <string>
21 #include <unordered_set>
22 
23 namespace SST {
24 
25 class Simulation;
26 
27 /**
28  Exit Action
29 
30  Tracks the number of components that have requested DoNotEndSim.
31  Once that count reaches 0, the Exit object will end the simulation.
32  */
33 class Exit : public Action
34 {
35 public:
36  /**
37  Create a new ExitEvent
38 
39  @param num_threads number of threads on the rank
40  @param single_rank True if there are no other ranks
41  */
42  Exit(int num_threads, bool single_rank);
43 
44  ~Exit();
45 
46  /**
47  Increment reference count on a given thread
48 
49  @param thread number of Component making call
50  */
51  void refInc(uint32_t thread);
52 
53  /**
54  Decrement reference count on a given thread
55 
56  @param thread number of Component making call
57  */
58  void refDec(uint32_t thread);
59 
60  /**
61  Get the current local reference count
62 
63  @return current local reference count
64  */
65  unsigned int getRefCount();
66 
67  /**
68  Gets the end time of the simulation
69 
70  @return Time when simulation ends
71  */
72  SimTime_t getEndTime() { return end_time_; }
73 
74  /**
75  Stores the time the simulation has ended
76 
77  @param time Current simulation time
78  */
79  void setEndTime(SimTime_t time) { end_time_ = time; }
80 
81  /**
82  Computes the end time of the simulation. This call
83  synchronizes across ranks.
84 
85  @return End time of the simulation
86  */
87  SimTime_t computeEndTime();
88 
89  /**
90  Function called when Exit fires in the TimeVortex
91  */
92  void execute() override;
93 
94  /**
95  Function called by SyncManager to check to see if it's time to
96  exit. This call synchronizes across ranks.
97  */
98  void check();
99 
100  /**
101  Creates a string representation of the Exit object
102 
103  @return string representation of Exit object
104  */
105  std::string toString() const override;
106 
107  /**
108  Get the global ref_count. This is only valid after check() is
109  called
110 
111  @return global ref_count
112  */
113  unsigned int getGlobalCount() { return global_count_; }
114 
115  // Exit should not be serialized. It will be created new on
116  // restart and Components store there primary component state and
117  // reregister with Exit on restart.
118  NotSerializable(SST::Exit)
119 
120 private:
121  Exit() = delete;
122  Exit(const Exit&) = delete;
123  Exit& operator=(const Exit&) = delete;
124 
125  int num_threads_ = 0;
126  unsigned int ref_count_ = 0;
127  unsigned int* thread_counts_ = nullptr;
128  unsigned int global_count_ = 0;
129  SimTime_t end_time_ = 0;
130  bool single_rank_ = true;
131 
133 };
134 
135 } // namespace SST
136 
137 #endif // SST_CORE_EXIT_H
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
unsigned int getRefCount()
Get the current local reference count.
Definition: exit.cc:82
void refInc(uint32_t thread)
Increment reference count on a given thread.
Definition: exit.cc:46
Definition: action.cc:18
Exit Action.
Definition: exit.h:33
void setEndTime(SimTime_t time)
Stores the time the simulation has ended.
Definition: exit.h:79
unsigned int getGlobalCount()
Get the global ref_count.
Definition: exit.h:113
void refDec(uint32_t thread)
Decrement reference count on a given thread.
Definition: exit.cc:54
void check()
Function called by SyncManager to check to see if it&#39;s time to exit.
Definition: exit.cc:112
Definition: threadsafe.h:135
Exit(int num_threads, bool single_rank)
Create a new ExitEvent.
Definition: exit.cc:28
SimTime_t getEndTime()
Gets the end time of the simulation.
Definition: exit.h:72
SimTime_t computeEndTime()
Computes the end time of the simulation.
Definition: exit.cc:95
std::string toString() const override
Creates a string representation of the Exit object.
Definition: exit.cc:135
void execute() override
Function called when Exit fires in the TimeVortex.
Definition: exit.cc:88