vtk-m2/vtkm/exec/AtomicArray.h

97 lines
3.0 KiB
C
Raw Normal View History

2016-02-10 15:51:31 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
2016-02-10 15:51:31 +00:00
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
2016-02-10 15:51:31 +00:00
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_exec_AtomicArray_h
#define vtk_m_exec_AtomicArray_h
#include <vtkm/ListTag.h>
2016-02-10 15:51:31 +00:00
#include <vtkm/cont/ArrayHandle.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/cont/ExecutionObjectFactoryBase.h>
2016-02-10 15:51:31 +00:00
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace exec
{
2016-02-10 15:51:31 +00:00
/// \brief A type list containing types that can be used with an AtomicArray.
///
2017-05-18 14:29:41 +00:00
struct AtomicArrayTypeListTag : vtkm::ListTagBase<vtkm::Int32, vtkm::Int64>
{
};
/// A class that can be used to atomically operate on an array of values safely
/// across multiple instances of the same worklet. This is useful when you have
/// an algorithm that needs to accumulate values in parallel, but writing out a
/// value per worklet might be memory prohibitive.
2016-02-10 15:51:31 +00:00
///
/// To construct an AtomicArray you will need to pass in an
/// vtkm::cont::ArrayHandle that is used as the underlying storage for the
/// AtomicArray
2016-02-10 15:51:31 +00:00
///
/// Supported Operations: add / compare and swap (CAS)
///
/// Supported Types: 32 / 64 bit signed integers
///
///
2017-05-18 14:29:41 +00:00
template <typename T, typename DeviceAdapterTag>
class AtomicArray : public vtkm::cont::ExecutionObjectFactoryBase
2016-02-10 15:51:31 +00:00
{
public:
using ValueType = T;
VTKM_CONT
AtomicArray()
: AtomicImplementation((vtkm::cont::ArrayHandle<T>()))
2017-05-18 14:29:41 +00:00
{
}
2017-05-18 14:29:41 +00:00
template <typename StorageType>
VTKM_CONT AtomicArray(vtkm::cont::ArrayHandle<T, StorageType> handle)
: AtomicImplementation(handle)
2016-02-10 15:51:31 +00:00
{
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC
2016-02-10 15:51:31 +00:00
T Add(vtkm::Id index, const T& value) const
{
2017-05-18 14:29:41 +00:00
return this->AtomicImplementation.Add(index, value);
2016-02-10 15:51:31 +00:00
}
2016-03-08 17:58:20 +00:00
//
// Compare and Swap is an atomic exchange operation. If the value at
2016-03-08 17:58:20 +00:00
// the index is equal to oldValue, then newValue is written to the index.
// The operation was successful if return value is equal to oldValue
2016-03-08 17:58:20 +00:00
//
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC
T CompareAndSwap(vtkm::Id index, const T& newValue, const T& oldValue) const
{
2017-05-18 14:29:41 +00:00
return this->AtomicImplementation.CompareAndSwap(index, newValue, oldValue);
}
2016-02-10 15:51:31 +00:00
private:
2017-05-18 14:29:41 +00:00
vtkm::cont::DeviceAdapterAtomicArrayImplementation<T, DeviceAdapterTag> AtomicImplementation;
2016-02-10 15:51:31 +00:00
};
}
} // namespace vtkm::exec
#endif //vtk_m_exec_AtomicArray_h