From fc3845ec8941d9ca493c5fa4b5a681916a920402 Mon Sep 17 00:00:00 2001 From: Spiros Tsalikis Date: Fri, 22 Mar 2024 11:13:00 -0400 Subject: [PATCH] AtomicArrayExecutionObject: Allow order of atomic operations --- ...Object_Allow_order_of_atomic_operations.md | 3 ++ vtkm/exec/AtomicArrayExecutionObject.h | 31 +++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 docs/changelog/AtomicArrayExecutionObject_Allow_order_of_atomic_operations.md diff --git a/docs/changelog/AtomicArrayExecutionObject_Allow_order_of_atomic_operations.md b/docs/changelog/AtomicArrayExecutionObject_Allow_order_of_atomic_operations.md new file mode 100644 index 000000000..f4654f5ea --- /dev/null +++ b/docs/changelog/AtomicArrayExecutionObject_Allow_order_of_atomic_operations.md @@ -0,0 +1,3 @@ +# AtomicArrayExecutionObject: Allow order of atomic operations + +AtomicArrayExecutionObject now allows order of atomic operations for Get/Set/Add/CompareExchange. diff --git a/vtkm/exec/AtomicArrayExecutionObject.h b/vtkm/exec/AtomicArrayExecutionObject.h index 8f730d06c..d36355447 100644 --- a/vtkm/exec/AtomicArrayExecutionObject.h +++ b/vtkm/exec/AtomicArrayExecutionObject.h @@ -112,30 +112,34 @@ public: /// \brief Perform an atomic load of the indexed element with acquire memory /// ordering. /// \param index The index of the element to load. + /// \param order The memory ordering to use for the load operation. /// \return The value of the atomic array at \a index. /// VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC - ValueType Get(vtkm::Id index) const + ValueType Get(vtkm::Id index, vtkm::MemoryOrder order = vtkm::MemoryOrder::Acquire) const { // We only support 32/64 bit signed/unsigned ints, and vtkm::Atomic // currently only provides API for unsigned types. // We'll cast the signed types to unsigned to work around this. using APIType = typename detail::MakeUnsigned::type; - return static_cast(vtkm::AtomicLoad(reinterpret_cast(this->Data + index))); + return static_cast(vtkm::AtomicLoad(reinterpret_cast(this->Data + index), order)); } /// \brief Peform an atomic addition with sequentially consistent memory /// ordering. /// \param index The index of the array element that will be added to. /// \param value The addend of the atomic add operation. + /// \param order The memory ordering to use for the add operation. /// \return The original value of the element at \a index (before addition). /// \warning Overflow behavior from this operation is undefined. /// VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC - ValueType Add(vtkm::Id index, const ValueType& value) const + ValueType Add(vtkm::Id index, + const ValueType& value, + vtkm::MemoryOrder order = vtkm::MemoryOrder::SequentiallyConsistent) const { // We only support 32/64 bit signed/unsigned ints, and vtkm::Atomic // currently only provides API for unsigned types. @@ -145,14 +149,15 @@ public: // document that overflow is undefined for this operation. using APIType = typename detail::ArithType::type; - return static_cast( - vtkm::AtomicAdd(reinterpret_cast(this->Data + index), static_cast(value))); + return static_cast(vtkm::AtomicAdd( + reinterpret_cast(this->Data + index), static_cast(value), order)); } /// \brief Peform an atomic store to memory while enforcing, at minimum, "release" /// memory ordering. /// \param index The index of the array element that will be added to. /// \param value The value to write for the atomic store operation. + /// \param order The memory ordering to use for the store operation. /// \warning Using something like: /// ``` /// Set(index, Get(index)+N) @@ -162,7 +167,9 @@ public: /// VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC - void Set(vtkm::Id index, const ValueType& value) const + void Set(vtkm::Id index, + const ValueType& value, + vtkm::MemoryOrder order = vtkm::MemoryOrder::Release) const { // We only support 32/64 bit signed/unsigned ints, and vtkm::Atomic // currently only provides API for unsigned types. @@ -172,7 +179,8 @@ public: // document that overflow is undefined for this operation. using APIType = typename detail::MakeUnsigned::type; - vtkm::AtomicStore(reinterpret_cast(this->Data + index), static_cast(value)); + vtkm::AtomicStore( + reinterpret_cast(this->Data + index), static_cast(value), order); } /// \brief Perform an atomic compare and exchange operation with sequentially consistent @@ -181,6 +189,7 @@ public: /// modified. /// \param oldValue A pointer to the expected value of the indexed element. /// \param newValue The value to replace the indexed element with. + /// \param order The memory ordering to use for the compare and exchange operation. /// \return If the operation is successful, \a true is returned. Otherwise, /// \a oldValue is replaced with the current value of the indexed element, /// the element is not modified, and \a false is returned. In either case, \a oldValue @@ -220,7 +229,10 @@ public: /// VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC - bool CompareExchange(vtkm::Id index, ValueType* oldValue, const ValueType& newValue) const + bool CompareExchange(vtkm::Id index, + ValueType* oldValue, + const ValueType& newValue, + vtkm::MemoryOrder order = vtkm::MemoryOrder::SequentiallyConsistent) const { // We only support 32/64 bit signed/unsigned ints, and vtkm::Atomic // currently only provides API for unsigned types. @@ -231,7 +243,8 @@ public: return vtkm::AtomicCompareExchange(reinterpret_cast(this->Data + index), reinterpret_cast(oldValue), - static_cast(newValue)); + static_cast(newValue), + order); } private: