Convert ArrayHandleSOA to use Buffer

Make `ArrayHandleSOA` use the new `ArrayHandle` style that uses `Buffer`
objects to manage and transfer data.
This commit is contained in:
Kenneth Moreland 2020-08-13 08:32:58 -06:00
parent 16abba7136
commit 9941db6df5
6 changed files with 311 additions and 383 deletions

@ -1352,6 +1352,19 @@ private:
}
};
std::shared_ptr<InternalsStruct> Internals;
protected:
VTKM_CONT void SetBuffer(vtkm::IdComponent index, const vtkm::cont::internal::Buffer& buffer)
{
this->Internals->Buffers[static_cast<std::size_t>(index)] = buffer;
}
// BufferContainer must be an iteratable container of Buffer objects.
template <typename BufferContainer>
VTKM_CONT void SetBuffers(const BufferContainer& buffers)
{
std::copy(buffers.begin(), buffers.end(), this->Iterators->Buffers.begin());
}
};
namespace detail

@ -19,24 +19,6 @@ namespace cont
namespace internal
{
namespace detail
{
vtkm::BufferSizeType NumberOfBytes(vtkm::Id numValues, std::size_t typeSize)
{
VTKM_ASSERT(numValues >= 0);
if (numValues > (std::numeric_limits<vtkm::BufferSizeType>::max() /
static_cast<vtkm::BufferSizeType>(typeSize)))
{
throw vtkm::cont::ErrorBadAllocation("Asking for a buffer too big to represent.");
}
return numValues * static_cast<vtkm::BufferSizeType>(typeSize);
}
} // namespace detail
#define VTKM_STORAGE_INSTANTIATE(Type) \
template class VTKM_CONT_EXPORT Storage<Type, StorageTagBasic>; \
template class VTKM_CONT_EXPORT Storage<vtkm::Vec<Type, 2>, StorageTagBasic>; \

@ -27,14 +27,6 @@ namespace cont
namespace internal
{
namespace detail
{
VTKM_CONT_EXPORT VTKM_CONT vtkm::BufferSizeType NumberOfBytes(vtkm::Id numValues,
std::size_t typeSize);
} // namespace detail
template <typename T>
class VTKM_ALWAYS_EXPORT Storage<T, vtkm::cont::StorageTagBasic>
{
@ -49,7 +41,8 @@ public:
vtkm::CopyFlag preserve,
vtkm::cont::Token& token)
{
buffers[0].SetNumberOfBytes(detail::NumberOfBytes(numValues, sizeof(T)), preserve, token);
buffers[0].SetNumberOfBytes(
vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numValues), preserve, token);
}
VTKM_CONT vtkm::Id GetNumberOfValues(const vtkm::cont::internal::Buffer* buffers)
@ -160,11 +153,11 @@ public:
vtkm::Id numberOfValues,
vtkm::cont::internal::BufferInfo::Deleter deleter,
vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::internal::MakeBuffer(vtkm::cont::DeviceAdapterTagUndefined{},
: Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
vtkm::cont::DeviceAdapterTagUndefined{},
array,
array,
internal::detail::NumberOfBytes(numberOfValues, sizeof(T)),
vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
deleter,
reallocater) })
{
@ -176,11 +169,11 @@ public:
vtkm::cont::DeviceAdapterId device,
vtkm::cont::internal::BufferInfo::Deleter deleter,
vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::internal::MakeBuffer(device,
: Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
device,
array,
array,
internal::detail::NumberOfBytes(numberOfValues, sizeof(T)),
vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
deleter,
reallocater) })
{
@ -192,11 +185,11 @@ public:
vtkm::Id numberOfValues,
vtkm::cont::internal::BufferInfo::Deleter deleter,
vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::internal::MakeBuffer(vtkm::cont::DeviceAdapterTagUndefined{},
: Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
vtkm::cont::DeviceAdapterTagUndefined{},
array,
container,
internal::detail::NumberOfBytes(numberOfValues, sizeof(T)),
vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
deleter,
reallocater) })
{
@ -209,11 +202,11 @@ public:
vtkm::cont::DeviceAdapterId device,
vtkm::cont::internal::BufferInfo::Deleter deleter,
vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::internal::MakeBuffer(device,
: Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
device,
array,
container,
internal::detail::NumberOfBytes(numberOfValues, sizeof(T)),
vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
deleter,
reallocater) })
{

@ -15,6 +15,7 @@
#include <vtkm/Math.h>
#include <vtkm/VecTraits.h>
#include <vtkm/internal/ArrayPortalBasic.h>
#include <vtkm/internal/ArrayPortalHelpers.h>
#include <vtkmstd/integer_sequence.h>
@ -33,21 +34,21 @@ namespace internal
///
/// This will only work if \c VecTraits is defined for the type.
///
template <typename ValueType_, typename SourcePortalType>
template <typename ValueType_, typename ComponentPortalType>
class ArrayPortalSOA
{
public:
using ValueType = ValueType_;
private:
using ComponentType = typename SourcePortalType::ValueType;
using ComponentType = typename ComponentPortalType::ValueType;
VTKM_STATIC_ASSERT(vtkm::HasVecTraits<ValueType>::value);
using VTraits = vtkm::VecTraits<ValueType>;
VTKM_STATIC_ASSERT((std::is_same<typename VTraits::ComponentType, ComponentType>::value));
static constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS;
SourcePortalType Portals[NUM_COMPONENTS];
ComponentPortalType Portals[NUM_COMPONENTS];
vtkm::Id NumberOfValues;
public:
@ -58,14 +59,14 @@ public:
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT void SetPortal(vtkm::IdComponent index, const SourcePortalType& portal)
VTKM_EXEC_CONT void SetPortal(vtkm::IdComponent index, const ComponentPortalType& portal)
{
this->Portals[index] = portal;
}
VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
template <typename SPT = SourcePortalType,
template <typename SPT = ComponentPortalType,
typename Supported = typename vtkm::internal::PortalSupportsGets<SPT>::type,
typename = typename std::enable_if<Supported::value>::type>
VTKM_EXEC_CONT ValueType Get(vtkm::Id valueIndex) const
@ -73,7 +74,7 @@ public:
return this->Get(valueIndex, vtkmstd::make_index_sequence<NUM_COMPONENTS>());
}
template <typename SPT = SourcePortalType,
template <typename SPT = ComponentPortalType,
typename Supported = typename vtkm::internal::PortalSupportsSets<SPT>::type,
typename = typename std::enable_if<Supported::value>::type>
VTKM_EXEC_CONT void Set(vtkm::Id valueIndex, const ValueType& value) const
@ -126,348 +127,242 @@ struct VTKM_ALWAYS_EXPORT StorageTagSOA
namespace internal
{
namespace detail
template <typename T>
class VTKM_ALWAYS_EXPORT Storage<T, vtkm::cont::StorageTagSOA>
{
VTKM_STATIC_ASSERT(vtkm::HasVecTraits<T>::value);
using VTraits = vtkm::VecTraits<T>;
template <typename ValueType, typename PortalType, typename IsTrueVec>
struct SOAPortalChooser;
using ComponentType = typename VTraits::ComponentType;
static constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS;
template <typename ValueType, typename PortalType>
struct SOAPortalChooser<ValueType, PortalType, std::true_type>
{
using Type = vtkm::internal::ArrayPortalSOA<ValueType, PortalType>;
};
public:
using ReadPortalType =
vtkm::internal::ArrayPortalSOA<T, vtkm::internal::ArrayPortalBasicRead<ComponentType>>;
using WritePortalType =
vtkm::internal::ArrayPortalSOA<T, vtkm::internal::ArrayPortalBasicWrite<ComponentType>>;
template <typename ValueType, typename PortalType>
struct SOAPortalChooser<ValueType, PortalType, std::false_type>
{
using Type = PortalType;
};
VTKM_CONT vtkm::IdComponent GetNumberOfBuffers() const { return NUM_COMPONENTS; }
template <typename ReturnType, typename ValueType, std::size_t NUM_COMPONENTS, typename PortalMaker>
ReturnType MakeSOAPortal(std::array<vtkm::cont::ArrayHandle<ValueType, vtkm::cont::StorageTagBasic>,
NUM_COMPONENTS> arrays,
vtkm::Id numValues,
const PortalMaker& portalMaker)
VTKM_CONT void ResizeBuffers(vtkm::Id numValues,
vtkm::cont::internal::Buffer* buffers,
vtkm::CopyFlag preserve,
vtkm::cont::Token& token)
{
ReturnType portal(numValues);
for (std::size_t componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
vtkm::BufferSizeType numBytes =
vtkm::internal::NumberOfValuesToNumberOfBytes<ComponentType>(numValues);
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
portal.SetPortal(static_cast<vtkm::IdComponent>(componentIndex),
portalMaker(arrays[componentIndex]));
VTKM_ASSERT(arrays[componentIndex].GetNumberOfValues() == numValues);
buffers[componentIndex].SetNumberOfBytes(numBytes, preserve, token);
}
}
VTKM_CONT vtkm::Id GetNumberOfValues(const vtkm::cont::internal::Buffer* buffers)
{
// Assume all buffers are the same size.
return static_cast<vtkm::Id>(buffers[0].GetNumberOfBytes()) /
static_cast<vtkm::Id>(sizeof(ComponentType));
}
VTKM_CONT ReadPortalType CreateReadPortal(const vtkm::cont::internal::Buffer* buffers,
vtkm::cont::DeviceAdapterId device,
vtkm::cont::Token& token)
{
vtkm::Id numValues = this->GetNumberOfValues(buffers);
ReadPortalType portal(numValues);
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
VTKM_ASSERT(buffers[0].GetNumberOfBytes() == buffers[componentIndex].GetNumberOfBytes());
portal.SetPortal(componentIndex,
vtkm::internal::ArrayPortalBasicRead<ComponentType>(
reinterpret_cast<const ComponentType*>(
buffers[componentIndex].ReadPointerDevice(device, token)),
numValues));
}
return portal;
}
template <typename ReturnType, typename ValueType, typename PortalMaker>
ReturnType MakeSOAPortal(
std::array<vtkm::cont::ArrayHandle<ValueType, vtkm::cont::StorageTagBasic>, 1> arrays,
vtkm::Id vtkmNotUsed(numValues),
const PortalMaker& portalMaker)
{
return portalMaker(arrays[0]);
}
} // namespace detail
template <typename ValueType>
struct ArrayHandleSOATraits
{
using VTraits = vtkm::VecTraits<ValueType>;
using ComponentType = typename VTraits::ComponentType;
using BaseArrayType = vtkm::cont::ArrayHandle<ComponentType, vtkm::cont::StorageTagBasic>;
static constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS;
VTKM_STATIC_ASSERT_MSG(NUM_COMPONENTS > 0,
"ArrayHandleSOA requires a type with at least 1 component.");
using IsTrueVec = std::integral_constant<bool, (NUM_COMPONENTS > 1)>;
using PortalControl = typename detail::
SOAPortalChooser<ValueType, typename BaseArrayType::WritePortalType, IsTrueVec>::Type;
using PortalConstControl = typename detail::
SOAPortalChooser<ValueType, typename BaseArrayType::ReadPortalType, IsTrueVec>::Type;
template <typename Device>
using PortalExecution = typename detail::SOAPortalChooser<
ValueType,
typename BaseArrayType::template ExecutionTypes<Device>::Portal,
IsTrueVec>::Type;
template <typename Device>
using PortalConstExecution = typename detail::SOAPortalChooser<
ValueType,
typename BaseArrayType::template ExecutionTypes<Device>::PortalConst,
IsTrueVec>::Type;
};
template <typename ValueType_>
class Storage<ValueType_, vtkm::cont::StorageTagSOA>
{
using Traits = ArrayHandleSOATraits<ValueType_>;
static constexpr vtkm::IdComponent NUM_COMPONENTS = Traits::NUM_COMPONENTS;
using BaseArrayType = typename Traits::BaseArrayType;
std::array<BaseArrayType, NUM_COMPONENTS> Arrays;
VTKM_CONT bool IsValidImpl(std::true_type) const
{
vtkm::Id size = this->Arrays[0].GetNumberOfValues();
for (vtkm::IdComponent componentIndex = 1; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
if (this->GetArray(componentIndex).GetNumberOfValues() != size)
{
return false;
}
}
return true;
}
VTKM_CONT constexpr bool IsValidImpl(std::false_type) const { return true; }
public:
using ValueType = ValueType_;
using PortalType = typename Traits::PortalControl;
using PortalConstType = typename Traits::PortalConstControl;
VTKM_CONT bool IsValid() const { return this->IsValidImpl(typename Traits::IsTrueVec{}); }
Storage() = default;
VTKM_CONT Storage(std::array<BaseArrayType, NUM_COMPONENTS>&& arrays)
: Arrays(std::move(arrays))
{
VTKM_ASSERT(IsValid());
}
// For this constructor to work, all types have to be
// vtkm::cont::ArrayHandle<ValueType, StorageTagBasic>
template <typename... ArrayTypes>
VTKM_CONT Storage(const BaseArrayType& array0, const ArrayTypes&... arrays)
: Arrays{ { array0, arrays... } }
{
VTKM_ASSERT(IsValid());
}
VTKM_CONT BaseArrayType& GetArray(vtkm::IdComponent index)
{
return this->Arrays[static_cast<std::size_t>(index)];
}
VTKM_CONT const BaseArrayType& GetArray(vtkm::IdComponent index) const
{
return this->Arrays[static_cast<std::size_t>(index)];
}
VTKM_CONT std::array<BaseArrayType, NUM_COMPONENTS>& GetArrays() { return this->Arrays; }
VTKM_CONT const std::array<BaseArrayType, NUM_COMPONENTS>& GetArrays() const
{
return this->Arrays;
}
VTKM_CONT void SetArray(vtkm::IdComponent index, const BaseArrayType& array)
{
this->Arrays[static_cast<std::size_t>(index)] = array;
}
VTKM_CONT vtkm::Id GetNumberOfValues() const
{
VTKM_ASSERT(IsValid());
return this->GetArray(0).GetNumberOfValues();
}
VTKM_CONT PortalType GetPortal()
{
VTKM_ASSERT(this->IsValid());
return detail::MakeSOAPortal<PortalType>(
this->Arrays, this->GetNumberOfValues(), [](BaseArrayType& array) {
return array.WritePortal();
});
}
VTKM_CONT PortalConstType GetPortalConst() const
{
VTKM_ASSERT(this->IsValid());
return detail::MakeSOAPortal<PortalConstType>(
this->Arrays, this->GetNumberOfValues(), [](const BaseArrayType& array) {
return array.ReadPortal();
});
}
VTKM_CONT void Allocate(vtkm::Id numValues)
VTKM_CONT WritePortalType CreateWritePortal(vtkm::cont::internal::Buffer* buffers,
vtkm::cont::DeviceAdapterId device,
vtkm::cont::Token& token)
{
vtkm::Id numValues = this->GetNumberOfValues(buffers);
WritePortalType portal(numValues);
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->GetArray(componentIndex).Allocate(numValues);
}
}
VTKM_CONT void Shrink(vtkm::Id numValues)
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->GetArray(componentIndex).Shrink(numValues);
}
}
VTKM_CONT void ReleaseResources()
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->GetArray(componentIndex).ReleaseResources();
}
}
};
template <typename ValueType_, typename Device>
class ArrayTransfer<ValueType_, vtkm::cont::StorageTagSOA, Device>
{
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
using StorageType = vtkm::cont::internal::Storage<ValueType_, vtkm::cont::StorageTagSOA>;
using Traits = ArrayHandleSOATraits<ValueType_>;
using BaseArrayType = typename Traits::BaseArrayType;
static constexpr vtkm::IdComponent NUM_COMPONENTS = Traits::NUM_COMPONENTS;
StorageType* Storage;
public:
using ValueType = ValueType_;
using PortalControl = typename StorageType::PortalType;
using PortalConstControl = typename StorageType::PortalConstType;
using PortalExecution = typename Traits::template PortalExecution<Device>;
using PortalConstExecution = typename Traits::template PortalConstExecution<Device>;
VTKM_CONT ArrayTransfer(StorageType* storage)
: Storage(storage)
{
}
VTKM_CONT vtkm::Id GetNumberOfValues() const { return this->Storage->GetNumberOfValues(); }
VTKM_CONT PortalConstExecution PrepareForInput(bool vtkmNotUsed(updateData),
vtkm::cont::Token& token) const
{
return detail::MakeSOAPortal<PortalConstExecution>(
this->Storage->GetArrays(), this->GetNumberOfValues(), [&token](const BaseArrayType& array) {
return array.PrepareForInput(Device{}, token);
});
}
VTKM_CONT PortalExecution PrepareForInPlace(bool vtkmNotUsed(updateData),
vtkm::cont::Token& token) const
{
return detail::MakeSOAPortal<PortalExecution>(
this->Storage->GetArrays(), this->GetNumberOfValues(), [&token](BaseArrayType& array) {
return array.PrepareForInPlace(Device{}, token);
});
}
VTKM_CONT PortalExecution PrepareForOutput(vtkm::Id numValues, vtkm::cont::Token& token) const
{
return detail::MakeSOAPortal<PortalExecution>(
this->Storage->GetArrays(), numValues, [numValues, &token](BaseArrayType& array) {
return array.PrepareForOutput(numValues, Device{}, token);
});
}
VTKM_CONT
void RetrieveOutputData(StorageType* vtkmNotUsed(storage)) const
{
// Implementation of this method should be unnecessary. The internal
// array handle should automatically retrieve the output data as
// necessary.
}
VTKM_CONT void Shrink(vtkm::Id numValues)
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->Storage->GetArray(componentIndex).Shrink(numValues);
}
}
VTKM_CONT void ReleaseResources()
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->Storage->GetArray(componentIndex).ReleaseResourcesExecution();
VTKM_ASSERT(buffers[0].GetNumberOfBytes() == buffers[componentIndex].GetNumberOfBytes());
portal.SetPortal(componentIndex,
vtkm::internal::ArrayPortalBasicWrite<ComponentType>(
reinterpret_cast<ComponentType*>(
buffers[componentIndex].WritePointerDevice(device, token)),
numValues));
}
return portal;
}
};
} // namespace internal
/// \brief An \c ArrayHandle that for Vecs stores each component in a separate physical array.
///
/// \c ArrayHandleSOA behaves like a regular \c ArrayHandle (with a basic storage) except that
/// if you specify a \c ValueType of a \c Vec or a \c Vec-like, it will actually store each
/// component in a separate physical array. When data are retrieved from the array, they are
/// reconstructed into \c Vec objects as expected.
///
/// The intention of this array type is to help cover the most common ways data is lain out in
/// memory. Typically, arrays of data are either an "array of structures" like the basic storage
/// where you have a single array of structures (like \c Vec) or a "structure of arrays" where
/// you have an array of a basic type (like \c float) for each component of the data being
/// represented. The\c ArrayHandleSOA makes it easy to cover this second case without creating
/// special types.
///
/// \c ArrayHandleSOA can be constructed from a collection of \c ArrayHandle with basic storage.
/// This allows you to construct \c Vec arrays from components without deep copies.
///
template <typename ValueType_>
class ArrayHandleSOA : public ArrayHandle<ValueType_, vtkm::cont::StorageTagSOA>
// This can go away once ArrayHandle is replaced with ArrayHandleNewStyle
template <typename T>
class VTKM_ALWAYS_EXPORT ArrayHandle<T, vtkm::cont::StorageTagSOA>
: public ArrayHandleNewStyle<T, vtkm::cont::StorageTagSOA>
{
using Traits = vtkm::cont::internal::ArrayHandleSOATraits<ValueType_>;
using ComponentType = typename Traits::ComponentType;
using BaseArrayType = typename Traits::BaseArrayType;
using StorageType = vtkm::cont::internal::Storage<ValueType_, vtkm::cont::StorageTagSOA>;
using Superclass = ArrayHandleNewStyle<T, vtkm::cont::StorageTagSOA>;
public:
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleSOA,
(ArrayHandleSOA<ValueType_>),
(ArrayHandle<ValueType_, vtkm::cont::StorageTagSOA>));
ArrayHandleSOA(std::array<BaseArrayType, Traits::NUM_COMPONENTS>&& componentArrays)
: Superclass(StorageType(std::move(componentArrays)))
VTKM_CONT
ArrayHandle()
: Superclass()
{
}
ArrayHandleSOA(std::initializer_list<BaseArrayType>&& componentArrays)
VTKM_CONT
ArrayHandle(const ArrayHandle<T, vtkm::cont::StorageTagSOA>& src)
: Superclass(src)
{
VTKM_ASSERT(componentArrays.size() == Traits::NUM_COMPONENTS);
std::copy(
componentArrays.begin(), componentArrays.end(), this->GetStorage().GetArrays().begin());
}
VTKM_CONT
ArrayHandle(ArrayHandle<T, vtkm::cont::StorageTagSOA>&& src) noexcept
: Superclass(std::move(src))
{
}
VTKM_CONT
ArrayHandle(const ArrayHandleNewStyle<T, vtkm::cont::StorageTagSOA>& src)
: Superclass(src)
{
}
VTKM_CONT
ArrayHandle(ArrayHandleNewStyle<T, vtkm::cont::StorageTagSOA>&& src) noexcept
: Superclass(std::move(src))
{
}
VTKM_CONT ArrayHandle(
const vtkm::cont::internal::Buffer* buffers,
const typename Superclass::StorageType& storage = typename Superclass::StorageType())
: Superclass(buffers, storage)
{
}
VTKM_CONT ArrayHandle(
const std::vector<vtkm::cont::internal::Buffer>& buffers,
const typename Superclass::StorageType& storage = typename Superclass::StorageType())
: Superclass(buffers, storage)
{
}
VTKM_CONT
ArrayHandle<T, vtkm::cont::StorageTagSOA>& operator=(
const ArrayHandle<T, vtkm::cont::StorageTagSOA>& src)
{
this->Superclass::operator=(src);
return *this;
}
VTKM_CONT
ArrayHandle<T, vtkm::cont::StorageTagSOA>& operator=(
ArrayHandle<T, vtkm::cont::StorageTagSOA>&& src) noexcept
{
this->Superclass::operator=(std::move(src));
return *this;
}
VTKM_CONT ~ArrayHandle() {}
};
/// \brief An `ArrayHandle` that for Vecs stores each component in a separate physical array.
///
/// `ArrayHandleSOA` behaves like a regular `ArrayHandle` (with a basic storage) except that
/// if you specify a `ValueType` of a `Vec` or a `Vec-like`, it will actually store each
/// component in a separate physical array. When data are retrieved from the array, they are
/// reconstructed into `Vec` objects as expected.
///
/// The intention of this array type is to help cover the most common ways data is lain out in
/// memory. Typically, arrays of data are either an "array of structures" like the basic storage
/// where you have a single array of structures (like `Vec`) or a "structure of arrays" where
/// you have an array of a basic type (like `float`) for each component of the data being
/// represented. The `ArrayHandleSOA` makes it easy to cover this second case without creating
/// special types.
///
/// `ArrayHandleSOA` can be constructed from a collection of `ArrayHandle` with basic storage.
/// This allows you to construct `Vec` arrays from components without deep copies.
///
template <typename T>
class ArrayHandleSOA : public ArrayHandle<T, vtkm::cont::StorageTagSOA>
{
using ComponentType = typename vtkm::VecTraits<T>::ComponentType;
static constexpr vtkm::IdComponent NUM_COMPONENTS = vtkm::VecTraits<T>::NUM_COMPONENTS;
using StorageType = vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagSOA>;
using ComponentArrayType = vtkm::cont::ArrayHandle<ComponentType, vtkm::cont::StorageTagBasic>;
public:
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleSOA,
(ArrayHandleSOA<T>),
(ArrayHandle<T, vtkm::cont::StorageTagSOA>));
ArrayHandleSOA(std::initializer_list<vtkm::cont::internal::Buffer>&& componentBuffers)
: Superclass(std::move(componentBuffers))
{
}
ArrayHandleSOA(const std::array<ComponentArrayType, NUM_COMPONENTS>& componentArrays)
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->SetArray(componentIndex, componentArrays[componentIndex]);
}
}
ArrayHandleSOA(const std::vector<ComponentArrayType>& componentArrays)
{
VTKM_ASSERT(componentArrays.size() == NUM_COMPONENTS);
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
this->SetArray(componentIndex, componentArrays[componentIndex]);
}
}
ArrayHandleSOA(std::initializer_list<ComponentArrayType>&& componentArrays)
{
VTKM_ASSERT(componentArrays.size() == NUM_COMPONENTS);
vtkm::IdComponent componentIndex = 0;
for (auto&& array : componentArrays)
{
this->SetArray(componentIndex, array);
++componentIndex;
}
}
ArrayHandleSOA(std::initializer_list<std::vector<ComponentType>>&& componentVectors)
{
VTKM_ASSERT(componentVectors.size() == Traits::NUM_COMPONENTS);
VTKM_ASSERT(componentVectors.size() == NUM_COMPONENTS);
vtkm::IdComponent componentIndex = 0;
for (auto&& vectorIter = componentVectors.begin(); vectorIter != componentVectors.end();
++vectorIter)
for (auto&& vector : componentVectors)
{
// Note, std::vectors that come from std::initializer_list must be copied because the scope
// of the objects in the initializer list disappears.
this->SetArray(componentIndex, vtkm::cont::make_ArrayHandle(*vectorIter, vtkm::CopyFlag::On));
this->SetArray(componentIndex, vtkm::cont::make_ArrayHandle(vector, vtkm::CopyFlag::On));
++componentIndex;
}
}
// This only works if all the templated arguments are of type std::vector<ComponentType>.
template <typename... RemainingVectors>
template <typename Allocator, typename... RemainingVectors>
ArrayHandleSOA(vtkm::CopyFlag copy,
const std::vector<ComponentType>& vector0,
const std::vector<ComponentType, Allocator>& vector0,
RemainingVectors&&... componentVectors)
: Superclass(StorageType(
vtkm::cont::make_ArrayHandle(vector0, copy),
vtkm::cont::make_ArrayHandle(std::forward<RemainingVectors>(componentVectors), copy)...))
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::make_ArrayHandle(vector0, copy).GetBuffers()[0],
vtkm::cont::make_ArrayHandle(std::forward<RemainingVectors>(componentVectors), copy)
.GetBuffers()[0]... })
{
VTKM_STATIC_ASSERT(sizeof...(RemainingVectors) + 1 == Traits::NUM_COMPONENTS);
VTKM_STATIC_ASSERT(sizeof...(RemainingVectors) + 1 == NUM_COMPONENTS);
}
// This only works if all the templated arguments are of type std::vector<ComponentType>.
@ -475,11 +370,12 @@ public:
ArrayHandleSOA(vtkm::CopyFlag copy,
std::vector<ComponentType>&& vector0,
RemainingVectors&&... componentVectors)
: Superclass(StorageType(
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::make_ArrayHandle(std::move(vector0), copy),
vtkm::cont::make_ArrayHandle(std::forward<RemainingVectors>(componentVectors), copy)...))
vtkm::cont::make_ArrayHandle(std::forward<RemainingVectors>(componentVectors), copy)
.GetBuffers()[0]... })
{
VTKM_STATIC_ASSERT(sizeof...(RemainingVectors) + 1 == Traits::NUM_COMPONENTS);
VTKM_STATIC_ASSERT(sizeof...(RemainingVectors) + 1 == NUM_COMPONENTS);
}
// This only works if all the templated arguments are of type std::vector<ComponentType>.
@ -491,18 +387,20 @@ public:
#endif
ArrayHandleSOA(const std::vector<ComponentType>& vector0,
const RemainingVectors&... componentVectors)
: Superclass(
StorageType(vtkm::cont::make_ArrayHandle(vector0, vtkm::CopyFlag::Off),
vtkm::cont::make_ArrayHandle(componentVectors, vtkm::CopyFlag::Off)...))
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::make_ArrayHandle(vector0, vtkm::CopyFlag::Off).GetBuffers()[0],
vtkm::cont::make_ArrayHandle(std::forward<RemainingVectors>(componentVectors),
vtkm::CopyFlag::Off)
.GetBuffers()[0]... })
{
VTKM_STATIC_ASSERT(sizeof...(RemainingVectors) + 1 == Traits::NUM_COMPONENTS);
VTKM_STATIC_ASSERT(sizeof...(RemainingVectors) + 1 == NUM_COMPONENTS);
}
ArrayHandleSOA(std::initializer_list<const ComponentType*> componentArrays,
vtkm::Id length,
vtkm::CopyFlag copy)
{
VTKM_ASSERT(componentArrays.size() == Traits::NUM_COMPONENTS);
VTKM_ASSERT(componentArrays.size() == NUM_COMPONENTS);
vtkm::IdComponent componentIndex = 0;
for (auto&& vectorIter = componentArrays.begin(); vectorIter != componentArrays.end();
++vectorIter)
@ -515,7 +413,7 @@ public:
VTKM_DEPRECATED(1.6, "Specify a vtkm::CopyFlag or use a move version of make_ArrayHandle.")
ArrayHandleSOA(std::initializer_list<const ComponentType*> componentArrays, vtkm::Id length)
{
VTKM_ASSERT(componentArrays.size() == Traits::NUM_COMPONENTS);
VTKM_ASSERT(componentArrays.size() == NUM_COMPONENTS);
vtkm::IdComponent componentIndex = 0;
for (auto&& vectorIter = componentArrays.begin(); vectorIter != componentArrays.end();
++vectorIter)
@ -532,10 +430,11 @@ public:
vtkm::CopyFlag copy,
const ComponentType* array0,
const RemainingArrays&... componentArrays)
: Superclass(StorageType(vtkm::cont::make_ArrayHandle(array0, length, copy),
vtkm::cont::make_ArrayHandle(componentArrays, length, copy)...))
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::make_ArrayHandle(array0, length, copy).GetBuffers()[0],
vtkm::cont::make_ArrayHandle(componentArrays, length, copy).GetBuffers()[0]... })
{
VTKM_STATIC_ASSERT(sizeof...(RemainingArrays) + 1 == Traits::NUM_COMPONENTS);
VTKM_STATIC_ASSERT(sizeof...(RemainingArrays) + 1 == NUM_COMPONENTS);
}
// This only works if all the templated arguments are of type std::vector<ComponentType>.
@ -548,26 +447,22 @@ public:
ArrayHandleSOA(vtkm::Id length,
const ComponentType* array0,
const RemainingArrays&... componentArrays)
: Superclass(
StorageType(vtkm::cont::make_ArrayHandle(array0, length, vtkm::CopyFlag::Off),
vtkm::cont::make_ArrayHandle(componentArrays, length, vtkm::CopyFlag::Off)...))
: Superclass(std::vector<vtkm::cont::internal::Buffer>{
vtkm::cont::make_ArrayHandle(array0, length, vtkm::CopyFlag::Off).GetBuffers()[0],
vtkm::cont::make_ArrayHandle(componentArrays, length, vtkm::CopyFlag::Off)
.GetBuffers()[0]... })
{
VTKM_STATIC_ASSERT(sizeof...(RemainingArrays) + 1 == Traits::NUM_COMPONENTS);
VTKM_STATIC_ASSERT(sizeof...(RemainingArrays) + 1 == NUM_COMPONENTS);
}
VTKM_CONT BaseArrayType& GetArray(vtkm::IdComponent index)
VTKM_CONT vtkm::cont::ArrayHandleBasic<ComponentType> GetArray(vtkm::IdComponent index) const
{
return this->GetStorage().GetArray(index);
return ComponentArrayType(&this->GetBuffers()[index]);
}
VTKM_CONT const BaseArrayType& GetArray(vtkm::IdComponent index) const
VTKM_CONT void SetArray(vtkm::IdComponent index, const ComponentArrayType& array)
{
return this->GetStorage().GetArray(index);
}
VTKM_CONT void SetArray(vtkm::IdComponent index, const BaseArrayType& array)
{
this->GetStorage().SetArray(index, array);
this->SetBuffer(index, array.GetBuffers()[0]);
}
};
@ -734,25 +629,24 @@ template <typename ValueType>
struct Serialization<vtkm::cont::ArrayHandleSOA<ValueType>>
{
using BaseType = vtkm::cont::ArrayHandle<ValueType, vtkm::cont::StorageTagSOA>;
using Traits = vtkm::cont::internal::ArrayHandleSOATraits<ValueType>;
static constexpr vtkm::IdComponent NUM_COMPONENTS = Traits::NUM_COMPONENTS;
static constexpr vtkm::IdComponent NUM_COMPONENTS = vtkm::VecTraits<ValueType>::NUM_COMPONENTS;
static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
vtkmdiy::save(bb, obj.GetStorage().GetArray(componentIndex));
vtkmdiy::save(bb, obj.GetBuffers()[componentIndex]);
}
}
static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
{
for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
std::vector<vtkm::cont::internal::Buffer> buffers(NUM_COMPONENTS);
for (std::size_t componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex)
{
typename Traits::BaseArrayType componentArray;
vtkmdiy::load(bb, componentArray);
obj.GetStorage().SetArray(componentIndex, componentArray);
vtkmdiy::load(bb, buffers[componentIndex]);
}
obj = BaseType(buffers);
}
};

@ -22,6 +22,26 @@
#include <deque>
#include <map>
namespace vtkm
{
namespace internal
{
vtkm::BufferSizeType NumberOfValuesToNumberOfBytes(vtkm::Id numValues, std::size_t typeSize)
{
VTKM_ASSERT(numValues >= 0);
if (numValues > (std::numeric_limits<vtkm::BufferSizeType>::max() /
static_cast<vtkm::BufferSizeType>(typeSize)))
{
throw vtkm::cont::ErrorBadAllocation("Asking for a buffer too big to represent.");
}
return numValues * static_cast<vtkm::BufferSizeType>(typeSize);
}
}
} // namespace vtkm::internal
namespace
{

@ -23,6 +23,32 @@
namespace vtkm
{
namespace internal
{
///@{
/// \brief Convert the number of values of a type to the number of bytes needed to store it.
///
/// A convenience function that takes the number of values in an array and either the type or the
/// size of the type and safely converts that to the number of bytes required to store the array.
///
/// This function can throw an `vtkm::cont::ErrorBadAllocation` if the number of bytes cannot be
/// stored in the returned `vtkm::BufferSizeType`. (That would be a huge array and probably
/// indicative of an error.)
///
VTKM_CONT_EXPORT vtkm::BufferSizeType NumberOfValuesToNumberOfBytes(vtkm::Id numValues,
std::size_t typeSize);
template <typename T>
vtkm::BufferSizeType NumberOfValuesToNumberOfBytes(vtkm::Id numValues)
{
return NumberOfValuesToNumberOfBytes(numValues, sizeof(T));
}
///@}
} // namespace internal
namespace cont
{
namespace internal