12 #ifndef SST_CORE_SHARED_SHAREDMAP_H
13 #define SST_CORE_SHARED_SHAREDMAP_H
15 #include "sst/core/shared/sharedObject.h"
16 #include "sst/core/sst_types.h"
27 template <
typename keyT,
typename valT>
30 static_assert(!std::is_pointer<valT>::value,
"Cannot use a pointer type as value with SharedMap");
68 Private::getSimulationOutput().fatal(
69 CALL_INFO, 1,
"ERROR: called initialize() of SharedMap %s more than once\n", obj_name.c_str());
72 data = manager.getSharedObjectData<Data>(obj_name);
73 int ret = incShareCount(data);
74 data->setVerify(v_type);
79 typedef typename std::map<keyT, valT>::const_iterator const_iterator;
80 typedef typename std::map<keyT, valT>::const_reverse_iterator const_reverse_iterator;
87 inline size_t size()
const {
return data->getSize(); }
94 inline bool empty()
const {
return data->map.empty(); }
102 size_t count(
const keyT& k)
const {
return data->map.count(k); }
111 const_iterator
find(
const keyT& key)
const {
return data->map.find(key); }
116 const_iterator
begin()
const {
return data->map.cbegin(); }
121 const_iterator
end()
const {
return data->map.cend(); }
126 const_reverse_iterator
rbegin()
const {
return data->map.crbegin(); }
131 const_reverse_iterator
rend()
const {
return data->map.crend(); }
140 inline const_iterator
lower_bound(
const keyT& key)
const {
return data->map.lower_bound(key); }
148 inline const_iterator
upper_bound(
const keyT& key)
const {
return data->map.lower_bound(key); }
157 if ( published )
return;
159 incPublishCount(data);
179 inline void write(
const keyT& key,
const valT& value)
182 Private::getSimulationOutput().fatal(
183 CALL_INFO, 1,
"ERROR: write to SharedMap %s after publish() was called\n", data->getName().c_str());
185 return data->write(key, value);
207 inline const valT&
operator[](
const keyT& key)
const {
return data->read(key); }
221 inline const valT&
mutex_read(
const keyT& key)
const {
return data->mutex_read(key); }
234 std::map<keyT, valT> map;
235 ChangeSet* change_set;
238 Data(
const std::string& name) :
SharedObjectData(name), change_set(nullptr), verify(VERIFY_UNINITIALIZED)
240 if ( Private::getSimulation()->getNumRanks().rank > 1 ) { change_set =
new ChangeSet(name); }
243 ~Data() {
delete change_set; }
247 if ( v_type != verify && verify != VERIFY_UNINITIALIZED ) {
248 Private::getSimulationOutput().fatal(
249 CALL_INFO, 1,
"ERROR: Two different verify_types specified for SharedMap %s\n", name.c_str());
252 if ( change_set ) change_set->setVerify(v_type);
255 size_t getSize()
const {
return map.size(); }
257 void update_write(
const keyT& key,
const valT& value)
262 auto success = map.insert(std::make_pair(key, value));
263 if ( !success.second ) {
265 if ( verify != NO_VERIFY && value != success.first->second ) {
266 Private::getSimulationOutput().fatal(
267 CALL_INFO, 1,
"ERROR: wrote two different values to same key in SharedMap %s\n", name.c_str());
272 void write(
const keyT& key,
const valT& value)
274 std::lock_guard<std::mutex>
lock(mtx);
275 check_lock_for_write(
"SharedMap");
276 update_write(key, value);
277 if ( change_set ) change_set->addChange(key, value);
286 inline const valT& read(
const keyT& key) {
return map.at(key); }
289 inline const valT&
mutex_read(
const keyT& key)
291 std::lock_guard<std::mutex>
lock(mtx);
292 auto ret = map.at(key);
297 virtual SharedObjectChangeSet* getChangeSet()
override {
return change_set; }
298 virtual void resetChangeSet()
override { change_set->clear(); }
301 class ChangeSet :
public SharedObjectChangeSet
304 std::map<keyT, valT> changes;
309 SharedObjectChangeSet::serialize_order(ser);
318 ChangeSet() : SharedObjectChangeSet(), verify(VERIFY_UNINITIALIZED) {}
319 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name), verify(VERIFY_UNINITIALIZED) {}
321 void addChange(
const keyT& key,
const valT& value) { changes[key] = value; }
323 void setVerify(
verify_type v_type) { verify = v_type; }
325 void applyChanges(SharedObjectDataManager* manager)
override
327 auto data = manager->getSharedObjectData<Data>(
getName());
328 data->setVerify(verify);
329 for (
auto x : changes ) {
330 data->update_write(x.first, x.second);
334 void clear()
override { changes.clear(); }
342 #endif // SST_CORE_SHARED_SHAREDMAP_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:34
Definition: sharedObject.h:259
bool empty() const
Tests if the map is empty.
Definition: sharedMap.h:94
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedMap.h:126
void lock()
Called by the core when writing to shared regions is no longer allowed.
Definition: sharedObject.h:185
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedMap.h:116
const std::string & getName()
Get the name of the shared data the changeset should be applied to.
Definition: sharedObject.h:71
bool isFullyPublished()
Check whether all instances of this SharedMap have called publish().
Definition: sharedMap.h:169
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedMap.h:131
int initialize(const std::string &obj_name, verify_type v_type=FE_VERIFY)
Initialize the SharedMap.
Definition: sharedMap.h:65
SharedMap class.
Definition: sharedMap.h:28
const valT & mutex_read(const keyT &key) const
Read data from the map.
Definition: sharedMap.h:221
verify_type
Enum of verify types.
Definition: sharedObject.h:266
const_iterator find(const keyT &key) const
Searches the container for an element with a key equivalent to k and returns an iterator to it if fou...
Definition: sharedMap.h:111
size_t size() const
Get the size of the map.
Definition: sharedMap.h:87
void write(const keyT &key, const valT &value)
Write data to the map.
Definition: sharedMap.h:179
size_t count(const keyT &k) const
Counts elements with a specific key.
Definition: sharedMap.h:102
const_iterator lower_bound(const keyT &key) const
Returns an iterator pointing to the first element in the container whose key is not considered to go ...
Definition: sharedMap.h:140
const valT & operator[](const keyT &key) const
Read data from the map.
Definition: sharedMap.h:207
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedMap.h:155
virtual void applyChanges(SharedObjectDataManager *UNUSED(manager))=0
Apply the changes to the name shared data.
const_iterator upper_bound(const keyT &key) const
Returns an iterator pointing to the first element in the container whose key is considered to go afte...
Definition: sharedMap.h:148
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedMap.h:121
Base class for holding SharedObject data.
Definition: sharedObject.h:86