12 #ifndef SST_CORE_CORE_SHARED_SHAREDMAP_H
13 #define SST_CORE_CORE_SHARED_SHAREDMAP_H
15 #include "sst/core/sst_types.h"
16 #include "sst/core/shared/sharedObject.h"
27 template <
typename keyT,
typename valT>
29 static_assert(!std::is_pointer<valT>::value,
"Cannot use a pointer type as value with SharedMap");
70 CALL_INFO,1,
"ERROR: called initialize() of SharedMap %s more than once\n",obj_name.c_str());
73 data = manager.getSharedObjectData<Data>(obj_name);
74 int ret = incShareCount(data);
75 data->setVerify(v_type);
80 typedef typename std::map<keyT,valT>::const_iterator const_iterator;
81 typedef typename std::map<keyT,valT>::const_reverse_iterator const_reverse_iterator;
88 inline size_t size()
const {
return data->getSize(); }
95 inline bool empty()
const {
return data->map.empty(); }
104 size_t count (
const keyT& k)
const {
105 return data->map.count(k);
115 const_iterator
find (
const keyT& key)
const {
116 return data->map.find(key);
123 return data->map.cbegin();
129 const_iterator
end()
const {
130 return data->map.cend();
137 return data->map.crbegin();
143 const_reverse_iterator
rend()
const {
144 return data->map.crend();
155 return data->map.lower_bound(key);
165 return data->map.lower_bound(key);
174 if ( published )
return;
176 incPublishCount(data);
187 return data->isFullyPublished();
199 inline void write(
const keyT& key,
const valT& value) {
202 CALL_INFO,1,
"ERROR: write to SharedMap %s after publish() was called\n",
203 data->getName().c_str());
205 return data->write(key, value);
228 return data->read(key);
244 return data->mutex_read(key);
260 std::map<keyT,valT> map;
264 Data(
const std::string& name) :
267 verify(VERIFY_UNINITIALIZED)
279 if ( v_type != verify && verify != VERIFY_UNINITIALIZED ) {
281 CALL_INFO,1,
"ERROR: Two different verify_types specified for SharedMap %s\n",name.c_str());
284 if ( change_set ) change_set->setVerify(v_type);
287 size_t getSize()
const {
291 void update_write(
const keyT& key,
const valT& value) {
295 auto success = map.insert(std::make_pair(key,value));
296 if ( !success.second ) {
298 if ( verify != NO_VERIFY && value != success.first->second ) {
300 CALL_INFO, 1,
"ERROR: wrote two different values to same key in SharedMap %s\n",name.c_str());
305 void write(
const keyT& key,
const valT& value) {
306 std::lock_guard<std::mutex>
lock(mtx);
307 check_lock_for_write(
"SharedMap");
308 update_write(key,value);
309 if ( change_set ) change_set->addChange(key,value);
318 inline const valT& read(
const keyT& key) {
323 inline const valT&
mutex_read(
const keyT& key) {
324 std::lock_guard<std::mutex>
lock(mtx);
325 auto ret = map.at(key);
330 virtual SharedObjectChangeSet* getChangeSet()
override {
333 virtual void resetChangeSet()
override {
338 class ChangeSet :
public SharedObjectChangeSet {
340 std::map<keyT,valT> changes;
344 SharedObjectChangeSet::serialize_order(ser);
354 SharedObjectChangeSet(),
355 verify(VERIFY_UNINITIALIZED)
357 ChangeSet(
const std::string& name) :
358 SharedObjectChangeSet(name),
359 verify(VERIFY_UNINITIALIZED)
362 void addChange(
const keyT& key,
const valT& value) {
363 changes[key] = value;
370 void applyChanges(SharedObjectDataManager* manager)
override {
371 auto data = manager->getSharedObjectData<Data>(
getName());
372 data->setVerify(verify);
373 for (
auto x : changes ) {
374 data->update_write(x.first, x.second);
378 void clear()
override {
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Definition: sharedObject.h:263
static Simulation * getSimulation()
Return a pointer to the singleton instance of the Simulation.
Definition: simulation.cc:52
bool empty() const
Tests if the map is empty.
Definition: sharedMap.h:95
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedMap.h:136
Definition: sharedRegionImpl.h:31
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:122
const std::string & getName()
Get the name of the shared data the changeset should be applied to.
Definition: sharedObject.h:68
bool isFullyPublished()
Check whether all instances of this SharedMap have called publish().
Definition: sharedMap.h:186
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedMap.h:143
int initialize(const std::string &obj_name, verify_type v_type=FE_VERIFY)
Initialize the SharedMap.
Definition: sharedMap.h:67
SharedMap class.
Definition: sharedMap.h:28
const valT & mutex_read(const keyT &key) const
Read data from the map.
Definition: sharedMap.h:243
verify_type
Enum of verify types.
Definition: sharedObject.h:271
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:115
void fatal(uint32_t line, const char *file, const char *func, int exit_code, const char *format,...) const
Output the fatal message with formatting as specified by the format parameter.
Definition: output.cc:155
size_t size() const
Get the size of the map.
Definition: sharedMap.h:88
void write(const keyT &key, const valT &value)
Write data to the map.
Definition: sharedMap.h:199
size_t count(const keyT &k) const
Counts elements with a specific key.
Definition: sharedMap.h:104
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:154
const valT & operator[](const keyT &key) const
Read data from the map.
Definition: sharedMap.h:227
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedMap.h:173
virtual void applyChanges(SharedObjectDataManager *UNUSED(manager))=0
Apply the changes to the name shared data.
static Output & getSimulationOutput()
Return the base simulation Output class instance.
Definition: simulation.cc:70
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:164
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedMap.h:129
Base class for holding SharedObject data.
Definition: sharedObject.h:87