Add ArrayHandleRecombineVec

The primary purpose of `ArrayHandleRecombineVec` is to take arrays
returned from `ArrayExtractComponent` and recombine them again into a
single `ArrayHandle` that has `Vec` values.
This commit is contained in:
Kenneth Moreland 2020-12-23 08:48:13 -07:00
parent d2dd60e4ed
commit 755af739e3
7 changed files with 606 additions and 91 deletions

@ -0,0 +1,470 @@
//============================================================================
// 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.
//============================================================================
#ifndef vtk_m_cont_ArrayHandleRecombineVec_h
#define vtk_m_cont_ArrayHandleRecombineVec_h
#include <vtkm/cont/ArrayExtractComponent.h>
#include <vtkm/cont/ArrayHandleMultiplexer.h>
#include <vtkm/cont/ArrayHandleStride.h>
#include <vtkm/cont/DeviceAdapterTag.h>
#include <vtkm/VecVariable.h>
#include <vtkm/internal/ArrayPortalValueReference.h>
namespace vtkm
{
namespace internal
{
template <typename PortalType>
class RecombineVec
{
vtkm::VecCConst<PortalType> Portals;
vtkm::Id Index;
public:
using ComponentType = typename std::remove_const<typename PortalType::ValueType>::type;
VTKM_EXEC_CONT RecombineVec(const vtkm::VecCConst<PortalType>& portals, vtkm::Id index)
: Portals(portals)
, Index(index)
{
}
VTKM_EXEC_CONT vtkm::IdComponent GetNumberOfComponents() const
{
return this->Portals.GetNumberOfComponents();
}
VTKM_EXEC_CONT
vtkm::internal::ArrayPortalValueReference<PortalType> operator[](vtkm::IdComponent cIndex) const
{
return vtkm::internal::ArrayPortalValueReference<PortalType>(this->Portals[cIndex],
this->Index);
}
template <typename T, vtkm::IdComponent DestSize>
VTKM_EXEC_CONT void CopyInto(vtkm::Vec<T, DestSize>& dest) const
{
vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->GetNumberOfComponents());
for (vtkm::IdComponent cIndex = 0; cIndex < numComponents; ++cIndex)
{
dest[cIndex] = this->Portals[cIndex].Get(this->Index);
}
// Clear out any components not held by this dynamic Vec-like
for (vtkm::IdComponent cIndex = numComponents; cIndex < DestSize; ++cIndex)
{
dest[cIndex] = vtkm::TypeTraits<T>::ZeroInitialization();
}
}
VTKM_EXEC_CONT vtkm::Id GetIndex() const { return this->Index; }
template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
VTKM_EXEC_CONT RecombineVec& operator=(const T& src)
{
using VTraits = vtkm::VecTraits<T>;
vtkm::IdComponent numComponents = VTraits::GetNumberOfComponents(src);
if (numComponents > this->GetNumberOfComponents())
{
numComponents = this->GetNumberOfComponents();
}
for (vtkm::IdComponent cIndex = 0; cIndex < numComponents; ++cIndex)
{
this->Portals[cIndex].Set(this->Index, VTraits::GetComponent(src, cIndex));
}
return *this;
}
VTKM_EXEC_CONT operator ComponentType() const { return this->Portals[0].Get(this->Index); }
template <vtkm::IdComponent N>
VTKM_EXEC_CONT operator vtkm::Vec<ComponentType, N>() const
{
vtkm::Vec<ComponentType, N> result;
this->CopyInto(result);
return result;
}
};
} // namespace internal
template <typename PortalType>
struct TypeTraits<vtkm::internal::RecombineVec<PortalType>>
{
private:
using VecType = vtkm::internal::RecombineVec<PortalType>;
using ComponentType = typename VecType::ComponentType;
public:
using NumericTag = typename vtkm::TypeTraits<ComponentType>::NumericTag;
using DimensionalityTag = vtkm::TypeTraitsVectorTag;
VTKM_EXEC_CONT static vtkm::internal::RecombineVec<PortalType> ZeroInitialization()
{
// Return a vec-like of size 0.
return vtkm::internal::RecombineVec<PortalType>{};
}
};
template <typename PortalType>
struct VecTraits<vtkm::internal::RecombineVec<PortalType>>
{
using VecType = vtkm::internal::RecombineVec<PortalType>;
using ComponentType = typename VecType::ComponentType;
using BaseComponentType = typename vtkm::VecTraits<ComponentType>::BaseComponentType;
using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;
using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;
VTKM_EXEC_CONT static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
{
return vector.GetNumberOfComponents();
}
VTKM_EXEC_CONT
static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
{
return vector[componentIndex];
}
template <vtkm::IdComponent destSize>
VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
{
src.CopyInto(dest);
}
};
namespace internal
{
template <typename SourcePortalType>
class ArrayPortalRecombineVec
{
vtkm::VecCConst<SourcePortalType> Portals;
public:
using ValueType = vtkm::internal::RecombineVec<SourcePortalType>;
ArrayPortalRecombineVec() = default;
ArrayPortalRecombineVec(const vtkm::VecCConst<SourcePortalType>& portals)
: Portals(portals)
{
}
ArrayPortalRecombineVec(const SourcePortalType* portals, vtkm::IdComponent numComponents)
: Portals(portals, numComponents)
{
}
VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->Portals[0].GetNumberOfValues(); }
VTKM_EXEC_CONT ValueType Get(vtkm::Id index) const { return ValueType(this->Portals, index); }
VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
{
// The ValueType is actually a reference back to the portals, and sets to it should
// already be set in the portal. Thus, we don't really need to do anything.
VTKM_ASSERT(value.GetIndex() == index);
}
template <typename T>
VTKM_EXEC_CONT void Set(vtkm::Id index, const T& value) const
{
using Traits = vtkm::VecTraits<T>;
VTKM_ASSERT(Traits::GetNumberOfComponents(value) == this->Portals.GetNumberOfComponents());
for (vtkm::IdComponent cIndex = 0; cIndex < this->Portals.GetNumberOfComponents(); ++cIndex)
{
this->Portals[cIndex].Set(index, Traits::GetComponent(value, cIndex));
}
}
};
}
} // namespace vtkm::internal
namespace vtkm
{
namespace cont
{
namespace internal
{
struct StorageTagRecombineVec
{
};
namespace detail
{
// Note: Normally a decorating ArrayHandle holds the buffers of the arrays it is decorating
// in its list of arrays. However, the numbers of buffers is expected to be compile-time static
// and ArrayHandleRecombineVec needs to set the number of buffers at runtime. We cheat around
// this by stuffing the decorated buffers in the metadata. To make sure deep copies work
// right, a copy of the metadata results in a deep copy of the contained buffers. The
// vtkm::cont::internal::Buffer holding the metadata is not supposed to copy the metadata
// except for a deep copy (and when it is first set). If this behavior changes, there could
// be a performance degredation.
struct RecombineVecMetaData
{
mutable std::vector<vtkm::cont::internal::Buffer> PortalBuffers;
std::vector<std::vector<vtkm::cont::internal::Buffer>> ArrayBuffers;
RecombineVecMetaData() = default;
RecombineVecMetaData(const RecombineVecMetaData& src) { *this = src; }
RecombineVecMetaData& operator=(const RecombineVecMetaData& src)
{
this->ArrayBuffers.resize(src.ArrayBuffers.size());
for (std::size_t arrayIndex = 0; arrayIndex < src.ArrayBuffers.size(); ++arrayIndex)
{
this->ArrayBuffers[arrayIndex].resize(src.ArrayBuffers[arrayIndex].size());
for (std::size_t bufferIndex = 0; bufferIndex < src.ArrayBuffers[arrayIndex].size();
++bufferIndex)
{
this->ArrayBuffers[arrayIndex][bufferIndex].DeepCopyFrom(
src.ArrayBuffers[arrayIndex][bufferIndex]);
}
}
this->PortalBuffers.clear();
// Intentionally not copying portals. Portals will be recreated from proper array when requsted.
return *this;
}
};
template <typename T>
using RecombinedPortalType = vtkm::internal::ArrayPortalMultiplexer<
typename vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagStride>::ReadPortalType,
typename vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagStride>::WritePortalType>;
template <typename T>
using RecombinedValueType = vtkm::internal::RecombineVec<RecombinedPortalType<T>>;
} // namespace detail
template <typename ReadWritePortal>
class Storage<vtkm::internal::RecombineVec<ReadWritePortal>,
vtkm::cont::internal::StorageTagRecombineVec>
{
using ComponentType = typename ReadWritePortal::ValueType;
using SourceStorage = vtkm::cont::internal::Storage<ComponentType, vtkm::cont::StorageTagStride>;
using ArrayType = vtkm::cont::ArrayHandle<ComponentType, vtkm::cont::StorageTagStride>;
VTKM_STATIC_ASSERT(
(std::is_same<ReadWritePortal, detail::RecombinedPortalType<ComponentType>>::value));
template <typename Buff>
VTKM_CONT static Buff* BuffersForComponent(Buff* buffers, vtkm::IdComponent componentIndex)
{
return buffers[0]
.template GetMetaData<detail::RecombineVecMetaData>()
.ArrayBuffers[componentIndex]
.data();
}
public:
VTKM_STORAGE_NO_RESIZE;
using ReadPortalType = vtkm::internal::ArrayPortalRecombineVec<ReadWritePortal>;
using WritePortalType = vtkm::internal::ArrayPortalRecombineVec<ReadWritePortal>;
VTKM_CONT static vtkm::IdComponent NumberOfComponents(const vtkm::cont::internal::Buffer* buffers)
{
return static_cast<vtkm::IdComponent>(
buffers[0].GetMetaData<detail::RecombineVecMetaData>().ArrayBuffers.size());
}
VTKM_CONT static vtkm::IdComponent GetNumberOfBuffers() { return 1; }
VTKM_CONT static vtkm::Id GetNumberOfValues(const vtkm::cont::internal::Buffer* buffers)
{
return SourceStorage::GetNumberOfValues(BuffersForComponent(buffers, 0));
}
VTKM_CONT static ReadPortalType CreateReadPortal(const vtkm::cont::internal::Buffer* buffers,
vtkm::cont::DeviceAdapterId device,
vtkm::cont::Token& token)
{
ReadPortalType portal;
vtkm::IdComponent numComponents = NumberOfComponents(buffers);
// The array portal needs a runtime-allocated array of portals for each component.
// We use the vtkm::cont::internal::Buffer object to allow us to allocate memory on the
// device and copy data there.
vtkm::cont::internal::Buffer portalBuffer;
portalBuffer.SetNumberOfBytes(static_cast<vtkm::BufferSizeType>(sizeof(ReadWritePortal)) *
numComponents,
vtkm::CopyFlag::Off,
token);
// Save a reference of the portal in our metadata.
// Note that the buffer we create is going to hang around until the ArrayHandle gets
// destroyed. The buffers are small and should not be a problem unless you create a
// lot of portals.
buffers[0].GetMetaData<detail::RecombineVecMetaData>().PortalBuffers.push_back(portalBuffer);
// Get the control-side memory and fill it with the execution-side portals
ReadWritePortal* portals =
reinterpret_cast<ReadWritePortal*>(portalBuffer.WritePointerHost(token));
for (vtkm::IdComponent cIndex = 0; cIndex < numComponents; ++cIndex)
{
portals[cIndex] = ReadWritePortal(
SourceStorage::CreateReadPortal(BuffersForComponent(buffers, cIndex), device, token));
}
// Now get the execution-side memory (portals will be copied as necessary) and create
// the portal for the appropriate device
return ReadPortalType(
reinterpret_cast<const ReadWritePortal*>(portalBuffer.ReadPointerDevice(device, token)),
numComponents);
}
VTKM_CONT static WritePortalType CreateWritePortal(vtkm::cont::internal::Buffer* buffers,
vtkm::cont::DeviceAdapterId device,
vtkm::cont::Token& token)
{
WritePortalType portal;
vtkm::IdComponent numComponents = NumberOfComponents(buffers);
// The array portal needs a runtime-allocated array of portals for each component.
// We use the vtkm::cont::internal::Buffer object to allow us to allocate memory on the
// device and copy data there.
vtkm::cont::internal::Buffer portalBuffer;
portalBuffer.SetNumberOfBytes(static_cast<vtkm::BufferSizeType>(sizeof(ReadWritePortal)) *
numComponents,
vtkm::CopyFlag::Off,
token);
// Save a reference of the portal in our metadata.
// Note that the buffer we create is going to hang around until the ArrayHandle gets
// destroyed. The buffers are small and should not be a problem unless you create a
// lot of portals.
buffers[0].GetMetaData<detail::RecombineVecMetaData>().PortalBuffers.push_back(portalBuffer);
// Get the control-side memory and fill it with the execution-side portals
ReadWritePortal* portals =
reinterpret_cast<ReadWritePortal*>(portalBuffer.WritePointerHost(token));
for (vtkm::IdComponent cIndex = 0; cIndex < numComponents; ++cIndex)
{
portals[cIndex] = ReadWritePortal(
SourceStorage::CreateWritePortal(BuffersForComponent(buffers, cIndex), device, token));
}
// Now get the execution-side memory (portals will be copied as necessary) and create
// the portal for the appropriate device
return WritePortalType(
reinterpret_cast<const ReadWritePortal*>(portalBuffer.ReadPointerDevice(device, token)),
numComponents);
}
VTKM_CONT static ArrayType ArrayForComponent(const vtkm::cont::internal::Buffer* buffers,
vtkm::IdComponent componentIndex)
{
return ArrayType(BuffersForComponent(buffers, componentIndex));
}
VTKM_CONT static void AppendComponent(vtkm::cont::internal::Buffer* buffers,
const ArrayType& array)
{
std::vector<vtkm::cont::internal::Buffer> arrayBuffers(
array.GetBuffers(), array.GetBuffers() + SourceStorage::GetNumberOfBuffers());
buffers[0].GetMetaData<detail::RecombineVecMetaData>().ArrayBuffers.push_back(
std::move(arrayBuffers));
}
};
} // namespace internal
template <typename T>
VTKM_ARRAY_HANDLE_NEW_STYLE(T, vtkm::cont::internal::StorageTagRecombineVec);
/// \brief A grouping of `ArrayHandleStride`s into an `ArrayHandle` of `Vec`s.
///
/// The main intention of `ArrayHandleStride` is to pull out a component of an
/// `ArrayHandle` without knowing there `ArrayHandle`'s storage or `Vec` shape.
/// However, usually you want to do an operation on all the components together.
/// `ArrayHandleRecombineVec` implements the functionality to easily take a
/// group of extracted components and treat them as a single `ArrayHandle` of
/// `Vec` values.
///
/// Note that caution should be used with `ArrayHandleRecombineVec` because the
/// size of the `Vec` values is not known at compile time. Thus, the value
/// type of this array is forced to a `VecVariable`, which can cause surprises
/// if treated as a `Vec`. In particular, the static `NUM_COMPONENTS` expression
/// does not exist.
///
template <typename ComponentType>
class ArrayHandleRecombineVec
: public vtkm::cont::ArrayHandle<internal::detail::RecombinedValueType<ComponentType>,
vtkm::cont::internal::StorageTagRecombineVec>
{
public:
VTKM_ARRAY_HANDLE_SUBCLASS(
ArrayHandleRecombineVec,
(ArrayHandleRecombineVec<ComponentType>),
(vtkm::cont::ArrayHandle<internal::detail::RecombinedValueType<ComponentType>,
vtkm::cont::internal::StorageTagRecombineVec>));
private:
using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
public:
vtkm::IdComponent GetNumberOfComponents() const
{
return StorageType::NumberOfComponents(this->GetBuffers());
}
vtkm::cont::ArrayHandleStride<ComponentType> GetComponentArray(
vtkm::IdComponent componentIndex) const
{
return StorageType::ArrayForComponent(this->GetBuffers(), componentIndex);
}
void AppendComponentArray(
const vtkm::cont::ArrayHandle<ComponentType, vtkm::cont::StorageTagStride>& array)
{
StorageType::AppendComponent(this->GetBuffers(), array);
}
};
namespace internal
{
template <>
struct ArrayExtractComponentImpl<vtkm::cont::internal::StorageTagRecombineVec>
{
template <typename RecombineVec>
vtkm::cont::ArrayHandleStride<
typename vtkm::VecFlat<typename RecombineVec::ComponentType>::ComponentType>
operator()(
const vtkm::cont::ArrayHandle<RecombineVec, vtkm::cont::internal::StorageTagRecombineVec>& src,
vtkm::IdComponent componentIndex,
vtkm::CopyFlag allowCopy) const
{
using ComponentType = typename RecombineVec::ComponentType;
vtkm::cont::ArrayHandleRecombineVec<ComponentType> array(src);
constexpr vtkm::IdComponent subComponents = vtkm::VecFlat<ComponentType>::NUM_COMPONENTS;
return vtkm::cont::ArrayExtractComponent(
array.GetComponentArray(componentIndex / subComponents),
componentIndex % subComponents,
allowCopy);
}
};
} // namespace internal
}
} // namespace vtkm::cont
#endif //vtk_m_cont_ArrayHandleRecombineVec_h

@ -208,7 +208,7 @@ public:
template <typename T>
VTKM_ARRAY_HANDLE_NEW_STYLE(T, vtkm::cont::StorageTagStride);
/// \brief An `ArrayHandle` that access a basic array with strides and offsets.
/// \brief An `ArrayHandle` that accesses a basic array with strides and offsets.
///
/// `ArrayHandleStride` is a simple `ArrayHandle` that accesses data with a prescribed
/// stride and offset. You specify the stride and offset at construction. So when a portal

@ -33,6 +33,7 @@ set(headers
ArrayHandleIndex.h
ArrayHandleMultiplexer.h
ArrayHandlePermutation.h
ArrayHandleRecombineVec.h
ArrayHandleReverse.h
ArrayHandleRandomStandardNormal.h
ArrayHandleRandomUniformBits.h

@ -110,17 +110,6 @@ struct MetaDataManager
MetaDataManager() = default;
MetaDataManager(void* data,
const std::string& type,
vtkm::cont::internal::detail::DeleterType* deleter,
vtkm::cont::internal::detail::CopierType* copier)
: Data(data)
, Type(type)
, Deleter(deleter)
, Copier(copier)
{
}
~MetaDataManager()
{
if (this->Data != nullptr)
@ -131,20 +120,23 @@ struct MetaDataManager
}
}
MetaDataManager(const MetaDataManager& src)
// We don't know how much information is the metadata, and copying it could be expensive.
// Thus, we want to be intentional about copying the metadata only for deep copies.
MetaDataManager(const MetaDataManager& src) = delete;
MetaDataManager& operator=(const MetaDataManager& src) = delete;
void Initialize(void* data,
const std::string& type,
vtkm::cont::internal::detail::DeleterType* deleter,
vtkm::cont::internal::detail::CopierType* copier)
{
if (src.Data != nullptr)
{
VTKM_ASSERT(src.Copier);
VTKM_ASSERT(src.Deleter);
this->Data = src.Copier(src.Data);
this->Type = src.Type;
this->Deleter = src.Deleter;
this->Copier = src.Copier;
}
Data = data;
Type = type;
Deleter = deleter;
Copier = copier;
}
MetaDataManager& operator=(const MetaDataManager& src)
void DeepCopyFrom(const MetaDataManager& src)
{
if (this->Data != nullptr)
{
@ -162,7 +154,6 @@ struct MetaDataManager
this->Deleter = src.Deleter;
this->Copier = src.Copier;
}
return *this;
}
};
@ -599,7 +590,7 @@ struct VTKM_NEVER_EXPORT BufferHelper
srcInternals->GetHostBuffer(srcLock).GetPointer(),
static_cast<std::size_t>(size));
destInternals->MetaData = srcInternals->MetaData;
destInternals->MetaData.DeepCopyFrom(srcInternals->MetaData);
}
static void CopyOnDevice(
@ -641,7 +632,7 @@ struct VTKM_NEVER_EXPORT BufferHelper
destInternals->SetNumberOfBytes(destLock, srcInternals->GetNumberOfBytes(srcLock));
destInternals->MetaData = srcInternals->MetaData;
destInternals->MetaData.DeepCopyFrom(srcInternals->MetaData);
}
};
@ -710,7 +701,7 @@ void Buffer::SetMetaData(void* data,
detail::DeleterType* deleter,
detail::CopierType* copier) const
{
this->Internals->MetaData = MetaDataManager(data, type, deleter, copier);
this->Internals->MetaData.Initialize(data, type, deleter, copier);
}
void* Buffer::GetMetaData(const std::string& type) const
@ -881,7 +872,7 @@ void Buffer::DeepCopyFrom(const vtkm::cont::internal::Buffer& src) const
src.Internals->GetNumberOfBytes(srcLock),
vtkm::CopyFlag::Off,
token);
dest.Internals->MetaData = src.Internals->MetaData;
dest.Internals->MetaData.DeepCopyFrom(src.Internals->MetaData);
}
}
}

@ -36,6 +36,7 @@ set(unit_tests
UnitTestArrayGetValues.cxx
UnitTestArrayHandleCartesianProduct.cxx
UnitTestArrayHandleCompositeVector.cxx
UnitTestArrayHandleConcatenate.cxx
UnitTestArrayHandleCounting.cxx
UnitTestArrayHandleDecorator.cxx
UnitTestArrayHandleDiscard.cxx
@ -52,7 +53,6 @@ set(unit_tests
UnitTestArrayHandleThreadSafety.cxx
UnitTestArrayHandleTransform.cxx
UnitTestArrayHandleUniformPointCoordinates.cxx
UnitTestArrayHandleConcatenate.cxx
UnitTestArrayHandleVirtual.cxx
UnitTestArrayPortalToIterators.cxx
UnitTestCellLocatorGeneral.cxx

@ -24,6 +24,7 @@
#include <vtkm/cont/ArrayHandleIndex.h>
#include <vtkm/cont/ArrayHandleMultiplexer.h>
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/ArrayHandleRecombineVec.h>
#include <vtkm/cont/ArrayHandleSOA.h>
#include <vtkm/cont/ArrayHandleTransform.h>
#include <vtkm/cont/ArrayHandleView.h>
@ -222,12 +223,12 @@ public:
struct PassThrough : public vtkm::worklet::WorkletMapField
{
using ControlSignature = void(FieldIn, FieldOut);
using ExecutionSignature = _2(_1);
using ExecutionSignature = void(_1, _2);
template <class ValueType>
VTKM_EXEC ValueType operator()(const ValueType& inValue) const
template <typename InValue, typename OutValue>
VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const
{
return inValue;
outValue = inValue;
}
};
@ -1154,6 +1155,78 @@ private:
}
};
struct TestRecombineVecAsInput
{
template <typename T>
VTKM_CONT void operator()(T) const
{
vtkm::cont::ArrayHandle<T> baseArray;
baseArray.Allocate(ARRAY_SIZE);
SetPortal(baseArray.WritePortal());
using VTraits = vtkm::VecTraits<T>;
vtkm::cont::ArrayHandleRecombineVec<typename VTraits::ComponentType> recombinedArray;
for (vtkm::IdComponent cIndex = 0; cIndex < VTraits::NUM_COMPONENTS; ++cIndex)
{
recombinedArray.AppendComponentArray(vtkm::cont::ArrayExtractComponent(baseArray, cIndex));
}
VTKM_TEST_ASSERT(recombinedArray.GetNumberOfComponents() == VTraits::NUM_COMPONENTS);
VTKM_TEST_ASSERT(recombinedArray.GetNumberOfValues() == ARRAY_SIZE);
vtkm::cont::ArrayHandle<T> outputArray;
vtkm::cont::Invoker invoke;
invoke(PassThrough{}, recombinedArray, outputArray);
VTKM_TEST_ASSERT(test_equal_ArrayHandles(baseArray, outputArray));
}
};
// RecombineVecAsOutput is a bit strange because it contains Vecs of
// lengths not known until runtime. The unintended consequence is that
// a simple FieldOut does not work because the variable Vec-like always
// has to point back to the array portals. Instead, you need to use something
// like a FieldInOut.
struct PassThroughWithInOut : public vtkm::worklet::WorkletMapField
{
using ControlSignature = void(FieldIn, FieldInOut);
using ExecutionSignature = void(_1, _2);
template <typename InValue, typename OutValue>
VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const
{
outValue = inValue;
}
};
struct TestRecombineVecAsOutput
{
template <typename T>
VTKM_CONT void operator()(T) const
{
vtkm::cont::ArrayHandle<T> baseArray;
baseArray.Allocate(ARRAY_SIZE);
SetPortal(baseArray.WritePortal());
vtkm::cont::ArrayHandle<T> outputArray;
outputArray.Allocate(ARRAY_SIZE); // Cannot resize after recombine
using VTraits = vtkm::VecTraits<T>;
vtkm::cont::ArrayHandleRecombineVec<typename VTraits::ComponentType> recombinedArray;
for (vtkm::IdComponent cIndex = 0; cIndex < VTraits::NUM_COMPONENTS; ++cIndex)
{
recombinedArray.AppendComponentArray(
vtkm::cont::ArrayExtractComponent(outputArray, cIndex));
}
VTKM_TEST_ASSERT(recombinedArray.GetNumberOfComponents() == VTraits::NUM_COMPONENTS);
VTKM_TEST_ASSERT(recombinedArray.GetNumberOfValues() == ARRAY_SIZE);
vtkm::cont::Invoker invoke;
invoke(PassThroughWithInOut{}, baseArray, recombinedArray);
VTKM_TEST_ASSERT(test_equal_ArrayHandles(baseArray, outputArray));
}
};
struct TestZipAsInput
{
template <typename KeyType, typename ValueType>
@ -1555,6 +1628,17 @@ private:
TestingFancyArrayHandles<DeviceAdapterTag>::TestGroupVecVariableAsOutput(),
ScalarTypesToTest());
std::cout << "-------------------------------------------" << std::endl;
std::cout << "Testing ArrayHandleRecombineVec as Input" << std::endl;
vtkm::testing::Testing::TryTypes(
TestingFancyArrayHandles<DeviceAdapterTag>::TestRecombineVecAsInput(), HandleTypesToTest{});
std::cout << "-------------------------------------------" << std::endl;
std::cout << "Testing ArrayHandleRecombineVec as Output" << std::endl;
vtkm::testing::Testing::TryTypes(
TestingFancyArrayHandles<DeviceAdapterTag>::TestRecombineVecAsOutput(),
HandleTypesToTest{});
std::cout << "-------------------------------------------" << std::endl;
std::cout << "Testing ArrayHandleZip as Input" << std::endl;
vtkm::testing::Testing::TryTypes(TestingFancyArrayHandles<DeviceAdapterTag>::TestZipAsInput(),

@ -22,6 +22,7 @@
#include <vtkm/TypeTraits.h>
#include <vtkm/Types.h>
#include <vtkm/VecTraits.h>
#include <vtkm/VecVariable.h>
#include <vtkm/cont/Logging.h>
@ -538,19 +539,13 @@ namespace detail
template <typename T1, typename T2>
struct TestEqualImpl
{
template <typename Dimensionality1, typename Dimensionality2>
VTKM_EXEC_CONT bool DoIt(T1 vector1,
T2 vector2,
vtkm::Float64 tolerance,
vtkm::TypeTraitsVectorTag) const
Dimensionality1,
Dimensionality2) const
{
// If you get a compiler error here, it means you are comparing a vector to
// a scalar, in which case the types are non-comparable.
VTKM_STATIC_ASSERT_MSG((std::is_same<typename vtkm::TypeTraits<T2>::DimensionalityTag,
vtkm::TypeTraitsVectorTag>::type::value) ||
(std::is_same<typename vtkm::TypeTraits<T2>::DimensionalityTag,
vtkm::TypeTraitsMatrixTag>::type::value),
"Trying to compare a vector with a scalar.");
using Traits1 = vtkm::VecTraits<T1>;
using Traits2 = vtkm::VecTraits<T2>;
@ -575,18 +570,33 @@ struct TestEqualImpl
return true;
}
VTKM_EXEC_CONT bool DoIt(T1 matrix1,
T2 matrix2,
VTKM_EXEC_CONT bool DoIt(T1 scalar1,
T2 scalar2,
vtkm::Float64 tolerance,
vtkm::TypeTraitsMatrixTag) const
vtkm::TypeTraitsScalarTag,
vtkm::TypeTraitsScalarTag) const
{
// For the purposes of comparison, treat matrices the same as vectors.
return this->DoIt(matrix1, matrix2, tolerance, vtkm::TypeTraitsVectorTag());
// Do all comparisons using 64-bit floats.
return test_equal(
static_cast<vtkm::Float64>(scalar1), static_cast<vtkm::Float64>(scalar2), tolerance);
}
VTKM_EXEC_CONT bool DoIt(vtkm::Float64 value1,
vtkm::Float64 value2,
vtkm::Float64 tolerance) const
VTKM_EXEC_CONT bool operator()(T1 value1, T2 value2, vtkm::Float64 tolerance) const
{
return this->DoIt(value1,
value2,
tolerance,
typename vtkm::TypeTraits<T1>::DimensionalityTag(),
typename vtkm::TypeTraits<T2>::DimensionalityTag());
}
};
template <>
struct TestEqualImpl<vtkm::Float64, vtkm::Float64>
{
VTKM_EXEC_CONT bool operator()(vtkm::Float64 value1,
vtkm::Float64 value2,
vtkm::Float64 tolerance) const
{
// Handle non-finites. Normally, non-finites are never "equal" to each other (for valid
// mathematical reasons), but for testing purposes if the two values are the same type of
@ -633,47 +643,6 @@ struct TestEqualImpl
return false;
}
}
VTKM_EXEC_CONT bool DoIt(T1 scalar1,
T2 scalar2,
vtkm::Float64 tolerance,
vtkm::TypeTraitsScalarTag) const
{
// If you get a compiler error here, it means you are comparing a scalar to
// a vector, in which case the types are non-comparable.
VTKM_STATIC_ASSERT_MSG((std::is_same<typename vtkm::TypeTraits<T2>::DimensionalityTag,
vtkm::TypeTraitsScalarTag>::type::value),
"Trying to compare a scalar with a vector.");
// Do all comparisons using 64-bit floats.
return DoIt(
static_cast<vtkm::Float64>(scalar1), static_cast<vtkm::Float64>(scalar2), tolerance);
}
VTKM_EXEC_CONT bool operator()(T1 value1, T2 value2, vtkm::Float64 tolerance) const
{
return this->DoIt(
value1, value2, tolerance, typename vtkm::TypeTraits<T1>::DimensionalityTag());
}
};
// Special cases of test equal where a scalar is compared with a Vec of size 1,
// which we will allow.
template <typename T>
struct TestEqualImpl<vtkm::Vec<T, 1>, T>
{
VTKM_EXEC_CONT bool operator()(vtkm::Vec<T, 1> value1, T value2, vtkm::Float64 tolerance) const
{
return test_equal(value1[0], value2, tolerance);
}
};
template <typename T>
struct TestEqualImpl<T, vtkm::Vec<T, 1>>
{
VTKM_EXEC_CONT bool operator()(T value1, vtkm::Vec<T, 1> value2, vtkm::Float64 tolerance) const
{
return test_equal(value1, value2[0], tolerance);
}
};
/// Special implementation of test_equal for strings, which don't fit a model