12 #ifndef SST_CORE_SHARED_SHAREDARRAY_H
13 #define SST_CORE_SHARED_SHAREDARRAY_H
15 #include "sst/core/shared/sharedObject.h"
16 #include "sst/core/sst_types.h"
30 static_assert(!std::is_pointer<T>::value,
"Cannot use a pointer type with SharedArray");
82 int initialize(
const std::string& obj_name,
size_t length = 0, T init_value = T(),
verify_type v_type = INIT_VERIFY)
86 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
89 if ( v_type == VERIFY_UNINITIALIZED ) {
92 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. "
93 "This is a reserved value and cannot be passed in here. \n",
97 data = manager.getSharedObjectData<Data>(obj_name);
98 int ret = incShareCount(data);
99 if ( length != 0 ) data->setSize(length, init_value, v_type);
105 typedef typename std::vector<T>::const_iterator const_iterator;
106 typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
113 inline size_t size()
const {
return data->getSize(); }
120 inline bool empty()
const {
return data->array.empty(); }
125 const_iterator
begin()
const {
return data->array.cbegin(); }
130 const_iterator
end()
const {
return data->array.cend(); }
135 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
140 const_reverse_iterator
rend()
const {
return data->array.crend(); }
149 if ( published )
return;
151 incPublishCount(data);
171 inline void write(
int index,
const T& value)
175 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->getName().c_str());
177 return data->write(index, value);
197 inline const T&
operator[](
int index)
const {
return data->read(index); }
208 inline const T&
mutex_read(
int index)
const {
return data->mutex_read(index); }
221 std::vector<T> array;
222 std::vector<bool> written;
227 Data(
const std::string& name) :
SharedObjectData(name), change_set(nullptr), verify(VERIFY_UNINITIALIZED)
232 ~Data() {
delete change_set; }
245 if ( v_type == VERIFY_UNINITIALIZED )
return;
246 std::lock_guard<std::mutex>
lock(mtx);
247 if ( size > array.size() ) {
249 array.resize(size, init_data);
250 if ( v_type == FE_VERIFY ) { written.resize(size); }
251 if ( change_set ) change_set->setSize(size, init_data, v_type);
256 if ( verify != VERIFY_UNINITIALIZED ) {
257 if ( init != init_data ) {
259 CALL_INFO, 1,
"ERROR: Two different init_data values passed into SharedArray %s\n",
263 if ( verify != v_type ) {
265 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
274 std::lock_guard<std::mutex>
lock(mtx);
278 void update_write(
int index,
const T& data)
286 check = written[index];
289 check = array[index] != init;
294 if ( check && (array[index] != data) ) {
296 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n", index,
300 if ( verify == FE_VERIFY ) written[index] =
true;
303 void write(
int index,
const T& data)
305 std::lock_guard<std::mutex>
lock(mtx);
306 check_lock_for_write(
"SharedArray");
307 update_write(index, data);
308 if ( verify == FE_VERIFY ) written[index] =
true;
309 if ( change_set ) change_set->addChange(index, data);
317 inline const T& read(
int index)
const {
return array[index]; }
322 std::lock_guard<std::mutex>
lock(mtx);
323 auto ret = array[index];
328 virtual SharedObjectChangeSet* getChangeSet()
override {
return change_set; }
329 virtual void resetChangeSet()
override { change_set->clear(); }
332 class ChangeSet :
public SharedObjectChangeSet
335 std::vector<std::pair<int, T>> changes;
342 SharedObjectChangeSet::serialize_order(ser);
353 ChangeSet() : SharedObjectChangeSet() {}
354 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name), size(0), verify(VERIFY_UNINITIALIZED) {}
356 void addChange(
int index,
const T& value) { changes.emplace_back(index, value); }
358 void setSize(
size_t length,
const T& init_data,
verify_type v_type)
364 size_t getSize() {
return size; }
366 void applyChanges(SharedObjectDataManager* manager)
override
368 auto data = manager->getSharedObjectData<Data>(
getName());
369 data->setSize(size, init, verify);
370 for (
auto x : changes ) {
371 data->update_write(x.first, x.second);
375 void clear()
override { changes.clear(); }
438 const std::string& obj_name,
size_t length = 0,
bool init_value =
false,
verify_type v_type = INIT_VERIFY)
442 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
445 if ( v_type == VERIFY_UNINITIALIZED ) {
448 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. "
449 "This is a reserved value and cannot be passed in here. \n",
453 data = manager.getSharedObjectData<Data>(obj_name);
454 int ret = incShareCount(data);
455 if ( length != 0 ) data->setSize(length, init_value, v_type);
461 typedef typename std::vector<bool>::const_iterator const_iterator;
462 typedef typename std::vector<bool>::const_reverse_iterator const_reverse_iterator;
469 inline size_t size()
const {
return data->getSize(); }
476 inline bool empty()
const {
return data->array.empty(); }
481 const_iterator
begin()
const {
return data->array.cbegin(); }
486 const_iterator
end()
const {
return data->array.cend(); }
491 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
496 const_reverse_iterator
rend()
const {
return data->array.crend(); }
505 if ( published )
return;
507 incPublishCount(data);
527 inline void write(
int index,
bool value)
531 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->getName().c_str());
533 return data->write(index, value);
553 inline bool operator[](
int index)
const {
return data->read(index); }
564 inline bool mutex_read(
int index)
const {
return data->mutex_read(index); }
577 std::vector<bool> array;
578 std::vector<bool> written;
583 Data(
const std::string& name) :
SharedObjectData(name), change_set(nullptr), verify(VERIFY_UNINITIALIZED)
588 ~Data() {
delete change_set; }
598 void setSize(
size_t size,
bool init_data,
verify_type v_type)
601 if ( v_type == VERIFY_UNINITIALIZED )
return;
602 std::lock_guard<std::mutex>
lock(mtx);
603 if ( size > array.size() ) {
605 array.resize(size, init_data);
606 if ( v_type == FE_VERIFY ) { written.resize(size); }
607 if ( change_set ) change_set->setSize(size, init_data, v_type);
612 if ( verify != VERIFY_UNINITIALIZED ) {
613 if ( init != init_data ) {
615 CALL_INFO, 1,
"ERROR: Two different init_data values passed into SharedArray %s\n",
619 if ( verify != v_type ) {
621 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
630 std::lock_guard<std::mutex>
lock(mtx);
634 void update_write(
int index,
bool data)
642 check = written[index];
645 check = array[index] != init;
650 if ( check && (array[index] != data) ) {
652 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n", index,
656 if ( verify == FE_VERIFY ) written[index] =
true;
659 void write(
int index,
bool data)
661 std::lock_guard<std::mutex>
lock(mtx);
662 check_lock_for_write(
"SharedArray");
663 update_write(index, data);
664 if ( verify == FE_VERIFY ) written[index] =
true;
665 if ( change_set ) change_set->addChange(index, data);
673 inline bool read(
int index)
const {
return array[index]; }
678 std::lock_guard<std::mutex>
lock(mtx);
683 virtual SharedObjectChangeSet* getChangeSet()
override {
return change_set; }
684 virtual void resetChangeSet()
override { change_set->clear(); }
687 class ChangeSet :
public SharedObjectChangeSet
690 std::vector<std::pair<int, bool>> changes;
697 SharedObjectChangeSet::serialize_order(ser);
708 ChangeSet() : SharedObjectChangeSet() {}
709 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name), size(0), verify(VERIFY_UNINITIALIZED) {}
711 void addChange(
int index,
bool value) { changes.emplace_back(index, value); }
713 void setSize(
size_t length,
bool init_data,
verify_type v_type)
719 size_t getSize() {
return size; }
721 void applyChanges(SharedObjectDataManager* manager)
override
723 auto data = manager->getSharedObjectData<Data>(
getName());
724 data->setSize(size, init, verify);
725 for (
auto x : changes ) {
726 data->update_write(x.first, x.second);
730 void clear()
override { changes.clear(); }
737 #endif // SST_CORE_SHARED_SHAREDARRAY_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:253
static Simulation * getSimulation()
Return a pointer to the singleton instance of the Simulation.
Definition: simulation.cc:52
Definition: sharedRegionImpl.h:30
const T & operator[](int index) const
Read data from the array.
Definition: sharedArray.h:197
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:496
void lock()
Called by the core when writing to shared regions is no longer allowed.
Definition: sharedObject.h:179
const std::string & getName()
Get the name of the shared data the changeset should be applied to.
Definition: sharedObject.h:65
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:147
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:125
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:394
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:130
const T & mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:208
size_t size() const
Get the length of the array.
Definition: sharedArray.h:113
verify_type
Enum of verify types.
Definition: sharedObject.h:260
bool operator[](int index) const
Read data from the array.
Definition: sharedArray.h:553
void write(int index, const T &value)
Write data to the array.
Definition: sharedArray.h:171
size_t size() const
Get the length of the array.
Definition: sharedArray.h:469
int initialize(const std::string &obj_name, size_t length=0, bool init_value=false, verify_type v_type=INIT_VERIFY)
Initialize the SharedArray.
Definition: sharedArray.h:437
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:491
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:135
void write(int index, bool value)
Write data to the array.
Definition: sharedArray.h:527
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:140
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:161
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
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:481
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:120
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:39
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:476
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:486
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:503
SharedArray class.
Definition: sharedArray.h:28
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:399
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:44
bool mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:564
int initialize(const std::string &obj_name, size_t length=0, T init_value=T(), verify_type v_type=INIT_VERIFY)
Initialize the SharedArray.
Definition: sharedArray.h:82
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
Base class for holding SharedObject data.
Definition: sharedObject.h:80
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:517