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" 33 static_assert(!std::is_pointer_v<T>,
"Cannot use a pointer type with SharedArray");
89 int initialize(
const std::string& obj_name,
size_t length = 0, T init_value = T(),
verify_type v_type = INIT_VERIFY)
92 Private::getSimulationOutput().fatal(
93 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
96 if ( v_type == VERIFY_UNINITIALIZED ) {
97 Private::getSimulationOutput().fatal(CALL_INFO, 1,
98 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. " 99 "This is a reserved value and cannot be passed in here. \n",
103 data = manager.getSharedObjectData<Data>(obj_name);
104 int ret = incShareCount(data);
105 if ( length != 0 ) data->
setSize(length, init_value, v_type);
111 using const_iterator =
typename std::vector<T>::const_iterator;
112 using const_reverse_iterator =
typename std::vector<T>::const_reverse_iterator;
119 inline size_t size()
const {
return data->getSize(); }
126 inline bool empty()
const {
return data->array.empty(); }
131 const_iterator
begin()
const {
return data->array.cbegin(); }
136 const_iterator
end()
const {
return data->array.cend(); }
141 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
146 const_reverse_iterator
rend()
const {
return data->array.crend(); }
155 if ( published )
return;
157 incPublishCount(data);
177 inline void write(
int index,
const T& value)
180 Private::getSimulationOutput().fatal(
181 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->
getName().c_str());
183 return data->write(index, value);
203 inline const T&
operator[](
int index)
const {
return data->read(index); }
214 inline const T&
mutex_read(
int index)
const {
return data->mutex_read(index); }
218 SST::Shared::SharedObject::serialize_order(ser);
220 bool initialized = (data !=
nullptr);
221 SST_SER(initialized);
223 if ( !initialized )
return;
225 switch ( ser.mode() ) {
226 case SST::Core::Serialization::serializer::SIZER:
227 case SST::Core::Serialization::serializer::PACK:
229 std::string name = data->
getName();
233 case SST::Core::Serialization::serializer::UNPACK:
237 data = manager.getSharedObjectData<Data>(name);
240 case SST::Core::Serialization::serializer::MAP:
249 Data* data =
nullptr;
258 std::vector<T> array;
259 std::vector<bool> written;
260 ChangeSet* change_set;
267 verify(VERIFY_UNINITIALIZED)
269 explicit Data(
const std::string& name) :
272 verify(VERIFY_UNINITIALIZED)
274 if ( Private::getNumRanks().rank > 1 ) {
275 change_set =
new ChangeSet(name);
281 if ( change_set )
delete change_set;
295 if ( v_type == VERIFY_UNINITIALIZED )
return;
296 std::lock_guard<std::mutex>
lock(mtx);
297 if (
size > array.size() ) {
299 array.resize(
size, init_data);
300 if ( v_type == FE_VERIFY ) {
301 written.resize(
size);
303 if ( change_set ) change_set->setSize(
size, init_data, v_type);
308 if ( verify != VERIFY_UNINITIALIZED ) {
309 if ( init != init_data ) {
310 Private::getSimulationOutput().fatal(CALL_INFO, 1,
311 "ERROR: Two different init_data values passed into SharedArray %s\n", name.c_str());
314 if ( verify != v_type ) {
315 Private::getSimulationOutput().fatal(
316 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
325 std::lock_guard<std::mutex>
lock(mtx);
329 void update_write(
int index,
const T& data)
337 check = written[index];
340 check = array[index] != init;
345 if ( check && (array[index] != data) ) {
346 Private::getSimulationOutput().fatal(CALL_INFO, 1,
347 "ERROR: wrote two different values to index %d of SharedArray %s\n", index, name.c_str());
350 if ( verify == FE_VERIFY ) written[index] =
true;
353 void write(
int index,
const T& data)
355 std::lock_guard<std::mutex>
lock(mtx);
356 check_lock_for_write(
"SharedArray");
357 update_write(index, data);
358 if ( verify == FE_VERIFY ) written[index] =
true;
359 if ( change_set ) change_set->addChange(index, data);
367 inline const T& read(
int index)
const {
return array[index]; }
370 inline const T& mutex_read(
int index)
const 372 std::lock_guard<std::mutex>
lock(mtx);
382 SharedObjectData::serialize_order(ser);
392 std::vector<std::pair<int, T>> changes;
399 SharedObjectChangeSet::serialize_order(ser);
411 SharedObjectChangeSet()
413 explicit ChangeSet(
const std::string& name) :
414 SharedObjectChangeSet(name),
416 verify(VERIFY_UNINITIALIZED)
419 void addChange(
int index,
const T& value) { changes.emplace_back(index, value); }
427 size_t getSize() {
return size; }
429 void applyChanges(SharedObjectDataManager* manager)
override 431 auto data = manager->getSharedObjectData<Data>(
getName());
432 data->setSize(
size, init, verify);
433 for (
auto x : changes ) {
434 data->update_write(x.first, x.second);
438 void clear()
override { changes.clear(); }
505 const std::string& obj_name,
size_t length = 0,
bool init_value =
false,
verify_type v_type = INIT_VERIFY)
508 Private::getSimulationOutput().fatal(
509 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
512 if ( v_type == VERIFY_UNINITIALIZED ) {
513 Private::getSimulationOutput().fatal(CALL_INFO, 1,
514 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. " 515 "This is a reserved value and cannot be passed in here. \n",
518 data = manager.getSharedObjectData<Data>(obj_name);
519 int ret = incShareCount(data);
521 data->setSize(length, init_value, v_type);
529 using const_iterator =
typename std::vector<bool>::const_iterator;
530 using const_reverse_iterator =
typename std::vector<bool>::const_reverse_iterator;
537 inline size_t size()
const {
return data->getSize(); }
544 inline bool empty()
const {
return data->array.empty(); }
549 const_iterator
begin()
const {
return data->array.cbegin(); }
554 const_iterator
end()
const {
return data->array.cend(); }
559 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
564 const_reverse_iterator
rend()
const {
return data->array.crend(); }
573 if ( published )
return;
575 incPublishCount(data);
595 inline void write(
int index,
bool value)
598 Private::getSimulationOutput().fatal(
599 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->getName().c_str());
601 return data->write(index, value);
621 inline bool operator[](
int index)
const {
return data->read(index); }
632 inline bool mutex_read(
int index)
const {
return data->mutex_read(index); }
636 SST::Shared::SharedObject::serialize_order(ser);
638 bool initialized = (data !=
nullptr);
639 SST_SER(initialized);
641 if ( !initialized )
return;
643 switch ( ser.mode() ) {
644 case SST::Core::Serialization::serializer::SIZER:
645 case SST::Core::Serialization::serializer::PACK:
647 std::string name = data->getName();
651 case SST::Core::Serialization::serializer::UNPACK:
655 data = manager.getSharedObjectData<Data>(name);
658 case SST::Core::Serialization::serializer::MAP:
667 Data* data =
nullptr;
676 std::vector<bool> array;
677 std::vector<bool> written;
678 ChangeSet* change_set;
685 verify(VERIFY_UNINITIALIZED)
687 explicit Data(
const std::string& name) :
690 verify(VERIFY_UNINITIALIZED)
692 if ( Private::getNumRanks().rank > 1 ) {
693 change_set =
new ChangeSet(name);
699 if ( change_set )
delete change_set;
713 if ( v_type == VERIFY_UNINITIALIZED )
return;
714 std::lock_guard<std::mutex>
lock(mtx);
715 if (
size > array.size() ) {
717 array.resize(
size, init_data);
718 if ( v_type == FE_VERIFY ) {
719 written.resize(
size);
721 if ( change_set ) change_set->setSize(
size, init_data, v_type);
726 if ( verify != VERIFY_UNINITIALIZED ) {
727 if ( init != init_data ) {
728 Private::getSimulationOutput().fatal(CALL_INFO, 1,
729 "ERROR: Two different init_data values passed into SharedArray %s\n", name.c_str());
732 if ( verify != v_type ) {
733 Private::getSimulationOutput().fatal(
734 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
743 std::lock_guard<std::mutex>
lock(mtx);
747 void update_write(
int index,
bool data)
755 check = written[index];
758 check = array[index] != init;
763 if ( check && (array[index] != data) ) {
764 Private::getSimulationOutput().fatal(CALL_INFO, 1,
765 "ERROR: wrote two different values to index %d of SharedArray %s\n", index, name.c_str());
768 if ( verify == FE_VERIFY ) written[index] =
true;
771 void write(
int index,
bool data)
773 std::lock_guard<std::mutex>
lock(mtx);
774 check_lock_for_write(
"SharedArray");
775 update_write(index, data);
776 if ( verify == FE_VERIFY ) written[index] =
true;
777 if ( change_set ) change_set->addChange(index, data);
785 inline bool read(
int index)
const {
return array[index]; }
788 inline bool mutex_read(
int index)
const 790 std::lock_guard<std::mutex>
lock(mtx);
800 SharedObjectData::serialize_order(ser);
811 std::vector<std::pair<int, bool>> changes;
818 SharedObjectChangeSet::serialize_order(ser);
830 SharedObjectChangeSet()
832 explicit ChangeSet(
const std::string& name) :
833 SharedObjectChangeSet(name),
835 verify(VERIFY_UNINITIALIZED)
838 void addChange(
int index,
bool value) { changes.emplace_back(index, value); }
846 size_t getSize() {
return size; }
848 void applyChanges(SharedObjectDataManager* manager)
override 850 auto data = manager->getSharedObjectData<Data>(
getName());
851 data->setSize(
size, init, verify);
852 for (
auto x : changes ) {
853 data->update_write(x.first, x.second);
857 void clear()
override { changes.clear(); }
864 #endif // SST_CORE_SHARED_SHAREDARRAY_H size_t size() const
Get the length of the array.
Definition: sharedArray.h:537
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedArray.h:377
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedArray.h:795
Definition: sharedObject.h:276
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:141
const T & operator[](int index) const
Read data from the array.
Definition: sharedArray.h:203
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:549
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedArray.h:796
void lock()
Called by the core when writing to shared regions is no longer allowed.
Definition: sharedObject.h:192
bool isFullyPublished()
Checks to see if all instances of this SharedObject have called publish().
Definition: sharedObject.h:115
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:153
size_t size() const
Get the length of the array.
Definition: sharedArray.h:119
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:457
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:146
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:544
verify_type
Enum of verify types.
Definition: sharedObject.h:283
bool empty() const
Tests if array is empty.
Definition: sharedArray.h:126
SharedArray class.
Definition: sharedArray.h:448
void write(int index, const T &value)
Write data to the array.
Definition: sharedArray.h:177
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:504
Definition: sharedArray.h:24
const_iterator begin() const
Get const_iterator to beginning of underlying map.
Definition: sharedArray.h:131
void write(int index, bool value)
Write data to the array.
Definition: sharedArray.h:595
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:167
void setSize(size_t size, bool init_data, verify_type v_type)
Set the size of the array.
Definition: sharedArray.h:710
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedArray.h:378
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:564
bool mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:632
SharedArray()
Default constructor for SharedArray.
Definition: sharedArray.h:42
const_reverse_iterator rbegin() const
Get const_reverse_iterator to beginning of underlying map.
Definition: sharedArray.h:559
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:571
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:554
SharedArray class.
Definition: sharedArray.h:31
Definition: sharedArray.h:251
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:466
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:51
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:89
const std::string & getName()
Get the name of the SharedObject for this data.
Definition: sharedObject.h:105
This is the base class for holding data on changes made to the shared data on each rank...
Definition: sharedObject.h:48
const T & mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:214
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:136
Base class for holding SharedObject data.
Definition: sharedObject.h:93
bool isFullyPublished()
Check whether all instances of this SharedArray have called publish().
Definition: sharedArray.h:585
bool operator[](int index) const
Read data from the array.
Definition: sharedArray.h:621
void setSize(size_t size, const T &init_data, verify_type v_type)
Set the size of the array.
Definition: sharedArray.h:292