VTK-m  2.0
Classes | Public Types | Public Member Functions | Private Attributes | List of all members
vtkm::exec::AtomicArrayExecutionObject< T > Class Template Reference

#include <AtomicArrayExecutionObject.h>

Classes

struct  HasPointerAccess
 

Public Types

using ValueType = T
 

Public Member Functions

 AtomicArrayExecutionObject ()=default
 
VTKM_CONT AtomicArrayExecutionObject (vtkm::cont::ArrayHandle< T > handle, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
 
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC vtkm::Id GetNumberOfValues () const
 
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC ValueType Get (vtkm::Id index) const
 Perform an atomic load of the indexed element with acquire memory ordering. More...
 
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC ValueType Add (vtkm::Id index, const ValueType &value) const
 Peform an atomic addition with sequentially consistent memory ordering. More...
 
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC void Set (vtkm::Id index, const ValueType &value) const
 Peform an atomic store to memory while enforcing, at minimum, "release" memory ordering. More...
 
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC bool CompareExchange (vtkm::Id index, ValueType *oldValue, const ValueType &newValue) const
 Perform an atomic compare and exchange operation with sequentially consistent memory ordering. More...
 

Private Attributes

ValueTypeData { nullptr }
 
vtkm::Id NumberOfValues { 0 }
 

Member Typedef Documentation

◆ ValueType

template<typename T >
using vtkm::exec::AtomicArrayExecutionObject< T >::ValueType = T

Constructor & Destructor Documentation

◆ AtomicArrayExecutionObject() [1/2]

template<typename T >
vtkm::exec::AtomicArrayExecutionObject< T >::AtomicArrayExecutionObject ( )
default

◆ AtomicArrayExecutionObject() [2/2]

Member Function Documentation

◆ Add()

template<typename T >
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC ValueType vtkm::exec::AtomicArrayExecutionObject< T >::Add ( vtkm::Id  index,
const ValueType value 
) const
inline

Peform an atomic addition with sequentially consistent memory ordering.

Parameters
indexThe index of the array element that will be added to.
valueThe addend of the atomic add operation.
Returns
The original value of the element at index (before addition).
Warning
Overflow behavior from this operation is undefined.

◆ CompareExchange()

template<typename T >
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC bool vtkm::exec::AtomicArrayExecutionObject< T >::CompareExchange ( vtkm::Id  index,
ValueType oldValue,
const ValueType newValue 
) const
inline

Perform an atomic compare and exchange operation with sequentially consistent memory ordering.

Parameters
indexThe index of the array element that will be atomically modified.
oldValueA pointer to the expected value of the indexed element.
newValueThe value to replace the indexed element with.
Returns
If the operation is successful, true is returned. Otherwise, oldValue is replaced with the current value of the indexed element, the element is not modified, and false is returned. In either case, oldValue becomes the value that was originally in the indexed element.

This operation is typically used in a loop. For example usage, an atomic multiplication may be implemented using compare-exchange as follows:

AtomicArrayExecutionObject<vtkm::Int32> atomicArray = ...;
// Compare-exchange multiplication:
vtkm::Int32 current = atomicArray.Get(idx); // Load the current value at idx
vtkm::Int32 newVal;
do {
newVal = current * multFactor; // the actual multiplication
} while (!atomicArray.CompareExchange(idx, &current, newVal));

The while condition here updates newVal what the proper multiplication is given the expected current value. It then compares this to the value in the array. If the values match, the operation was successful and the loop exits. If the values do not match, the value at idx was changed by another thread since the initial Get, and the compare-exchange operation failed – the target element was not modified by the compare-exchange call. If this happens, the loop body re-executes using the new value of current and tries again until it succeeds.

Note that for demonstration purposes, the previous code is unnecessarily verbose. We can express the same atomic operation more succinctly with just two lines where newVal is just computed in place.

vtkm::Int32 current = atomicArray.Get(idx); // Load the current value at idx
while (!atomicArray.CompareExchange(idx, &current, current * multFactor));

◆ Get()

template<typename T >
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC ValueType vtkm::exec::AtomicArrayExecutionObject< T >::Get ( vtkm::Id  index) const
inline

Perform an atomic load of the indexed element with acquire memory ordering.

Parameters
indexThe index of the element to load.
Returns
The value of the atomic array at index.

◆ GetNumberOfValues()

template<typename T >
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC vtkm::Id vtkm::exec::AtomicArrayExecutionObject< T >::GetNumberOfValues ( ) const
inline

◆ Set()

template<typename T >
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC void vtkm::exec::AtomicArrayExecutionObject< T >::Set ( vtkm::Id  index,
const ValueType value 
) const
inline

Peform an atomic store to memory while enforcing, at minimum, "release" memory ordering.

Parameters
indexThe index of the array element that will be added to.
valueThe value to write for the atomic store operation.
Warning
Using something like:
Set(index, Get(index)+N)

Should not be done as it is not thread safe, instead you should use the provided Add method instead.

Member Data Documentation

◆ Data

template<typename T >
ValueType* vtkm::exec::AtomicArrayExecutionObject< T >::Data { nullptr }
private

◆ NumberOfValues

template<typename T >
vtkm::Id vtkm::exec::AtomicArrayExecutionObject< T >::NumberOfValues { 0 }
private

The documentation for this class was generated from the following file:
vtkm::exec::AtomicArrayExecutionObject::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC ValueType Get(vtkm::Id index) const
Perform an atomic load of the indexed element with acquire memory ordering.
Definition: AtomicArrayExecutionObject.h:119
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::exec::AtomicArrayExecutionObject::Set
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC void Set(vtkm::Id index, const ValueType &value) const
Peform an atomic store to memory while enforcing, at minimum, "release" memory ordering.
Definition: AtomicArrayExecutionObject.h:165