12 #ifndef SST_CORE_SHARED_SHAREDARRAY_H 13 #define SST_CORE_SHARED_SHAREDARRAY_H 15 #include "sst/core/serialization/serializable.h" 16 #include "sst/core/shared/sharedObject.h" 17 #include "sst/core/sst_types.h" 31 static_assert(!std::is_pointer<T>::value,
"Cannot use a pointer type with SharedArray");
83 int initialize(
const std::string& obj_name,
size_t length = 0, T init_value = T(),
verify_type v_type = INIT_VERIFY)
86 Private::getSimulationOutput().fatal(
87 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
90 if ( v_type == VERIFY_UNINITIALIZED ) {
91 Private::getSimulationOutput().fatal(
93 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. " 94 "This is a reserved value and cannot be passed in here. \n",
98 data = manager.getSharedObjectData<Data>(obj_name);
99 int ret = incShareCount(data);
100 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(); }
121 inline bool empty()
const {
return data->array.empty(); }
126 const_iterator
begin()
const {
return data->array.cbegin(); }
131 const_iterator
end()
const {
return data->array.cend(); }
136 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
141 const_reverse_iterator
rend()
const {
return data->array.crend(); }
150 if ( published )
return;
152 incPublishCount(data);
172 inline void write(
int index,
const T& value)
175 Private::getSimulationOutput().fatal(
176 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->
getName().c_str());
178 return data->write(index, value);
198 inline const T&
operator[](
int index)
const {
return data->read(index); }
209 inline const T&
mutex_read(
int index)
const {
return data->mutex_read(index); }
213 SST::Shared::SharedObject::serialize_order(ser);
215 switch ( ser.mode() ) {
216 case SST::Core::Serialization::serializer::SIZER:
217 case SST::Core::Serialization::serializer::PACK:
219 std::string name = data->
getName();
223 case SST::Core::Serialization::serializer::UNPACK:
227 data = manager.getSharedObjectData<Data>(name);
230 case SST::Core::Serialization::serializer::MAP:
248 std::vector<T> array;
249 std::vector<bool> written;
250 ChangeSet* change_set;
255 Data(
const std::string& name) :
SharedObjectData(name), change_set(
nullptr), verify(VERIFY_UNINITIALIZED)
257 if ( Private::getNumRanks().rank > 1 ) { change_set =
new ChangeSet(name); }
262 if ( change_set )
delete change_set;
276 if ( v_type == VERIFY_UNINITIALIZED )
return;
277 std::lock_guard<std::mutex>
lock(mtx);
278 if (
size > array.size() ) {
280 array.resize(
size, init_data);
281 if ( v_type == FE_VERIFY ) { 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 ) {
289 Private::getSimulationOutput().fatal(
290 CALL_INFO, 1,
"ERROR: Two different init_data values passed into SharedArray %s\n",
294 if ( verify != v_type ) {
295 Private::getSimulationOutput().fatal(
296 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
305 std::lock_guard<std::mutex>
lock(mtx);
309 void update_write(
int index,
const T& data)
317 check = written[index];
320 check = array[index] != init;
325 if ( check && (array[index] != data) ) {
326 Private::getSimulationOutput().fatal(
327 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n", index,
331 if ( verify == FE_VERIFY ) written[index] =
true;
334 void write(
int index,
const T& data)
336 std::lock_guard<std::mutex>
lock(mtx);
337 check_lock_for_write(
"SharedArray");
338 update_write(index, data);
339 if ( verify == FE_VERIFY ) written[index] =
true;
340 if ( change_set ) change_set->addChange(index, data);
348 inline const T& read(
int index)
const {
return array[index]; }
351 inline const T& mutex_read(
int index)
const 353 std::lock_guard<std::mutex>
lock(mtx);
363 SharedObjectData::serialize_order(ser);
373 std::vector<std::pair<int, T>> changes;
380 SharedObjectChangeSet::serialize_order(ser);
391 ChangeSet() : SharedObjectChangeSet() {}
392 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name),
size(0), verify(VERIFY_UNINITIALIZED) {}
394 void addChange(
int index,
const T& value) { changes.emplace_back(index, value); }
402 size_t getSize() {
return size; }
404 void applyChanges(SharedObjectDataManager* manager)
override 406 auto data = manager->getSharedObjectData<Data>(
getName());
407 data->setSize(
size, init, verify);
408 for (
auto x : changes ) {
409 data->update_write(x.first, x.second);
413 void clear()
override { changes.clear(); }
476 const std::string& obj_name,
size_t length = 0,
bool init_value =
false,
verify_type v_type = INIT_VERIFY)
479 Private::getSimulationOutput().fatal(
480 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
483 if ( v_type == VERIFY_UNINITIALIZED ) {
484 Private::getSimulationOutput().fatal(
486 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. " 487 "This is a reserved value and cannot be passed in here. \n",
490 data = manager.getSharedObjectData<Data>(obj_name);
491 int ret = incShareCount(data);
492 if ( length != 0 ) { data->setSize(length, init_value, v_type); }
499 typedef typename std::vector<bool>::const_iterator const_iterator;
500 typedef typename std::vector<bool>::const_reverse_iterator const_reverse_iterator;
507 inline size_t size()
const {
return data->getSize(); }
514 inline bool empty()
const {
return data->array.empty(); }
519 const_iterator
begin()
const {
return data->array.cbegin(); }
524 const_iterator
end()
const {
return data->array.cend(); }
529 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
534 const_reverse_iterator
rend()
const {
return data->array.crend(); }
543 if ( published )
return;
545 incPublishCount(data);
565 inline void write(
int index,
bool value)
568 Private::getSimulationOutput().fatal(
569 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->getName().c_str());
571 return data->write(index, value);
591 inline bool operator[](
int index)
const {
return data->read(index); }
602 inline bool mutex_read(
int index)
const {
return data->mutex_read(index); }
606 SST::Shared::SharedObject::serialize_order(ser);
608 switch ( ser.mode() ) {
609 case SST::Core::Serialization::serializer::SIZER:
610 case SST::Core::Serialization::serializer::PACK:
612 std::string name = data->getName();
616 case SST::Core::Serialization::serializer::UNPACK:
620 data = manager.getSharedObjectData<Data>(name);
623 case SST::Core::Serialization::serializer::MAP:
641 std::vector<bool> array;
642 std::vector<bool> written;
643 ChangeSet* change_set;
647 Data() :
SharedObjectData(), change_set(
nullptr), verify(VERIFY_UNINITIALIZED) {}
648 Data(
const std::string& name) :
SharedObjectData(name), change_set(
nullptr), verify(VERIFY_UNINITIALIZED)
650 if ( Private::getNumRanks().rank > 1 ) { change_set =
new ChangeSet(name); }
655 if ( change_set )
delete change_set;
669 if ( v_type == VERIFY_UNINITIALIZED )
return;
670 std::lock_guard<std::mutex>
lock(mtx);
671 if (
size > array.size() ) {
673 array.resize(
size, init_data);
674 if ( v_type == FE_VERIFY ) { written.resize(
size); }
675 if ( change_set ) change_set->setSize(
size, init_data, v_type);
680 if ( verify != VERIFY_UNINITIALIZED ) {
681 if ( init != init_data ) {
682 Private::getSimulationOutput().fatal(
683 CALL_INFO, 1,
"ERROR: Two different init_data values passed into SharedArray %s\n",
687 if ( verify != v_type ) {
688 Private::getSimulationOutput().fatal(
689 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
698 std::lock_guard<std::mutex>
lock(mtx);
702 void update_write(
int index,
bool data)
710 check = written[index];
713 check = array[index] != init;
718 if ( check && (array[index] != data) ) {
719 Private::getSimulationOutput().fatal(
720 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n", index,
724 if ( verify == FE_VERIFY ) written[index] =
true;
727 void write(
int index,
bool data)
729 std::lock_guard<std::mutex>
lock(mtx);
730 check_lock_for_write(
"SharedArray");
731 update_write(index, data);
732 if ( verify == FE_VERIFY ) written[index] =
true;
733 if ( change_set ) change_set->addChange(index, data);
741 inline bool read(
int index)
const {
return array[index]; }
744 inline bool mutex_read(
int index)
const 746 std::lock_guard<std::mutex>
lock(mtx);
756 SharedObjectData::serialize_order(ser);
767 std::vector<std::pair<int, bool>> changes;
774 SharedObjectChangeSet::serialize_order(ser);
785 ChangeSet() : SharedObjectChangeSet() {}
786 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name),
size(0), verify(VERIFY_UNINITIALIZED) {}
788 void addChange(
int index,
bool value) { changes.emplace_back(index, value); }
796 size_t getSize() {
return size; }
798 void applyChanges(SharedObjectDataManager* manager)
override 800 auto data = manager->getSharedObjectData<Data>(
getName());
801 data->setSize(
size, init, verify);
802 for (
auto x : changes ) {
803 data->update_write(x.first, x.second);
807 void clear()
override { changes.clear(); }
814 #endif // SST_CORE_SHARED_SHAREDARRAY_H size_t size() const
Get the length of the array.
Definition: sharedArray.h:507
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedArray.h:358
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:43
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedArray.h:751
Definition: sharedObject.h:269
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:136
const T & operator[](int index) const
Read data from the array.
Definition: sharedArray.h:198
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:519
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedArray.h:752
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
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:148
size_t size() const
Get the length of the array.
Definition: sharedArray.h:114
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:432
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:141
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:514
verify_type
Enum of verify types.
Definition: sharedObject.h:276
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:121
SharedArray class.
Definition: sharedArray.h:423
void write(int index, const T &value)
Write data to the array.
Definition: sharedArray.h:172
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:475
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:126
void write(int index, bool value)
Write data to the array.
Definition: sharedArray.h:565
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:162
void setSize(size_t size, bool init_data, verify_type v_type)
Set the size of the array.
Definition: sharedArray.h:666
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedArray.h:359
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:534
bool mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:602
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:40
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:529
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:541
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:524
SharedArray class.
Definition: sharedArray.h:29
Definition: sharedArray.h:241
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:437
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:45
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:83
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
const T & mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:209
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:131
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:555
bool operator[](int index) const
Read data from the array.
Definition: sharedArray.h:591
void setSize(size_t size, const T &init_data, verify_type v_type)
Set the size of the array.
Definition: sharedArray.h:273