12 #ifndef SST_CORE_CORE_SHARED_SHAREDARRAY_H
13 #define SST_CORE_CORE_SHARED_SHAREDARRAY_H
15 #include "sst/core/sst_types.h"
16 #include "sst/core/shared/sharedObject.h"
30 static_assert(!std::is_pointer<T>::value,
"Cannot use a pointer type with SharedArray");
85 int initialize(
const std::string& obj_name,
size_t length = 0, T init_value = T(),
verify_type v_type = INIT_VERIFY) {
88 CALL_INFO,1,
"ERROR: called initialize() of SharedArray %s more than once\n",obj_name.c_str());
91 if ( v_type == VERIFY_UNINITIALIZED ) {
93 CALL_INFO,1,
"ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. "
94 "This is a reserved value and cannot be passed in here. \n",obj_name.c_str());
97 data = manager.getSharedObjectData<Data>(obj_name);
98 int ret = incShareCount(data);
99 if ( length != 0 ) data->setSize(length,init_value,v_type);
106 typedef typename std::vector<T>::const_iterator const_iterator;
107 typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
114 inline size_t size()
const {
return data->getSize(); }
122 inline bool empty()
const {
return data->array.empty(); }
129 return data->array.cbegin();
135 const_iterator
end()
const {
136 return data->array.cend();
144 return data->array.crbegin();
150 const_reverse_iterator
rend()
const {
151 return data->array.crend();
162 if ( published )
return;
164 incPublishCount(data);
175 return data->isFullyPublished();
187 inline void write(
int index,
const T& value) {
190 CALL_INFO,1,
"ERROR: write to SharedArray %s after publish() was called\n",
191 data->getName().c_str());
193 return data->write(index, value);
214 return data->read(index);
227 return data->mutex_read(index);
243 std::vector<T> array;
244 std::vector<bool> written;
250 Data(
const std::string& name) :
253 verify(VERIFY_UNINITIALIZED)
274 if ( v_type == VERIFY_UNINITIALIZED )
return;
275 std::lock_guard<std::mutex>
lock(mtx);
276 if ( size > array.size() ) {
278 array.resize(size,init_data);
279 if ( v_type == FE_VERIFY ) {
280 written.resize(size);
282 if ( change_set ) change_set->setSize(size, init_data, v_type);
287 if ( verify != VERIFY_UNINITIALIZED ) {
288 if ( init != init_data ) {
290 CALL_INFO,1,
"ERROR: Two different init_data values passed into SharedArray %s\n",name.c_str());
293 if ( verify != v_type ) {
295 CALL_INFO,1,
"ERROR: Two different verify types passed into SharedArray %s\n",name.c_str());
303 std::lock_guard<std::mutex>
lock(mtx);
307 void update_write(
int index,
const T& data) {
314 check = written[index];
317 check = array[index] != init;
322 if ( check && (array[index] != data) ) {
324 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n",index,name.c_str());
327 if ( verify == FE_VERIFY ) written[index] =
true;
330 void write(
int index,
const T& data) {
331 std::lock_guard<std::mutex>
lock(mtx);
332 check_lock_for_write(
"SharedArray");
333 update_write(index,data);
334 if ( verify == FE_VERIFY ) written[index] =
true;
335 if ( change_set ) change_set->addChange(index,data);
343 inline const T& read(
int index)
const {
349 std::lock_guard<std::mutex>
lock(mtx);
350 auto ret = array[index];
355 virtual SharedObjectChangeSet* getChangeSet()
override {
358 virtual void resetChangeSet()
override {
363 class ChangeSet :
public SharedObjectChangeSet {
365 std::vector<std::pair<int,T> > changes;
371 SharedObjectChangeSet::serialize_order(ser);
383 SharedObjectChangeSet()
385 ChangeSet(
const std::string& name) :
386 SharedObjectChangeSet(name),
388 verify(VERIFY_UNINITIALIZED)
391 void addChange(
int index,
const T& value) {
392 changes.emplace_back(index,value);
395 void setSize(
size_t length,
const T& init_data,
verify_type v_type) {
400 size_t getSize() {
return size; }
402 void applyChanges(SharedObjectDataManager* manager)
override {
403 auto data = manager->getSharedObjectData<Data>(
getName());
404 data->setSize(size, init, verify);
405 for (
auto x : changes ) {
406 data->update_write(x.first, x.second);
410 void clear()
override {
481 int initialize(
const std::string& obj_name,
size_t length = 0,
bool init_value =
false,
verify_type v_type = INIT_VERIFY) {
484 CALL_INFO,1,
"ERROR: called initialize() of SharedArray %s more than once\n",obj_name.c_str());
487 if ( v_type == VERIFY_UNINITIALIZED ) {
489 CALL_INFO,1,
"ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. "
490 "This is a reserved value and cannot be passed in here. \n",obj_name.c_str());
493 data = manager.getSharedObjectData<Data>(obj_name);
494 int ret = incShareCount(data);
495 if ( length != 0 ) data->setSize(length,init_value,v_type);
502 typedef typename std::vector<bool>::const_iterator const_iterator;
503 typedef typename std::vector<bool>::const_reverse_iterator const_reverse_iterator;
510 inline size_t size()
const {
return data->getSize(); }
518 inline bool empty()
const {
return data->array.empty(); }
525 return data->array.cbegin();
531 const_iterator
end()
const {
532 return data->array.cend();
540 return data->array.crbegin();
546 const_reverse_iterator
rend()
const {
547 return data->array.crend();
558 if ( published )
return;
560 incPublishCount(data);
571 return data->isFullyPublished();
583 inline void write(
int index,
bool value) {
586 CALL_INFO,1,
"ERROR: write to SharedArray %s after publish() was called\n",
587 data->getName().c_str());
589 return data->write(index, value);
610 return data->read(index);
623 return data->mutex_read(index);
639 std::vector<bool> array;
640 std::vector<bool> written;
646 Data(
const std::string& name) :
649 verify(VERIFY_UNINITIALIZED)
668 void setSize(
size_t size,
bool init_data,
verify_type v_type) {
670 if ( v_type == VERIFY_UNINITIALIZED )
return;
671 std::lock_guard<std::mutex>
lock(mtx);
672 if ( size > array.size() ) {
674 array.resize(size,init_data);
675 if ( v_type == FE_VERIFY ) {
676 written.resize(size);
678 if ( change_set ) change_set->setSize(size, init_data, v_type);
683 if ( verify != VERIFY_UNINITIALIZED ) {
684 if ( init != init_data ) {
686 CALL_INFO,1,
"ERROR: Two different init_data values passed into SharedArray %s\n",name.c_str());
689 if ( verify != v_type ) {
691 CALL_INFO,1,
"ERROR: Two different verify types passed into SharedArray %s\n",name.c_str());
699 std::lock_guard<std::mutex>
lock(mtx);
703 void update_write(
int index,
bool data) {
710 check = written[index];
713 check = array[index] != init;
718 if ( check && (array[index] != data) ) {
720 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n",index,name.c_str());
723 if ( verify == FE_VERIFY ) written[index] =
true;
726 void write(
int index,
bool data) {
727 std::lock_guard<std::mutex>
lock(mtx);
728 check_lock_for_write(
"SharedArray");
729 update_write(index,data);
730 if ( verify == FE_VERIFY ) written[index] =
true;
731 if ( change_set ) change_set->addChange(index,data);
739 inline bool read(
int index)
const {
745 std::lock_guard<std::mutex>
lock(mtx);
750 virtual SharedObjectChangeSet* getChangeSet()
override {
753 virtual void resetChangeSet()
override {
758 class ChangeSet :
public SharedObjectChangeSet {
760 std::vector<std::pair<int,bool> > changes;
766 SharedObjectChangeSet::serialize_order(ser);
778 SharedObjectChangeSet()
780 ChangeSet(
const std::string& name) :
781 SharedObjectChangeSet(name),
783 verify(VERIFY_UNINITIALIZED)
786 void addChange(
int index,
bool value) {
787 changes.emplace_back(index,value);
790 void setSize(
size_t length,
bool init_data,
verify_type v_type) {
795 size_t getSize() {
return size; }
797 void applyChanges(SharedObjectDataManager* manager)
override {
798 auto data = manager->getSharedObjectData<Data>(
getName());
799 data->setSize(size, init, verify);
800 for (
auto x : changes ) {
801 data->update_write(x.first, x.second);
805 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
Definition: sharedRegionImpl.h:31
const T & operator[](int index) const
Read data from the array.
Definition: sharedArray.h:213
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:546
void lock()
Called by the core when writing to shared regions is no longer allowed.
Definition: sharedObject.h:185
const std::string & getName()
Get the name of the shared data the changeset should be applied to.
Definition: sharedObject.h:68
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:161
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:128
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:436
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:135
const T & mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:226
size_t size() const
Get the length of the array.
Definition: sharedArray.h:114
verify_type
Enum of verify types.
Definition: sharedObject.h:271
bool operator[](int index) const
Read data from the array.
Definition: sharedArray.h:609
void write(int index, const T &value)
Write data to the array.
Definition: sharedArray.h:187
size_t size() const
Get the length of the array.
Definition: sharedArray.h:510
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:481
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:539
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:143
void write(int index, bool value)
Write data to the array.
Definition: sharedArray.h:583
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:150
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:174
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:524
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:122
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:40
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:518
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:531
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:557
SharedArray class.
Definition: sharedArray.h:29
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:443
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:47
bool mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:622
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:85
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:87
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:570