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); }
225 SST::Shared::SharedObject::serialize_order(ser);
227 switch ( ser.mode() ) {
228 case SST::Core::Serialization::serializer::SIZER:
229 case SST::Core::Serialization::serializer::PACK:
231 std::string name = data->
getName();
235 case SST::Core::Serialization::serializer::UNPACK:
239 data = manager.getSharedObjectData<Data>(name);
256 std::map<keyT, valT> map;
257 ChangeSet* change_set;
261 Data(
const std::string& name) :
SharedObjectData(name), change_set(
nullptr), verify(VERIFY_UNINITIALIZED)
263 if ( Private::getNumRanks().rank > 1 ) { change_set =
new ChangeSet(name); }
268 if ( change_set )
delete change_set;
273 if ( v_type != verify && verify != VERIFY_UNINITIALIZED ) {
274 Private::getSimulationOutput().fatal(
275 CALL_INFO, 1,
"ERROR: Two different verify_types specified for SharedMap %s\n", name.c_str());
278 if ( change_set ) change_set->setVerify(v_type);
281 size_t getSize()
const {
return map.size(); }
283 void update_write(
const keyT& key,
const valT& value)
288 auto success = map.insert(std::make_pair(key, value));
289 if ( !success.second ) {
291 if ( verify != NO_VERIFY && value != success.first->second ) {
292 Private::getSimulationOutput().fatal(
293 CALL_INFO, 1,
"ERROR: wrote two different values to same key in SharedMap %s\n", name.c_str());
298 void write(
const keyT& key,
const valT& value)
300 std::lock_guard<std::mutex>
lock(mtx);
301 check_lock_for_write(
"SharedMap");
302 update_write(key, value);
303 if ( change_set ) change_set->addChange(key, value);
312 inline const valT& read(
const keyT& key) {
return map.at(key); }
315 inline const valT& mutex_read(
const keyT& key)
317 std::lock_guard<std::mutex>
lock(mtx);
318 auto ret = map.at(key);
328 SharedObjectData::serialize_order(ser);
338 std::map<keyT, valT> changes;
343 SharedObjectChangeSet::serialize_order(ser);
352 ChangeSet() : SharedObjectChangeSet(), verify(VERIFY_UNINITIALIZED) {}
353 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name), verify(VERIFY_UNINITIALIZED) {}
355 void addChange(
const keyT& key,
const valT& value) { changes[key] = value; }
357 void setVerify(
verify_type v_type) { verify = v_type; }
359 void applyChanges(SharedObjectDataManager* manager)
override 361 auto data = manager->getSharedObjectData<Data>(
getName());
362 data->setVerify(verify);
363 for (
auto x : changes ) {
364 data->update_write(x.first, x.second);
368 void clear()
override { changes.clear(); }
376 #endif // SST_CORE_SHARED_SHAREDMAP_H 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 & mutex_read(const keyT &key) const
Read data from the map.
Definition: sharedMap.h:221
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
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
Definition: sharedObject.h:269
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedMap.h:116
void lock()
Called by the core when writing to shared regions is no longer allowed.
Definition: sharedObject.h:186
bool isFullyPublished()
Checks to see if all instances of this SharedObject have called publish().
Definition: sharedObject.h:109
bool isFullyPublished()
Check whether all instances of this SharedMap have called publish().
Definition: sharedMap.h:169
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedMap.h:121
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
verify_type
Enum of verify types.
Definition: sharedObject.h:276
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedMap.h:324
Definition: sharedMap.h:249
size_t count(const keyT &k) const
Counts elements with a specific key.
Definition: sharedMap.h:102
size_t size() const
Get the size of the map.
Definition: sharedMap.h:87
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedMap.h:131
void write(const keyT &key, const valT &value)
Write data to the map.
Definition: sharedMap.h:179
const valT & operator[](const keyT &key) const
Read data from the map.
Definition: sharedMap.h:207
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedMap.h:126
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
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedMap.h:323
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedMap.h:155
bool empty() const
Tests if the map is empty.
Definition: sharedMap.h:94
const std::string & getName()
Get the name of the SharedObject for this data.
Definition: sharedObject.h:99
virtual void applyChanges(SharedObjectDataManager *UNUSED(manager))=0
Apply the changes to the name shared data.
This is the base class for holding data on changes made to the shared data on each rank...
Definition: sharedObject.h:44
Base class for holding SharedObject data.
Definition: sharedObject.h:87