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);
245 std::vector<T> array;
246 std::vector<bool> written;
247 ChangeSet* change_set;
252 Data(
const std::string& name) :
SharedObjectData(name), change_set(
nullptr), verify(VERIFY_UNINITIALIZED)
254 if ( Private::getNumRanks().rank > 1 ) { change_set =
new ChangeSet(name); }
259 if ( change_set )
delete change_set;
273 if ( v_type == VERIFY_UNINITIALIZED )
return;
274 std::lock_guard<std::mutex>
lock(mtx);
275 if (
size > array.size() ) {
277 array.resize(
size, init_data);
278 if ( v_type == FE_VERIFY ) { written.resize(
size); }
279 if ( change_set ) change_set->setSize(
size, init_data, v_type);
284 if ( verify != VERIFY_UNINITIALIZED ) {
285 if ( init != init_data ) {
286 Private::getSimulationOutput().fatal(
287 CALL_INFO, 1,
"ERROR: Two different init_data values passed into SharedArray %s\n",
291 if ( verify != v_type ) {
292 Private::getSimulationOutput().fatal(
293 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
302 std::lock_guard<std::mutex>
lock(mtx);
306 void update_write(
int index,
const T& data)
314 check = written[index];
317 check = array[index] != init;
322 if ( check && (array[index] != data) ) {
323 Private::getSimulationOutput().fatal(
324 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n", index,
328 if ( verify == FE_VERIFY ) written[index] =
true;
331 void write(
int index,
const T& data)
333 std::lock_guard<std::mutex>
lock(mtx);
334 check_lock_for_write(
"SharedArray");
335 update_write(index, data);
336 if ( verify == FE_VERIFY ) written[index] =
true;
337 if ( change_set ) change_set->addChange(index, data);
345 inline const T& read(
int index)
const {
return array[index]; }
348 inline const T& mutex_read(
int index)
const 350 std::lock_guard<std::mutex>
lock(mtx);
351 auto ret = array[index];
361 SharedObjectData::serialize_order(ser);
371 std::vector<std::pair<int, T>> changes;
378 SharedObjectChangeSet::serialize_order(ser);
389 ChangeSet() : SharedObjectChangeSet() {}
390 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name),
size(0), verify(VERIFY_UNINITIALIZED) {}
392 void addChange(
int index,
const T& value) { changes.emplace_back(index, value); }
400 size_t getSize() {
return size; }
402 void applyChanges(SharedObjectDataManager* manager)
override 404 auto data = manager->getSharedObjectData<Data>(
getName());
405 data->setSize(
size, init, verify);
406 for (
auto x : changes ) {
407 data->update_write(x.first, x.second);
411 void clear()
override { changes.clear(); }
474 const std::string& obj_name,
size_t length = 0,
bool init_value =
false,
verify_type v_type = INIT_VERIFY)
477 Private::getSimulationOutput().fatal(
478 CALL_INFO, 1,
"ERROR: called initialize() of SharedArray %s more than once\n", obj_name.c_str());
481 if ( v_type == VERIFY_UNINITIALIZED ) {
482 Private::getSimulationOutput().fatal(
484 "ERROR: VERIFY_UNINITIALIZED passed into instance of SharedArray %s. " 485 "This is a reserved value and cannot be passed in here. \n",
488 data = manager.getSharedObjectData<Data>(obj_name);
489 int ret = incShareCount(data);
490 if ( length != 0 ) { data->setSize(length, init_value, v_type); }
497 typedef typename std::vector<bool>::const_iterator const_iterator;
498 typedef typename std::vector<bool>::const_reverse_iterator const_reverse_iterator;
505 inline size_t size()
const {
return data->getSize(); }
512 inline bool empty()
const {
return data->array.empty(); }
517 const_iterator
begin()
const {
return data->array.cbegin(); }
522 const_iterator
end()
const {
return data->array.cend(); }
527 const_reverse_iterator
rbegin()
const {
return data->array.crbegin(); }
532 const_reverse_iterator
rend()
const {
return data->array.crend(); }
541 if ( published )
return;
543 incPublishCount(data);
563 inline void write(
int index,
bool value)
566 Private::getSimulationOutput().fatal(
567 CALL_INFO, 1,
"ERROR: write to SharedArray %s after publish() was called\n", data->getName().c_str());
569 return data->write(index, value);
589 inline bool operator[](
int index)
const {
return data->read(index); }
600 inline bool mutex_read(
int index)
const {
return data->mutex_read(index); }
604 SST::Shared::SharedObject::serialize_order(ser);
606 switch ( ser.mode() ) {
607 case SST::Core::Serialization::serializer::SIZER:
608 case SST::Core::Serialization::serializer::PACK:
610 std::string name = data->getName();
614 case SST::Core::Serialization::serializer::UNPACK:
618 data = manager.getSharedObjectData<Data>(name);
636 std::vector<bool> array;
637 std::vector<bool> written;
638 ChangeSet* change_set;
642 Data() :
SharedObjectData(), change_set(
nullptr), verify(VERIFY_UNINITIALIZED) {}
643 Data(
const std::string& name) :
SharedObjectData(name), change_set(
nullptr), verify(VERIFY_UNINITIALIZED)
645 if ( Private::getNumRanks().rank > 1 ) { change_set =
new ChangeSet(name); }
650 if ( change_set )
delete change_set;
664 if ( v_type == VERIFY_UNINITIALIZED )
return;
665 std::lock_guard<std::mutex>
lock(mtx);
666 if (
size > array.size() ) {
668 array.resize(
size, init_data);
669 if ( v_type == FE_VERIFY ) { written.resize(
size); }
670 if ( change_set ) change_set->setSize(
size, init_data, v_type);
675 if ( verify != VERIFY_UNINITIALIZED ) {
676 if ( init != init_data ) {
677 Private::getSimulationOutput().fatal(
678 CALL_INFO, 1,
"ERROR: Two different init_data values passed into SharedArray %s\n",
682 if ( verify != v_type ) {
683 Private::getSimulationOutput().fatal(
684 CALL_INFO, 1,
"ERROR: Two different verify types passed into SharedArray %s\n", name.c_str());
693 std::lock_guard<std::mutex>
lock(mtx);
697 void update_write(
int index,
bool data)
705 check = written[index];
708 check = array[index] != init;
713 if ( check && (array[index] != data) ) {
714 Private::getSimulationOutput().fatal(
715 CALL_INFO, 1,
"ERROR: wrote two different values to index %d of SharedArray %s\n", index,
719 if ( verify == FE_VERIFY ) written[index] =
true;
722 void write(
int index,
bool data)
724 std::lock_guard<std::mutex>
lock(mtx);
725 check_lock_for_write(
"SharedArray");
726 update_write(index, data);
727 if ( verify == FE_VERIFY ) written[index] =
true;
728 if ( change_set ) change_set->addChange(index, data);
736 inline bool read(
int index)
const {
return array[index]; }
739 inline bool mutex_read(
int index)
const 741 std::lock_guard<std::mutex>
lock(mtx);
751 SharedObjectData::serialize_order(ser);
762 std::vector<std::pair<int, bool>> changes;
769 SharedObjectChangeSet::serialize_order(ser);
780 ChangeSet() : SharedObjectChangeSet() {}
781 ChangeSet(
const std::string& name) : SharedObjectChangeSet(name),
size(0), verify(VERIFY_UNINITIALIZED) {}
783 void addChange(
int index,
bool value) { changes.emplace_back(index, value); }
791 size_t getSize() {
return size; }
793 void applyChanges(SharedObjectDataManager* manager)
override 795 auto data = manager->getSharedObjectData<Data>(
getName());
796 data->setSize(
size, init, verify);
797 for (
auto x : changes ) {
798 data->update_write(x.first, x.second);
802 void clear()
override { changes.clear(); }
809 #endif // SST_CORE_SHARED_SHAREDARRAY_H size_t size() const
Get the length of the array.
Definition: sharedArray.h:505
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedArray.h:356
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
virtual SharedObjectChangeSet * getChangeSet() override
Gets the changeset for this data on this rank.
Definition: sharedArray.h:746
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:517
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedArray.h:747
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:430
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:512
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:421
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:473
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:563
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:661
virtual void resetChangeSet() override
Resets the changeset for this data on this rank.
Definition: sharedArray.h:357
const_reverse_iterator rend() const
Get const_reverse_iterator to end of underlying map.
Definition: sharedArray.h:532
bool mutex_read(int index) const
Read data from the array.
Definition: sharedArray.h:600
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:527
void publish()
Indicate that the calling element has written all the data it plans to write.
Definition: sharedArray.h:539
const_iterator end() const
Get const_iterator to end of underlying map.
Definition: sharedArray.h:522
SharedArray class.
Definition: sharedArray.h:29
Definition: sharedArray.h:238
~SharedArray()
Shared Array Destructor.
Definition: sharedArray.h:435
~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:553
bool operator[](int index) const
Read data from the array.
Definition: sharedArray.h:589
void setSize(size_t size, const T &init_data, verify_type v_type)
Set the size of the array.
Definition: sharedArray.h:270