SST 15.0
Structural Simulation Toolkit
timeVortexBinnedMap.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_IMPL_TIMEVORTEX_TIMEVORTEXBINNEDMAP_H
13#define SST_CORE_IMPL_TIMEVORTEX_TIMEVORTEXBINNEDMAP_H
14
15#include "sst/core/threadsafe.h"
16
17#include <atomic>
18#include <queue>
19#include <sst/core/timeVortex.h>
20#include <vector>
21
22namespace SST {
23
24class Output;
25
26namespace IMPL {
27
28
29template <typename T>
30class Pool
31{
32
33 std::vector<T*> pool;
34
35public:
36 ~Pool()
37 {
38 for ( auto x : pool ) {
39 delete x;
40 }
41 }
42
43 T* remove()
44 {
45 if ( pool.empty() ) {
46 pool.push_back(new T());
47 }
48 auto ret = pool.back();
49 pool.pop_back();
50 return ret;
51 }
52
53 void insert(T* item) { pool.push_back(item); }
54};
55
56/**
57 * Primary Event Queue
58 */
59template <bool TS>
60class TimeVortexBinnedMapBase : public TimeVortex
61{
62
63private:
64 // Class to hold a vector and a delivery time
65 class TimeUnit
66 {
67 private:
68 SimTime_t sort_time;
69 std::vector<Activity*> activities;
70 bool sorted;
71
72 CACHE_ALIGNED(SST::Core::ThreadSafe::Spinlock, tu_lock);
73
74 public:
75 // Create with initial event
76 TimeUnit() :
77 sorted(false)
78 {}
79
80 ~TimeUnit()
81 {
82 for ( auto x : activities ) {
83 delete x;
84 }
85 }
86
87 inline SimTime_t getSortTime() { return sort_time; }
88 inline void setSortTime(SimTime_t time) { sort_time = time; }
89
90
91 // Inserts can happen by multiple threads
92 void insert(Activity* act)
93 {
94 if ( TS ) tu_lock.lock();
95 activities.push_back(act);
96 sorted = false;
97 if ( TS ) tu_lock.unlock();
98 }
99
100 // pop only happens by one thread
101 Activity* pop()
102 {
103 if ( 0 == activities.size() ) return nullptr;
104 if ( !sorted ) sort();
105 auto ret = activities.back();
106 activities.pop_back();
107 return ret;
108 }
109
110 // front only happens by one thread
111 Activity* front()
112 {
113 if ( 0 == activities.size() ) return nullptr;
114 if ( !sorted ) sort();
115 return activities.back();
116 }
117
118 void sort();
119
120 inline bool operator<(const TimeUnit& rhs) { return this->sort_time < rhs.sort_time; }
121
122 /** To use with STL priority queues, that order in reverse. */
124 {
125 public:
126 /** Compare pointers */
127 inline bool operator()(const TimeUnit* lhs, const TimeUnit* rhs) { return lhs->sort_time > rhs->sort_time; }
128
129 /** Compare references */
130 inline bool operator()(const TimeUnit& lhs, const TimeUnit& rhs) { return lhs.sort_time > rhs.sort_time; }
131 };
132 };
133
134
135public:
136 explicit TimeVortexBinnedMapBase(Params& params);
138
139 bool empty() override;
140 int size() override;
141 void insert(Activity* activity) override;
142 Activity* pop() override;
143 Activity* front() override;
144
145
146 /** Print the state of the TimeVortex */
147 void print(Output& out) const override;
148
149 uint64_t getCurrentDepth() const override { return current_depth; }
150 uint64_t getMaxDepth() const override { return max_depth; }
151
152private:
153 // Should only ever be accessed by the "active" thread. Not safe
154 // for concurrent access.
155 TimeUnit* current_time_unit;
156
157 using mapType_t = std::map<SimTime_t, TimeUnit*>;
158
159 // Accessed by multiple threads, must be locked when accessing
160 mapType_t map;
161 std::conditional_t<TS, std::atomic<uint64_t>, uint64_t> insertOrder;
162 std::conditional_t<TS, std::atomic<uint64_t>, uint64_t> current_depth;
163
164 // Should only ever be accessed by the "active" thread, or in a
165 // mutex. There are no internal mutexes.
166 Pool<TimeUnit> pool;
167
168 CACHE_ALIGNED(SST::Core::ThreadSafe::Spinlock, slock);
169};
170
171} // namespace IMPL
172} // namespace SST
173
174#endif // SST_CORE_IMPL_TIMEVORTEX_TIMEVORTEXBINNEDMAP_H
Base class for all Activities in the SST Event Queue.
Definition activity.h:46
Definition threadsafe.h:132
Definition timeVortexBinnedMap.h:31
To use with STL priority queues, that order in reverse.
Definition timeVortexBinnedMap.h:124
bool operator()(const TimeUnit *lhs, const TimeUnit *rhs)
Compare pointers.
Definition timeVortexBinnedMap.h:127
bool operator()(const TimeUnit &lhs, const TimeUnit &rhs)
Compare references.
Definition timeVortexBinnedMap.h:130
Primary Event Queue.
Definition timeVortexBinnedMap.h:61
bool empty() override
Returns true if the queue is empty.
Definition timeVortexBinnedMap.cc:63
Activity * pop() override
Remove and return the next activity.
Definition timeVortexBinnedMap.cc:127
int size() override
Returns the number of activities in the queue.
Definition timeVortexBinnedMap.cc:73
Activity * front() override
Returns the next activity.
Definition timeVortexBinnedMap.cc:151
void print(Output &out) const override
Print the state of the TimeVortex.
Definition timeVortexBinnedMap.cc:166
void insert(Activity *activity) override
Insert a new activity into the queue.
Definition timeVortexBinnedMap.cc:80
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:54
Parameter store.
Definition params.h:58