12#ifndef SST_CORE_SHARED_SHAREDSET_H
13#define SST_CORE_SHARED_SHAREDSET_H
15#include "sst/core/shared/sharedObject.h"
16#include "sst/core/sst_types.h"
23namespace SST::Shared {
29template <
typename valT>
30class SharedSet :
public SharedObject
32 static_assert(!std::is_pointer_v<valT>,
"Cannot use a pointer type as value with SharedSet");
74 Private::getSimulationOutput().fatal(
75 CALL_INFO, 1,
"ERROR: called initialize() of SharedSet %s more than once\n", obj_name.c_str());
78 data = manager.getSharedObjectData<
Data>(obj_name);
79 int ret = incShareCount(data);
80 data->setVerify(v_type);
85 using const_iterator =
typename std::set<valT>::const_iterator;
86 using const_reverse_iterator =
typename std::set<valT>::const_reverse_iterator;
93 inline size_t size()
const {
return data->getSize(); }
100 inline bool empty()
const {
return data->set.empty(); }
108 size_t count(
const valT& k)
const {
return data->set.count(k); }
113 const_iterator
begin()
const {
return data->set.cbegin(); }
118 const_iterator
end()
const {
return data->set.cend(); }
123 const_reverse_iterator
rbegin()
const {
return data->set.crbegin(); }
128 const_reverse_iterator
rend()
const {
return data->set.crend(); }
137 if ( published )
return;
139 incPublishCount(data);
160 Private::getSimulationOutput().fatal(
161 CALL_INFO, 1,
"ERROR: insert into SharedSet %s after publish() was called\n", data->getName().c_str());
163 return data->write(value);
183 inline const_iterator
find(
const valT& value)
const {
return data->find(value); }
195 inline const_iterator
mutex_find(
const valT& value)
const {
return data->mutex_find(value); }
197 void serialize_order(
serializer& ser)
override
199 SharedObject::serialize_order(ser);
201 const auto mode = ser.mode();
202 initialized = data !=
nullptr;
204 if ( mode == serializer::MAP )
207 SST_SER(published, SerOption::map_read_only);
208 SST_SER(initialized, SerOption::map_read_only);
211 case serializer::SIZER:
212 case serializer::PACK:
214 if ( !initialized )
return;
215 std::string name = data->
getName();
219 case serializer::UNPACK:
221 if ( !initialized )
return;
224 data = manager.getSharedObjectData<Data>(name);
227 case serializer::MAP:
228 if ( initialized ) SST_SER_NAME(data->set,
"data");
229 ser.mapper().map_hierarchy_end();
234 ImplementSerializable(SharedSet<valT>)
237 bool published =
false;
238 Data* data =
nullptr;
239 bool initialized =
false;
249 ChangeSet* change_set;
256 verify(VERIFY_UNINITIALIZED)
258 explicit Data(
const std::string& name) :
261 verify(VERIFY_UNINITIALIZED)
263 if ( Private::getNumRanks().rank > 1 ) {
264 change_set =
new ChangeSet(name);
270 if ( change_set )
delete change_set;
275 if ( v_type != verify && verify != VERIFY_UNINITIALIZED ) {
276 Private::getSimulationOutput().fatal(
277 CALL_INFO, 1,
"ERROR: Type different verify_types specified for SharedSet %s\n", name.c_str());
280 if ( change_set ) change_set->setVerify(v_type);
283 size_t getSize()
const
285 std::lock_guard<std::mutex>
lock(mtx);
289 void update_write(
const valT& value)
294 auto success = set.insert(value);
295 if ( !success.second ) {
297 if ( verify != NO_VERIFY && !(value == *(success.first)) ) {
298 Private::getSimulationOutput().fatal(CALL_INFO, 1,
299 "ERROR: wrote two non-equal values to same set item in SharedSet %s\n", name.c_str());
304 void write(
const valT& value)
306 std::lock_guard<std::mutex>
lock(mtx);
307 check_lock_for_write(
"SharedSet");
309 if ( change_set ) change_set->addChange(value);
318 inline const_iterator find(
const valT& value) {
return set.find(value); }
321 inline const_iterator mutex_find(
const valT& value)
323 std::lock_guard<std::mutex>
lock(mtx);
324 auto ret = set.find(value);
332 void serialize_order(
serializer& ser)
override
334 SharedObjectData::serialize_order(ser);
344 std::set<valT> changes;
347 void serialize_order(
serializer& ser)
override
349 SharedObjectChangeSet::serialize_order(ser);
354 ImplementSerializable(SharedSet<valT>::Data::ChangeSet);
359 SharedObjectChangeSet(),
360 verify(VERIFY_UNINITIALIZED)
362 explicit ChangeSet(
const std::string& name) :
363 SharedObjectChangeSet(name),
364 verify(VERIFY_UNINITIALIZED)
367 void addChange(
const valT& value) { changes.insert(value); }
369 void setVerify(
verify_type v_type) { verify = v_type; }
371 void applyChanges(SharedObjectDataManager* manager)
override
373 auto data = manager->getSharedObjectData<Data>(
getName());
374 data->setVerify(verify);
375 for (
auto x : changes ) {
376 data->update_write(x);
380 void clear()
override { changes.clear(); }
Class used to map containers.
Definition objectMap.h:1420
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
This is the base class for holding data on changes made to the shared data on each rank.
Definition sharedObject.h:52
const std::string & getName()
Get the name of the shared data the changeset should be applied to.
Definition sharedObject.h:81
virtual void clear()=0
Clears the data.
const std::string & getName()
Get the name of the SharedObject for this data.
Definition sharedObject.h:108
SharedObjectData(const std::string &name)
Constructor for SharedObjectData.
Definition sharedObject.h:202
void lock()
Called by the core when writing to shared regions is no longer allowed.
Definition sharedObject.h:195
verify_type
Enum of verify types.
Definition sharedObject.h:286
Definition sharedSet.h:242
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition sharedSet.h:330
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition sharedSet.h:329
const_iterator mutex_find(const valT &value) const
Searches the SharedSet for an element equivalent to value and returns a const iterator to it if found...
Definition sharedSet.h:195
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying set.
Definition sharedSet.h:123
const_iterator end() const
Get const_iterator to end of underlying set.
Definition sharedSet.h:118
int initialize(const std::string &obj_name, verify_type v_type=FE_VERIFY)
Initialize the SharedSet.
Definition sharedSet.h:71
const_iterator find(const valT &value) const
Searches the SharedSet for an element equivalent to value and returns a const iterator to it if found...
Definition sharedSet.h:183
bool isFullyPublished()
Check whether all instances of this SharedSet have called publish().
Definition sharedSet.h:149
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying set.
Definition sharedSet.h:128
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition sharedSet.h:135
void insert(const valT &value)
Insert data to the set.
Definition sharedSet.h:157
bool empty() const
Tests if the set is empty.
Definition sharedSet.h:100
const_iterator begin() const
Get const_iterator to beginning of underlying set.
Definition sharedSet.h:113
size_t count(const valT &k) const
Counts elements with a specific value.
Definition sharedSet.h:108
size_t size() const
Get the size of the set.
Definition sharedSet.h:93