From 05bf2e4518c0a1c4709f8e899a2d88fe31b0c4c2 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Mon, 11 Jul 2022 11:47:59 -0600 Subject: [PATCH] Remove TestingFancyArrayHandles.h This header file contained tests for a bunch of fancy array handles so that they could be compiled for each device. These tests were bunched together because they were replicated for each device implementation, which was a hassle. However, having a bunch of tests crammed together is problematic for a number of reasons. The new testing no longer has a need to make a separate test for each device. Thus, the tests for the individual devices are removed, and the tests are split up and added to the basic vtkm_cont tests. In some cases, individual tests already existed there as well (probably because the developer did not see the test). --- vtkm/cont/cuda/testing/CMakeLists.txt | 1 - .../testing/UnitTestCudaArrayHandleFancy.cu | 23 - vtkm/cont/kokkos/testing/CMakeLists.txt | 1 - .../UnitTestKokkosArrayHandleFancy.cxx | 20 - vtkm/cont/openmp/testing/CMakeLists.txt | 1 - .../UnitTestOpenMPArrayHandleFancy.cxx | 19 - vtkm/cont/serial/testing/CMakeLists.txt | 1 - .../UnitTestSerialArrayHandleFancy.cxx | 20 - vtkm/cont/tbb/testing/CMakeLists.txt | 1 - .../testing/UnitTestTBBArrayHandleFancy.cxx | 20 - vtkm/cont/testing/CMakeLists.txt | 13 +- vtkm/cont/testing/TestingFancyArrayHandles.h | 1760 ----------------- vtkm/cont/testing/UnitTestArrayHandleCast.cxx | 113 ++ .../UnitTestArrayHandleConcatenate.cxx | 92 +- .../testing/UnitTestArrayHandleConstant.cxx | 82 + .../testing/UnitTestArrayHandleGroupVec.cxx | 150 ++ .../UnitTestArrayHandleGroupVecVariable.cxx | 155 ++ .../testing/UnitTestArrayHandleImplicit.cxx | 31 +- .../UnitTestArrayHandleRecombineVec.cxx | 109 + vtkm/cont/testing/UnitTestArrayHandleSOA.cxx | 235 +++ .../testing/UnitTestArrayHandleTransform.cxx | 82 +- vtkm/cont/testing/UnitTestArrayHandleView.cxx | 145 ++ vtkm/cont/testing/UnitTestArrayHandleZip.cxx | 204 ++ 23 files changed, 1397 insertions(+), 1881 deletions(-) delete mode 100644 vtkm/cont/cuda/testing/UnitTestCudaArrayHandleFancy.cu delete mode 100644 vtkm/cont/kokkos/testing/UnitTestKokkosArrayHandleFancy.cxx delete mode 100644 vtkm/cont/openmp/testing/UnitTestOpenMPArrayHandleFancy.cxx delete mode 100644 vtkm/cont/serial/testing/UnitTestSerialArrayHandleFancy.cxx delete mode 100644 vtkm/cont/tbb/testing/UnitTestTBBArrayHandleFancy.cxx delete mode 100644 vtkm/cont/testing/TestingFancyArrayHandles.h create mode 100644 vtkm/cont/testing/UnitTestArrayHandleCast.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleConstant.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleGroupVec.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleGroupVecVariable.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleRecombineVec.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleSOA.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleView.cxx create mode 100644 vtkm/cont/testing/UnitTestArrayHandleZip.cxx diff --git a/vtkm/cont/cuda/testing/CMakeLists.txt b/vtkm/cont/cuda/testing/CMakeLists.txt index c5d881dc4..fd8f2ebee 100644 --- a/vtkm/cont/cuda/testing/CMakeLists.txt +++ b/vtkm/cont/cuda/testing/CMakeLists.txt @@ -9,7 +9,6 @@ ##============================================================================ set(unit_tests - UnitTestCudaArrayHandleFancy.cu UnitTestCudaBitField.cu UnitTestCudaCellLocatorRectilinearGrid.cu UnitTestCudaCellLocatorTwoLevel.cu diff --git a/vtkm/cont/cuda/testing/UnitTestCudaArrayHandleFancy.cu b/vtkm/cont/cuda/testing/UnitTestCudaArrayHandleFancy.cu deleted file mode 100644 index 37f6a8e73..000000000 --- a/vtkm/cont/cuda/testing/UnitTestCudaArrayHandleFancy.cu +++ /dev/null @@ -1,23 +0,0 @@ -//============================================================================ -// 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. -//============================================================================ - -#include - -#include -#include - -int UnitTestCudaArrayHandleFancy(int argc, char* argv[]) -{ - auto& tracker = vtkm::cont::GetRuntimeDeviceTracker(); - tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{}); - int result = vtkm::cont::testing::TestingFancyArrayHandles::Run( - argc, argv); - return vtkm::cont::cuda::internal::Testing::CheckCudaBeforeExit(result); -} diff --git a/vtkm/cont/kokkos/testing/CMakeLists.txt b/vtkm/cont/kokkos/testing/CMakeLists.txt index 8a2783e1f..847d8454e 100644 --- a/vtkm/cont/kokkos/testing/CMakeLists.txt +++ b/vtkm/cont/kokkos/testing/CMakeLists.txt @@ -9,7 +9,6 @@ ##============================================================================ set(unit_tests - UnitTestKokkosArrayHandleFancy.cxx UnitTestKokkosBitField.cxx UnitTestKokkosCellLocatorRectilinearGrid.cxx UnitTestKokkosCellLocatorTwoLevel.cxx diff --git a/vtkm/cont/kokkos/testing/UnitTestKokkosArrayHandleFancy.cxx b/vtkm/cont/kokkos/testing/UnitTestKokkosArrayHandleFancy.cxx deleted file mode 100644 index 2cd1f0a1e..000000000 --- a/vtkm/cont/kokkos/testing/UnitTestKokkosArrayHandleFancy.cxx +++ /dev/null @@ -1,20 +0,0 @@ -//============================================================================ -// 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. -//============================================================================ - -#include -#include - -int UnitTestKokkosArrayHandleFancy(int argc, char* argv[]) -{ - auto& tracker = vtkm::cont::GetRuntimeDeviceTracker(); - tracker.ForceDevice(vtkm::cont::DeviceAdapterTagKokkos{}); - return vtkm::cont::testing::TestingFancyArrayHandles::Run( - argc, argv); -} diff --git a/vtkm/cont/openmp/testing/CMakeLists.txt b/vtkm/cont/openmp/testing/CMakeLists.txt index 4b31423d1..a10c0f443 100644 --- a/vtkm/cont/openmp/testing/CMakeLists.txt +++ b/vtkm/cont/openmp/testing/CMakeLists.txt @@ -9,7 +9,6 @@ ##============================================================================ set(unit_tests - UnitTestOpenMPArrayHandleFancy.cxx UnitTestOpenMPBitField.cxx UnitTestOpenMPCellLocatorRectilinearGrid.cxx UnitTestOpenMPCellLocatorTwoLevel.cxx diff --git a/vtkm/cont/openmp/testing/UnitTestOpenMPArrayHandleFancy.cxx b/vtkm/cont/openmp/testing/UnitTestOpenMPArrayHandleFancy.cxx deleted file mode 100644 index 18efd1590..000000000 --- a/vtkm/cont/openmp/testing/UnitTestOpenMPArrayHandleFancy.cxx +++ /dev/null @@ -1,19 +0,0 @@ -//============================================================================ -// 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. -//============================================================================ -#include -#include - -int UnitTestOpenMPArrayHandleFancy(int argc, char* argv[]) -{ - auto& tracker = vtkm::cont::GetRuntimeDeviceTracker(); - tracker.ForceDevice(vtkm::cont::DeviceAdapterTagOpenMP{}); - return vtkm::cont::testing::TestingFancyArrayHandles::Run( - argc, argv); -} diff --git a/vtkm/cont/serial/testing/CMakeLists.txt b/vtkm/cont/serial/testing/CMakeLists.txt index 5f154c784..f25f4f414 100644 --- a/vtkm/cont/serial/testing/CMakeLists.txt +++ b/vtkm/cont/serial/testing/CMakeLists.txt @@ -9,7 +9,6 @@ ##============================================================================ set(unit_tests - UnitTestSerialArrayHandleFancy.cxx UnitTestSerialBitField.cxx UnitTestSerialCellLocatorRectilinearGrid.cxx UnitTestSerialCellLocatorTwoLevel.cxx diff --git a/vtkm/cont/serial/testing/UnitTestSerialArrayHandleFancy.cxx b/vtkm/cont/serial/testing/UnitTestSerialArrayHandleFancy.cxx deleted file mode 100644 index b84a7239f..000000000 --- a/vtkm/cont/serial/testing/UnitTestSerialArrayHandleFancy.cxx +++ /dev/null @@ -1,20 +0,0 @@ -//============================================================================ -// 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. -//============================================================================ -#include - -#include - -int UnitTestSerialArrayHandleFancy(int argc, char* argv[]) -{ - auto& tracker = vtkm::cont::GetRuntimeDeviceTracker(); - tracker.ForceDevice(vtkm::cont::DeviceAdapterTagSerial{}); - return vtkm::cont::testing::TestingFancyArrayHandles::Run( - argc, argv); -} diff --git a/vtkm/cont/tbb/testing/CMakeLists.txt b/vtkm/cont/tbb/testing/CMakeLists.txt index dc28b67a1..53a6380dd 100644 --- a/vtkm/cont/tbb/testing/CMakeLists.txt +++ b/vtkm/cont/tbb/testing/CMakeLists.txt @@ -9,7 +9,6 @@ ##============================================================================ set(unit_tests - UnitTestTBBArrayHandleFancy.cxx UnitTestTBBBitField.cxx UnitTestTBBCellLocatorRectilinearGrid.cxx UnitTestTBBCellLocatorTwoLevel.cxx diff --git a/vtkm/cont/tbb/testing/UnitTestTBBArrayHandleFancy.cxx b/vtkm/cont/tbb/testing/UnitTestTBBArrayHandleFancy.cxx deleted file mode 100644 index 72f7aee66..000000000 --- a/vtkm/cont/tbb/testing/UnitTestTBBArrayHandleFancy.cxx +++ /dev/null @@ -1,20 +0,0 @@ -//============================================================================ -// 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. -//============================================================================ - -#include -#include - -int UnitTestTBBArrayHandleFancy(int argc, char* argv[]) -{ - auto& tracker = vtkm::cont::GetRuntimeDeviceTracker(); - tracker.ForceDevice(vtkm::cont::DeviceAdapterTagTBB{}); - return vtkm::cont::testing::TestingFancyArrayHandles::Run(argc, - argv); -} diff --git a/vtkm/cont/testing/CMakeLists.txt b/vtkm/cont/testing/CMakeLists.txt index 1bf1f015d..d343614e2 100644 --- a/vtkm/cont/testing/CMakeLists.txt +++ b/vtkm/cont/testing/CMakeLists.txt @@ -20,7 +20,6 @@ set(headers TestingDeviceAdapter.h TestingDataSetExplicit.h TestingDataSetSingleType.h - TestingFancyArrayHandles.h TestingImplicitFunction.h TestingPointLocatorSparseGrid.h TestingRuntimeDeviceConfiguration.h @@ -33,10 +32,8 @@ set(unit_tests UnitTestArrayGetValues.cxx UnitTestArrayHandleCartesianProduct.cxx UnitTestArrayHandleCompositeVector.cxx - UnitTestArrayHandleConcatenate.cxx UnitTestArrayHandleCounting.cxx UnitTestArrayHandleDiscard.cxx - UnitTestArrayHandleImplicit.cxx UnitTestArrayHandleIndex.cxx UnitTestArrayHandleOffsetsToNumComponents.cxx UnitTestArrayHandleRandomUniformBits.cxx @@ -81,15 +78,25 @@ set(unit_tests_device UnitTestAlgorithm.cxx UnitTestArrayCopy.cxx UnitTestArrayHandle.cxx + UnitTestArrayHandleConcatenate.cxx + UnitTestArrayHandleConstant.cxx + UnitTestArrayHandleCast.cxx UnitTestArrayHandleDecorator.cxx UnitTestArrayHandleExtractComponent.cxx + UnitTestArrayHandleGroupVec.cxx + UnitTestArrayHandleGroupVecVariable.cxx + UnitTestArrayHandleImplicit.cxx UnitTestArrayHandleMultiplexer.cxx UnitTestArrayHandlePermutation.cxx UnitTestArrayHandleRandomStandardNormal.cxx UnitTestArrayHandleRandomUniformReal.cxx + UnitTestArrayHandleRecombineVec.cxx + UnitTestArrayHandleSOA.cxx UnitTestArrayHandleSwizzle.cxx UnitTestArrayHandleTransform.cxx + UnitTestArrayHandleView.cxx UnitTestArrayHandleXGCCoordinates.cxx + UnitTestArrayHandleZip.cxx UnitTestArrayRangeCompute.cxx UnitTestCellLocatorChooser.cxx UnitTestCellLocatorGeneral.cxx diff --git a/vtkm/cont/testing/TestingFancyArrayHandles.h b/vtkm/cont/testing/TestingFancyArrayHandles.h deleted file mode 100644 index e3f23fdef..000000000 --- a/vtkm/cont/testing/TestingFancyArrayHandles.h +++ /dev/null @@ -1,1760 +0,0 @@ -//============================================================================ -// 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_testing_TestingFancyArrayHandles_h -#define vtk_m_cont_testing_TestingFancyArrayHandles_h - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// MSVC is giving deprecation warnings in stupid places, so just disable the deprecated tests -// for that compiler -#if !defined(VTKM_NO_DEPRECATED_VIRTUAL) && defined(VTKM_MSVC) -#define VTKM_NO_DEPRECATED_VIRTUAL -#endif - -#ifndef VTKM_NO_DEPRECATED_VIRTUAL -#include -#endif //VTKM_NO_DEPRECATED_VIRTUAL - -#include -#include - -#include - -#include - -namespace fancy_array_detail -{ - -template -struct IndexSquared -{ - VTKM_EXEC_CONT - ValueType operator()(vtkm::Id index) const - { - using ComponentType = typename vtkm::VecTraits::ComponentType; - return ValueType(static_cast(index * index)); - } -}; - -template -struct ValueSquared -{ - template - VTKM_EXEC_CONT ValueType operator()(U u) const - { - return vtkm::Dot(u, u); - } -}; - -struct ValueScale -{ - ValueScale() - : Factor(1.0) - { - } - - ValueScale(vtkm::Float64 factor) - : Factor(factor) - { - } - - template - VTKM_EXEC_CONT ValueType operator()(const ValueType& v) const - { - using Traits = vtkm::VecTraits; - using TTraits = vtkm::TypeTraits; - using ComponentType = typename Traits::ComponentType; - - ValueType result = TTraits::ZeroInitialization(); - for (vtkm::IdComponent i = 0; i < Traits::GetNumberOfComponents(v); ++i) - { - vtkm::Float64 vi = static_cast(Traits::GetComponent(v, i)); - vtkm::Float64 ri = vi * this->Factor; - Traits::SetComponent(result, i, static_cast(ri)); - } - return result; - } - -private: - vtkm::Float64 Factor; -}; - -struct InverseValueScale -{ - InverseValueScale() - : InverseFactor(1.0) - { - } - - InverseValueScale(vtkm::Float64 factor) - : InverseFactor(1.0 / factor) - { - } - - template - VTKM_EXEC_CONT ValueType operator()(const ValueType& v) const - { - using Traits = vtkm::VecTraits; - using TTraits = vtkm::TypeTraits; - using ComponentType = typename Traits::ComponentType; - - ValueType result = TTraits::ZeroInitialization(); - for (vtkm::IdComponent i = 0; i < Traits::GetNumberOfComponents(v); ++i) - { - vtkm::Float64 vi = static_cast(Traits::GetComponent(v, i)); - vtkm::Float64 ri = vi * this->InverseFactor; - Traits::SetComponent(result, i, static_cast(ri)); - } - return result; - } - -private: - vtkm::Float64 InverseFactor; -}; - -#ifndef VTKM_NO_DEPRECATED_VIRTUAL -VTKM_DEPRECATED_SUPPRESS_BEGIN - -template -struct VirtualTransformFunctorBase : public vtkm::VirtualObjectBase -{ - VirtualTransformFunctorBase() = default; - - VTKM_EXEC_CONT - virtual ValueType operator()(const ValueType& v) const = 0; -}; - -template -struct VirtualTransformFunctor : VirtualTransformFunctorBase -{ - FunctorType Functor; - - VTKM_CONT - VirtualTransformFunctor(const FunctorType& functor) - : Functor(functor) - { - } - - VTKM_EXEC_CONT - ValueType operator()(const ValueType& v) const override { return this->Functor(v); } -}; - -template -struct TransformExecObject : public vtkm::cont::ExecutionAndControlObjectBase -{ - vtkm::cont::VirtualObjectHandle> VirtualFunctor; - - VTKM_CONT TransformExecObject() = default; - - template - VTKM_CONT TransformExecObject(const FunctorType& functor) - { - // Need to make sure the serial device is supported, since that is what is used on the - // control side. Therefore we reset to all supported devices. - vtkm::cont::ScopedRuntimeDeviceTracker scopedTracker( - vtkm::cont::DeviceAdapterTagSerial{}, vtkm::cont::RuntimeDeviceTrackerMode::Enable); - this->VirtualFunctor.Reset(new VirtualTransformFunctor(functor)); - } - - struct FunctorWrapper - { - const VirtualTransformFunctorBase* FunctorPointer; - - FunctorWrapper() = default; - - VTKM_CONT - FunctorWrapper(const VirtualTransformFunctorBase* functorPointer) - : FunctorPointer(functorPointer) - { - } - - template - VTKM_EXEC ValueType operator()(const InValueType& value) const - { - return (*this->FunctorPointer)(value); - } - }; - - template - VTKM_CONT FunctorWrapper PrepareForExecution(DeviceAdapterTag device, - vtkm::cont::Token& token) const - { - return FunctorWrapper(this->VirtualFunctor.PrepareForExecution(device, token)); - } - - VTKM_CONT FunctorWrapper PrepareForControl() const - { - return FunctorWrapper(this->VirtualFunctor.Get()); - } -}; - -VTKM_DEPRECATED_SUPPRESS_END -#endif //VTKM_NO_DEPRECATED_VIRTUAL - -} - -namespace vtkm -{ -namespace cont -{ -namespace testing -{ - -/// This class has a single static member, Run, that tests that all Fancy Array -/// Handles work with the given DeviceAdapter -/// -template -struct TestingFancyArrayHandles -{ - -private: - static const int ARRAY_SIZE = 10; - -public: - struct PassThrough : public vtkm::worklet::WorkletMapField - { - using ControlSignature = void(FieldIn, FieldOut); - using ExecutionSignature = void(_1, _2); - - template - VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const - { - outValue = inValue; - } - }; - - struct InplaceFunctorPair : public vtkm::worklet::WorkletMapField - { - using ControlSignature = void(FieldInOut); - using ExecutionSignature = void(_1); - - template - VTKM_EXEC void operator()(vtkm::Pair& value) const - { - value.second = value.first; - } - }; - -#ifndef VTKM_CUDA -private: -#endif - - struct TestArrayPortalSOA - { - template - VTKM_CONT void operator()(ComponentType) const - { - constexpr vtkm::IdComponent NUM_COMPONENTS = 4; - using ValueType = vtkm::Vec; - using ComponentArrayType = vtkm::cont::ArrayHandle; - using SOAPortalType = - vtkm::internal::ArrayPortalSOA; - - std::cout << "Test SOA portal reflects data in component portals." << std::endl; - SOAPortalType soaPortalIn(ARRAY_SIZE); - - std::array, NUM_COMPONENTS> implArrays; - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) - { - vtkm::cont::ArrayHandle array; - array.Allocate(ARRAY_SIZE); - auto portal = array.WritePortal(); - for (vtkm::IdComponent valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) - { - portal.Set(valueIndex, TestValue(valueIndex, ValueType{})[componentIndex]); - } - - soaPortalIn.SetPortal(componentIndex, portal); - - implArrays[static_cast(componentIndex)] = array; - } - - VTKM_TEST_ASSERT(soaPortalIn.GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(soaPortalIn); - - std::cout << "Test data set in SOA portal gets set in component portals." << std::endl; - { - SOAPortalType soaPortalOut(ARRAY_SIZE); - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; - ++componentIndex) - { - vtkm::cont::ArrayHandle array; - array.Allocate(ARRAY_SIZE); - auto portal = array.WritePortal(); - soaPortalOut.SetPortal(componentIndex, portal); - - implArrays[static_cast(componentIndex)] = array; - } - - SetPortal(soaPortalOut); - } - - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) - { - auto portal = implArrays[static_cast(componentIndex)].ReadPortal(); - for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) - { - ComponentType x = TestValue(valueIndex, ValueType{})[componentIndex]; - VTKM_TEST_ASSERT(test_equal(x, portal.Get(valueIndex))); - } - } - } - }; - - struct TestSOAAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using VTraits = vtkm::VecTraits; - using ComponentType = typename VTraits::ComponentType; - constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS; - - { - vtkm::cont::ArrayHandleSOA soaArray; - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; - ++componentIndex) - { - vtkm::cont::ArrayHandle componentArray; - componentArray.Allocate(ARRAY_SIZE); - auto componentPortal = componentArray.WritePortal(); - for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) - { - componentPortal.Set( - valueIndex, - VTraits::GetComponent(TestValue(valueIndex, ValueType{}), componentIndex)); - } - soaArray.SetArray(componentIndex, componentArray); - } - - VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); - VTKM_TEST_ASSERT(soaArray.ReadPortal().GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(soaArray.ReadPortal()); - - vtkm::cont::ArrayHandle basicArray; - vtkm::cont::ArrayCopy(soaArray, basicArray); - VTKM_TEST_ASSERT(basicArray.GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(basicArray.ReadPortal()); - } - - { - // Check constructors - using Vec3 = vtkm::Vec; - std::vector vector0; - std::vector vector1; - std::vector vector2; - for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) - { - Vec3 value = TestValue(valueIndex, Vec3{}); - vector0.push_back(value[0]); - vector1.push_back(value[1]); - vector2.push_back(value[2]); - } - - { - vtkm::cont::ArrayHandleSOA soaArray = - vtkm::cont::make_ArrayHandleSOA({ vector0, vector1, vector2 }); - VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(soaArray.ReadPortal()); - } - - { - vtkm::cont::ArrayHandleSOA soaArray = - vtkm::cont::make_ArrayHandleSOA(vtkm::CopyFlag::Off, vector0, vector1, vector2); - VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(soaArray.ReadPortal()); - - // Make sure calling ReleaseResources does not result in error. - soaArray.ReleaseResources(); - } - - { - vtkm::cont::ArrayHandleSOA soaArray = vtkm::cont::make_ArrayHandleSOA( - { vector0.data(), vector1.data(), vector2.data() }, ARRAY_SIZE, vtkm::CopyFlag::Off); - VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(soaArray.ReadPortal()); - } - - { - vtkm::cont::ArrayHandleSOA soaArray = vtkm::cont::make_ArrayHandleSOA( - ARRAY_SIZE, vtkm::CopyFlag::Off, vector0.data(), vector1.data(), vector2.data()); - VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); - CheckPortal(soaArray.ReadPortal()); - } - } - } - }; - - struct TestSOAAsOutput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using VTraits = vtkm::VecTraits; - using ComponentType = typename VTraits::ComponentType; - constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS; - - vtkm::cont::ArrayHandle basicArray; - basicArray.Allocate(ARRAY_SIZE); - SetPortal(basicArray.WritePortal()); - - vtkm::cont::ArrayHandleSOA soaArray; - vtkm::cont::Invoker{}(PassThrough{}, basicArray, soaArray); - - VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) - { - vtkm::cont::ArrayHandle componentArray = soaArray.GetArray(componentIndex); - auto componentPortal = componentArray.ReadPortal(); - for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) - { - ComponentType expected = - VTraits::GetComponent(TestValue(valueIndex, ValueType{}), componentIndex); - ComponentType got = componentPortal.Get(valueIndex); - VTKM_TEST_ASSERT(test_equal(expected, got)); - } - } - } - }; - - struct TestCompositeAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const ValueType value = TestValue(13, ValueType()); - std::vector compositeData(ARRAY_SIZE, value); - vtkm::cont::ArrayHandle compositeInput = - vtkm::cont::make_ArrayHandle(compositeData, vtkm::CopyFlag::Off); - - auto composite = - vtkm::cont::make_ArrayHandleCompositeVector(compositeInput, compositeInput, compositeInput); - - vtkm::cont::ArrayHandle> result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(composite, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto compositePortal = composite.ReadPortal(); - for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) - { - const vtkm::Vec result_v = resultPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, vtkm::Vec(value)), - "CompositeVector Handle Failed"); - - const vtkm::Vec result_c = compositePortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_c, vtkm::Vec(value)), - "CompositeVector Handle Failed"); - } - - composite.ReleaseResources(); - } - }; - - struct TestConstantAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const ValueType value = TestValue(43, ValueType()); - - vtkm::cont::ArrayHandleConstant constant = - vtkm::cont::make_ArrayHandleConstant(value, ARRAY_SIZE); - - VTKM_TEST_ASSERT(constant.GetValue() == value); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(constant, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto constantPortal = constant.ReadPortal(); - for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) - { - const ValueType result_v = resultPortal.Get(i); - const ValueType control_value = constantPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, value), "Counting Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Counting Handle Control Failed"); - } - - constant.ReleaseResources(); - } - }; - - struct TestCountingAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using ComponentType = typename vtkm::VecTraits::ComponentType; - - const vtkm::Id length = ARRAY_SIZE; - - //need to initialize the start value or else vectors will have - //random values to start - ComponentType component_value(0); - const ValueType start = ValueType(component_value); - - vtkm::cont::ArrayHandleCounting counting = - vtkm::cont::make_ArrayHandleCounting(start, ValueType(1), length); - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(counting, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto countingPortal = counting.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = resultPortal.Get(i); - const ValueType correct_value = ValueType(component_value); - const ValueType control_value = countingPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Counting Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Counting Handle Control Failed"); - component_value = ComponentType(component_value + ComponentType(1)); - } - - counting.ReleaseResources(); - } - }; - - struct TestImplicitAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const vtkm::Id length = ARRAY_SIZE; - using FunctorType = ::fancy_array_detail::IndexSquared; - FunctorType functor; - - vtkm::cont::ArrayHandleImplicit implicit = - vtkm::cont::make_ArrayHandleImplicit(functor, length); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(implicit, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto implicitPortal = implicit.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = resultPortal.Get(i); - const ValueType correct_value = functor(i); - const ValueType control_value = implicitPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Implicit Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Implicit Handle Failed"); - } - - implicit.ReleaseResources(); - } - }; - - struct TestConcatenateAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const vtkm::Id length = ARRAY_SIZE; - - using FunctorType = ::fancy_array_detail::IndexSquared; - using ComponentType = typename vtkm::VecTraits::ComponentType; - - using ValueHandleType = vtkm::cont::ArrayHandleImplicit; - using BasicArrayType = vtkm::cont::ArrayHandle; - using ConcatenateType = vtkm::cont::ArrayHandleConcatenate; - - FunctorType functor; - for (vtkm::Id start_pos = 0; start_pos < length; start_pos += length / 4) - { - vtkm::Id implicitLen = length - start_pos; - vtkm::Id basicLen = start_pos; - - // make an implicit array - ValueHandleType implicit = vtkm::cont::make_ArrayHandleImplicit(functor, implicitLen); - // make a basic array - std::vector basicVec; - for (vtkm::Id i = 0; i < basicLen; i++) - { - basicVec.push_back(ValueType(static_cast(i))); - basicVec.push_back(ValueType(ComponentType(i))); - } - BasicArrayType basic = vtkm::cont::make_ArrayHandle(basicVec, vtkm::CopyFlag::Off); - - // concatenate two arrays together - ConcatenateType concatenate = vtkm::cont::make_ArrayHandleConcatenate(implicit, basic); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(concatenate, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto implicitPortal = implicit.ReadPortal(); - auto basicPortal = basic.ReadPortal(); - auto concatPortal = concatenate.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = resultPortal.Get(i); - ValueType correct_value; - if (i < implicitLen) - correct_value = implicitPortal.Get(i); - else - correct_value = basicPortal.Get(i - implicitLen); - const ValueType control_value = concatPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), - "ArrayHandleConcatenate as Input Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), - "ArrayHandleConcatenate as Input Failed"); - } - - concatenate.ReleaseResources(); - } - } - }; - - struct TestPermutationAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const vtkm::Id length = ARRAY_SIZE; - - using FunctorType = ::fancy_array_detail::IndexSquared; - - using KeyHandleType = vtkm::cont::ArrayHandleCounting; - using ValueHandleType = vtkm::cont::ArrayHandleImplicit; - using PermutationHandleType = - vtkm::cont::ArrayHandlePermutation; - - FunctorType functor; - for (vtkm::Id start_pos = 0; start_pos < length; start_pos += length / 4) - { - const vtkm::Id counting_length = length - start_pos; - - KeyHandleType counting = - vtkm::cont::make_ArrayHandleCounting(start_pos, 1, counting_length); - - ValueHandleType implicit = vtkm::cont::make_ArrayHandleImplicit(functor, length); - - PermutationHandleType permutation = - vtkm::cont::make_ArrayHandlePermutation(counting, implicit); - - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(permutation, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto implicitPortal = implicit.ReadPortal(); - auto permutationPortal = permutation.ReadPortal(); - for (vtkm::Id i = 0; i < counting_length; ++i) - { - const vtkm::Id value_index = i; - const vtkm::Id key_index = start_pos + i; - - const ValueType result_v = resultPortal.Get(value_index); - const ValueType correct_value = implicitPortal.Get(key_index); - const ValueType control_value = permutationPortal.Get(value_index); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Implicit Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Implicit Handle Failed"); - } - - permutation.ReleaseResources(); - } - } - }; - - struct TestViewAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const vtkm::Id length = ARRAY_SIZE; - - using FunctorType = ::fancy_array_detail::IndexSquared; - - using ValueHandleType = vtkm::cont::ArrayHandleImplicit; - using ViewHandleType = vtkm::cont::ArrayHandleView; - - FunctorType functor; - for (vtkm::Id start_pos = 0; start_pos < length; start_pos += length / 4) - { - const vtkm::Id counting_length = length - start_pos; - - ValueHandleType implicit = vtkm::cont::make_ArrayHandleImplicit(functor, length); - - ViewHandleType view = - vtkm::cont::make_ArrayHandleView(implicit, start_pos, counting_length); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(view, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto implicitPortal = implicit.ReadPortal(); - auto viewPortal = view.ReadPortal(); - for (vtkm::Id i = 0; i < counting_length; ++i) - { - const vtkm::Id value_index = i; - const vtkm::Id key_index = start_pos + i; - - const ValueType result_v = resultPortal.Get(value_index); - const ValueType correct_value = implicitPortal.Get(key_index); - const ValueType control_value = viewPortal.Get(value_index); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Implicit Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Implicit Handle Failed"); - } - - view.ReleaseResources(); - } - } - }; - - struct TestTransformAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using FunctorType = fancy_array_detail::ValueScale; - - const vtkm::Id length = ARRAY_SIZE; - FunctorType functor(2.0); - - vtkm::cont::ArrayHandle input; - vtkm::cont::ArrayHandleTransform, FunctorType> - transformed = vtkm::cont::make_ArrayHandleTransform(input, functor); - - input.Allocate(length); - SetPortal(input.WritePortal()); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(transformed, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto transformedPortal = transformed.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = resultPortal.Get(i); - const ValueType correct_value = functor(TestValue(i, ValueType())); - const ValueType control_value = transformedPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Transform Handle Control Failed"); - } - - transformed.ReleaseResources(); - } - }; - -#ifndef VTKM_NO_DEPRECATED_VIRTUAL - VTKM_DEPRECATED_SUPPRESS_BEGIN - - struct TestTransformVirtualAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using FunctorType = fancy_array_detail::ValueScale; - using VirtualFunctorType = fancy_array_detail::TransformExecObject; - - const vtkm::Id length = ARRAY_SIZE; - FunctorType functor(2.0); - VirtualFunctorType virtualFunctor(functor); - - vtkm::cont::ArrayHandle input; - auto transformed = vtkm::cont::make_ArrayHandleTransform(input, virtualFunctor); - - input.Allocate(length); - SetPortal(input.WritePortal()); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(transformed, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto transformedPortal = transformed.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = resultPortal.Get(i); - const ValueType correct_value = functor(TestValue(i, ValueType())); - const ValueType control_value = transformedPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Transform Handle Control Failed"); - } - } - }; - - VTKM_DEPRECATED_SUPPRESS_END -#endif //VTKM_NO_DEPRECATED_VIRTUAL - - struct TestCountingTransformAsInput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using ComponentType = typename vtkm::VecTraits::ComponentType; - using OutputValueType = ComponentType; - using FunctorType = fancy_array_detail::ValueSquared; - - vtkm::Id length = ARRAY_SIZE; - FunctorType functor; - - //need to initialize the start value or else vectors will have - //random values to start - ComponentType component_value(0); - const ValueType start = ValueType(component_value); - - vtkm::cont::ArrayHandleCounting counting(start, ValueType(1), length); - - vtkm::cont::ArrayHandleTransform, FunctorType> - countingTransformed = vtkm::cont::make_ArrayHandleTransform(counting, functor); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(countingTransformed, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - auto countingPortal = countingTransformed.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const OutputValueType result_v = resultPortal.Get(i); - const OutputValueType correct_value = functor(ValueType(component_value)); - const OutputValueType control_value = countingPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Counting Handle Failed"); - VTKM_TEST_ASSERT(test_equal(result_v, control_value), - "Transform Counting Handle Control Failed"); - component_value = ComponentType(component_value + ComponentType(1)); - } - - countingTransformed.ReleaseResources(); - } - }; - - struct TestCastAsInput - { - template - VTKM_CONT void operator()(CastToType vtkmNotUsed(type)) const - { - using InputArrayType = vtkm::cont::ArrayHandleIndex; - - InputArrayType input(ARRAY_SIZE); - vtkm::cont::ArrayHandleCast castArray = - vtkm::cont::make_ArrayHandleCast(input, CastToType()); - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(castArray, result); - - // verify results - vtkm::Id length = ARRAY_SIZE; - auto resultPortal = result.ReadPortal(); - auto inputPortal = input.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - VTKM_TEST_ASSERT(resultPortal.Get(i) == static_cast(inputPortal.Get(i)), - "Casting ArrayHandle Failed"); - } - - castArray.ReleaseResources(); - } - }; - - struct TestCastAsOutput - { - template - VTKM_CONT void operator()(CastFromType vtkmNotUsed(type)) const - { - using InputArrayType = vtkm::cont::ArrayHandleIndex; - using ResultArrayType = vtkm::cont::ArrayHandle; - - InputArrayType input(ARRAY_SIZE); - - ResultArrayType result; - vtkm::cont::ArrayHandleCast castArray = - vtkm::cont::make_ArrayHandleCast(result); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, castArray); - - // verify results - vtkm::Id length = ARRAY_SIZE; - auto inputPortal = input.ReadPortal(); - auto resultPortal = result.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - VTKM_TEST_ASSERT(inputPortal.Get(i) == static_cast(resultPortal.Get(i)), - "Casting ArrayHandle Failed"); - } - } - }; - - struct TestMultiplexerAsInput - { - vtkm::cont::Invoker Invoke; - - template - VTKM_CONT void operator()(T vtkmNotUsed(type)) const - { - using InputArrayType = vtkm::cont::ArrayHandleCounting; - - InputArrayType input(T(1), T(2), ARRAY_SIZE); - vtkm::cont::ArrayHandleMultiplexer< - vtkm::cont::ArrayHandle, - InputArrayType, - vtkm::cont::ArrayHandleCast> - multiplexArray(input); - vtkm::cont::ArrayHandle result; - - this->Invoke(PassThrough{}, multiplexArray, result); - - // verify results - VTKM_TEST_ASSERT(test_equal_portals(result.ReadPortal(), input.ReadPortal()), - "CastingArrayHandle failed"); - - multiplexArray.ReleaseResources(); - } - }; - - struct TestMultiplexerAsOutput - { - vtkm::cont::Invoker Invoke; - - template - VTKM_CONT void operator()(CastFromType vtkmNotUsed(type)) const - { - using InputArrayType = vtkm::cont::ArrayHandleIndex; - using ResultArrayType = vtkm::cont::ArrayHandle; - - InputArrayType input(ARRAY_SIZE); - - ResultArrayType result; - vtkm::cont::ArrayHandleMultiplexer, - vtkm::cont::ArrayHandleCast> - multiplexerArray = vtkm::cont::make_ArrayHandleCast(result); - - this->Invoke(PassThrough{}, input, multiplexerArray); - - // verify results - VTKM_TEST_ASSERT(test_equal_portals(input.ReadPortal(), result.ReadPortal()), - "Multiplexing ArrayHandle failed"); - } - }; - - template - struct TestGroupVecAsInput - { - template - VTKM_CONT void operator()(ComponentType) const - { - using ValueType = vtkm::Vec; - - vtkm::cont::ArrayHandle baseArray; - baseArray.Allocate(ARRAY_SIZE * NUM_COMPONENTS); - SetPortal(baseArray.WritePortal()); - - vtkm::cont::ArrayHandleGroupVec, NUM_COMPONENTS> - groupArray(baseArray); - VTKM_TEST_ASSERT(groupArray.GetNumberOfValues() == ARRAY_SIZE, - "Group array reporting wrong array size."); - - vtkm::cont::ArrayHandle resultArray; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(groupArray, resultArray); - - VTKM_TEST_ASSERT(resultArray.GetNumberOfValues() == ARRAY_SIZE, "Got bad result array size."); - - //verify that the control portal works - vtkm::Id totalIndex = 0; - auto resultPortal = resultArray.ReadPortal(); - for (vtkm::Id index = 0; index < ARRAY_SIZE; ++index) - { - const ValueType result = resultPortal.Get(index); - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; - componentIndex++) - { - const ComponentType expectedValue = TestValue(totalIndex, ComponentType()); - VTKM_TEST_ASSERT(test_equal(result[componentIndex], expectedValue), - "Result array got wrong value."); - totalIndex++; - } - } - - groupArray.ReleaseResources(); - } - }; - - template - struct TestGroupVecAsOutput - { - template - VTKM_CONT void operator()(ComponentType) const - { - using ValueType = vtkm::Vec; - - vtkm::cont::ArrayHandle baseArray; - baseArray.Allocate(ARRAY_SIZE); - SetPortal(baseArray.WritePortal()); - - vtkm::cont::ArrayHandle resultArray; - - vtkm::cont::ArrayHandleGroupVec, NUM_COMPONENTS> - groupArray(resultArray); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(baseArray, groupArray); - - VTKM_TEST_ASSERT(groupArray.GetNumberOfValues() == ARRAY_SIZE, - "Group array reporting wrong array size."); - - VTKM_TEST_ASSERT(resultArray.GetNumberOfValues() == ARRAY_SIZE * NUM_COMPONENTS, - "Got bad result array size."); - - //verify that the control portal works - vtkm::Id totalIndex = 0; - auto resultPortal = resultArray.ReadPortal(); - for (vtkm::Id index = 0; index < ARRAY_SIZE; ++index) - { - const ValueType expectedValue = TestValue(index, ValueType()); - for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; - componentIndex++) - { - const ComponentType result = resultPortal.Get(totalIndex); - VTKM_TEST_ASSERT(test_equal(result, expectedValue[componentIndex]), - "Result array got wrong value."); - totalIndex++; - } - } - } - }; - - // GroupVecVariable is a bit strange because it supports values of different - // lengths, so a simple pass through worklet will not work. Use custom - // worklets. - struct GroupVariableInputWorklet : public vtkm::worklet::WorkletMapField - { - using ControlSignature = void(FieldIn, FieldOut); - using ExecutionSignature = void(_1, WorkIndex, _2); - - template - VTKM_EXEC void operator()(const InputType& input, vtkm::Id workIndex, vtkm::Id& dummyOut) const - { - using ComponentType = typename InputType::ComponentType; - vtkm::IdComponent expectedSize = static_cast(workIndex); - if (expectedSize != input.GetNumberOfComponents()) - { - this->RaiseError("Got unexpected number of components."); - } - - vtkm::Id valueIndex = workIndex * (workIndex - 1) / 2; - dummyOut = valueIndex; - for (vtkm::IdComponent componentIndex = 0; componentIndex < expectedSize; componentIndex++) - { - ComponentType expectedValue = TestValue(valueIndex, ComponentType()); - if (vtkm::Abs(expectedValue - input[componentIndex]) > 0.000001) - { - this->RaiseError("Got bad value in GroupVariableInputWorklet."); - } - valueIndex++; - } - } - }; - - struct TestGroupVecVariableAsInput - { - template - VTKM_CONT void operator()(ComponentType) const - { - vtkm::Id sourceArraySize; - - vtkm::cont::ArrayHandle numComponentsArray; - vtkm::cont::ArrayCopy(vtkm::cont::ArrayHandleIndex(ARRAY_SIZE), numComponentsArray); - vtkm::cont::ArrayHandle offsetsArray = - vtkm::cont::ConvertNumComponentsToOffsets(numComponentsArray, sourceArraySize); - - vtkm::cont::ArrayHandle sourceArray; - sourceArray.Allocate(sourceArraySize); - SetPortal(sourceArray.WritePortal()); - - vtkm::cont::ArrayHandle dummyArray; - - auto groupVecArray = vtkm::cont::make_ArrayHandleGroupVecVariable(sourceArray, offsetsArray); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(groupVecArray, dummyArray); - - dummyArray.ReadPortal(); - - groupVecArray.ReleaseResources(); - } - }; - - // GroupVecVariable is a bit strange because it supports values of different - // lengths, so a simple pass through worklet will not work. Use custom - // worklets. - struct GroupVariableOutputWorklet : public vtkm::worklet::WorkletMapField - { - using ControlSignature = void(FieldIn, FieldOut); - using ExecutionSignature = void(_2, WorkIndex); - - template - VTKM_EXEC void operator()(OutputType& output, vtkm::Id workIndex) const - { - using ComponentType = typename OutputType::ComponentType; - vtkm::IdComponent expectedSize = static_cast(workIndex); - if (expectedSize != output.GetNumberOfComponents()) - { - this->RaiseError("Got unexpected number of components."); - } - - vtkm::Id valueIndex = workIndex * (workIndex - 1) / 2; - for (vtkm::IdComponent componentIndex = 0; componentIndex < expectedSize; componentIndex++) - { - output[componentIndex] = TestValue(valueIndex, ComponentType()); - valueIndex++; - } - } - }; - - struct TestGroupVecVariableAsOutput - { - template - VTKM_CONT void operator()(ComponentType) const - { - vtkm::Id sourceArraySize; - - vtkm::cont::ArrayHandle numComponentsArray; - vtkm::cont::ArrayCopy(vtkm::cont::ArrayHandleIndex(ARRAY_SIZE), numComponentsArray); - vtkm::cont::ArrayHandle offsetsArray = vtkm::cont::ConvertNumComponentsToOffsets( - numComponentsArray, sourceArraySize, DeviceAdapterTag()); - - vtkm::cont::ArrayHandle sourceArray; - sourceArray.Allocate(sourceArraySize); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(vtkm::cont::ArrayHandleIndex(ARRAY_SIZE), - vtkm::cont::make_ArrayHandleGroupVecVariable(sourceArray, offsetsArray)); - - CheckPortal(sourceArray.ReadPortal()); - } - }; - - struct TestRecombineVecAsInput - { - template - VTKM_CONT void operator()(T) const - { - vtkm::cont::ArrayHandle baseArray; - baseArray.Allocate(ARRAY_SIZE); - SetPortal(baseArray.WritePortal()); - - using VTraits = vtkm::VecTraits; - vtkm::cont::ArrayHandleRecombineVec 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 outputArray; - vtkm::cont::Invoker invoke; - invoke(PassThrough{}, recombinedArray, outputArray); - - VTKM_TEST_ASSERT(test_equal_ArrayHandles(baseArray, outputArray)); - } - }; - - struct TestRecombineVecAsOutput - { - template - VTKM_CONT void operator()(T) const - { - vtkm::cont::ArrayHandle baseArray; - baseArray.Allocate(ARRAY_SIZE); - SetPortal(baseArray.WritePortal()); - - vtkm::cont::ArrayHandle outputArray; - outputArray.Allocate(ARRAY_SIZE); // Cannot resize after recombine - - using VTraits = vtkm::VecTraits; - vtkm::cont::ArrayHandleRecombineVec 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(PassThrough{}, baseArray, recombinedArray); - - VTKM_TEST_ASSERT(test_equal_ArrayHandles(baseArray, outputArray)); - } - }; - - struct TestZipAsInput - { - template - VTKM_CONT void operator()(vtkm::Pair vtkmNotUsed(pair)) const - { - using PairType = vtkm::Pair; - using KeyComponentType = typename vtkm::VecTraits::ComponentType; - using ValueComponentType = typename vtkm::VecTraits::ComponentType; - - KeyType testKeys[ARRAY_SIZE]; - ValueType testValues[ARRAY_SIZE]; - - for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) - { - testKeys[i] = KeyType(static_cast(ARRAY_SIZE - i)); - testValues[i] = ValueType(static_cast(i)); - } - vtkm::cont::ArrayHandle keys = - vtkm::cont::make_ArrayHandle(testKeys, ARRAY_SIZE, vtkm::CopyFlag::Off); - vtkm::cont::ArrayHandle values = - vtkm::cont::make_ArrayHandle(testValues, ARRAY_SIZE, vtkm::CopyFlag::Off); - - vtkm::cont::ArrayHandleZip, - vtkm::cont::ArrayHandle> - zip = vtkm::cont::make_ArrayHandleZip(keys, values); - - vtkm::cont::ArrayHandle result; - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(zip, result); - - //verify that the control portal works - auto resultPortal = result.ReadPortal(); - for (int i = 0; i < ARRAY_SIZE; ++i) - { - const PairType result_v = resultPortal.Get(i); - const PairType correct_value(KeyType(static_cast(ARRAY_SIZE - i)), - ValueType(static_cast(i))); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "ArrayHandleZip Failed as input"); - } - - zip.ReleaseResources(); - } - }; - - struct TestDiscardAsOutput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using DiscardHandleType = vtkm::cont::ArrayHandleDiscard; - const vtkm::Id length = ARRAY_SIZE; - - vtkm::cont::ArrayHandle input; - input.Allocate(length); - SetPortal(input.WritePortal()); - - DiscardHandleType discard; - discard.Allocate(length); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, discard); - - // No output to verify since none is stored in memory. Just checking that - // this compiles/runs without errors. - - discard.ReleaseResources(); - } - }; - - struct TestPermutationAsOutput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const vtkm::Id length = ARRAY_SIZE; - - using KeyHandleType = vtkm::cont::ArrayHandleCounting; - using ValueHandleType = vtkm::cont::ArrayHandle; - using PermutationHandleType = - vtkm::cont::ArrayHandlePermutation; - - vtkm::cont::ArrayHandle input; - input.Allocate(length); - SetPortal(input.WritePortal()); - - ValueHandleType values; - values.Allocate(length * 2); - - KeyHandleType counting = vtkm::cont::make_ArrayHandleCounting(length, 1, length); - - PermutationHandleType permutation = vtkm::cont::make_ArrayHandlePermutation(counting, values); - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, permutation); - - //verify that the control portal works - CheckPortal(permutation.ReadPortal()); - } - }; - - struct TestViewAsOutput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - const vtkm::Id length = ARRAY_SIZE; - - using ValueHandleType = vtkm::cont::ArrayHandle; - using ViewHandleType = vtkm::cont::ArrayHandleView; - - vtkm::cont::ArrayHandle input; - input.Allocate(length); - SetPortal(input.WritePortal()); - - ValueHandleType values; - values.Allocate(length * 2); - - ViewHandleType view = vtkm::cont::make_ArrayHandleView(values, length, length); - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, view); - - //verify that the control portal works - CheckPortal(view.ReadPortal()); - - //verify that filling works - const ValueType expected = TestValue(20, ValueType{}); - view.Fill(expected); - auto valuesPortal = values.ReadPortal(); - for (vtkm::Id index = length; index < 2 * length; ++index) - { - VTKM_TEST_ASSERT(valuesPortal.Get(index) == expected); - } - } - }; - - struct TestTransformAsOutput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using FunctorType = fancy_array_detail::ValueScale; - using InverseFunctorType = fancy_array_detail::InverseValueScale; - - const vtkm::Id length = ARRAY_SIZE; - FunctorType functor(2.0); - InverseFunctorType inverseFunctor(2.0); - - vtkm::cont::ArrayHandle input; - input.Allocate(length); - SetPortal(input.WritePortal()); - - vtkm::cont::ArrayHandle output; - auto transformed = vtkm::cont::make_ArrayHandleTransform(output, functor, inverseFunctor); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, transformed); - - - //verify that the control portal works - auto outputPortal = output.ReadPortal(); - auto transformedPortal = transformed.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = outputPortal.Get(i); - const ValueType correct_value = inverseFunctor(TestValue(i, ValueType())); - const ValueType control_value = transformedPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Handle Failed"); - VTKM_TEST_ASSERT(test_equal(functor(result_v), control_value), - "Transform Handle Control Failed"); - } - } - }; - -#ifndef VTKM_NO_DEPRECATED_VIRTUAL - VTKM_DEPRECATED_SUPPRESS_BEGIN - - struct TestTransformVirtualAsOutput - { - template - VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const - { - using FunctorType = fancy_array_detail::ValueScale; - using InverseFunctorType = fancy_array_detail::InverseValueScale; - - using VirtualFunctorType = fancy_array_detail::TransformExecObject; - - const vtkm::Id length = ARRAY_SIZE; - FunctorType functor(2.0); - InverseFunctorType inverseFunctor(2.0); - - VirtualFunctorType virtualFunctor(functor); - VirtualFunctorType virtualInverseFunctor(inverseFunctor); - - vtkm::cont::ArrayHandle input; - input.Allocate(length); - SetPortal(input.WritePortal()); - - vtkm::cont::ArrayHandle output; - auto transformed = - vtkm::cont::make_ArrayHandleTransform(output, virtualFunctor, virtualInverseFunctor); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, transformed); - - //verify that the control portal works - auto outputPortal = output.ReadPortal(); - auto transformedPortal = transformed.ReadPortal(); - for (vtkm::Id i = 0; i < length; ++i) - { - const ValueType result_v = outputPortal.Get(i); - const ValueType correct_value = inverseFunctor(TestValue(i, ValueType())); - const ValueType control_value = transformedPortal.Get(i); - VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Handle Failed"); - VTKM_TEST_ASSERT(test_equal(functor(result_v), control_value), - "Transform Handle Control Failed"); - } - } - }; - - VTKM_DEPRECATED_SUPPRESS_END -#endif //VTKM_NO_DEPRECATED_VIRTUAL - - struct TestZipAsOutput - { - template - VTKM_CONT void operator()(vtkm::Pair vtkmNotUsed(pair)) const - { - using PairType = vtkm::Pair; - using KeyComponentType = typename vtkm::VecTraits::ComponentType; - using ValueComponentType = typename vtkm::VecTraits::ComponentType; - - PairType testKeysAndValues[ARRAY_SIZE]; - for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) - { - testKeysAndValues[i] = PairType(KeyType(static_cast(ARRAY_SIZE - i)), - ValueType(static_cast(i))); - } - vtkm::cont::ArrayHandle input = - vtkm::cont::make_ArrayHandle(testKeysAndValues, ARRAY_SIZE, vtkm::CopyFlag::Off); - - vtkm::cont::ArrayHandle result_keys; - vtkm::cont::ArrayHandle result_values; - vtkm::cont::ArrayHandleZip, - vtkm::cont::ArrayHandle> - result_zip = vtkm::cont::make_ArrayHandleZip(result_keys, result_values); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(input, result_zip); - - //now the two arrays we have zipped should have data inside them - auto keysPortal = result_keys.ReadPortal(); - auto valsPortal = result_values.ReadPortal(); - for (int i = 0; i < ARRAY_SIZE; ++i) - { - const KeyType result_key = keysPortal.Get(i); - const ValueType result_value = valsPortal.Get(i); - - VTKM_TEST_ASSERT( - test_equal(result_key, KeyType(static_cast(ARRAY_SIZE - i))), - "ArrayHandleZip Failed as input for key"); - VTKM_TEST_ASSERT(test_equal(result_value, ValueType(static_cast(i))), - "ArrayHandleZip Failed as input for value"); - } - - // Test filling the zipped array. - vtkm::cont::printSummary_ArrayHandle(result_zip, std::cout, true); - PairType fillValue{ TestValue(1, KeyType{}), TestValue(2, ValueType{}) }; - result_zip.Fill(fillValue, 1); - vtkm::cont::printSummary_ArrayHandle(result_zip, std::cout, true); - keysPortal = result_keys.ReadPortal(); - valsPortal = result_values.ReadPortal(); - // First entry should be the same. - VTKM_TEST_ASSERT( - test_equal(keysPortal.Get(0), KeyType(static_cast(ARRAY_SIZE)))); - VTKM_TEST_ASSERT( - test_equal(valsPortal.Get(0), ValueType(static_cast(0)))); - // The rest should be fillValue - for (vtkm::Id index = 1; index < ARRAY_SIZE; ++index) - { - const KeyType result_key = keysPortal.Get(index); - const ValueType result_value = valsPortal.Get(index); - - VTKM_TEST_ASSERT(test_equal(result_key, fillValue.first)); - VTKM_TEST_ASSERT(test_equal(result_value, fillValue.second)); - } - } - }; - - struct TestZipAsInPlace - { - template - VTKM_CONT void operator()(ValueType) const - { - vtkm::cont::ArrayHandle inputValues; - inputValues.Allocate(ARRAY_SIZE); - SetPortal(inputValues.WritePortal()); - - vtkm::cont::ArrayHandle outputValues; - outputValues.Allocate(ARRAY_SIZE); - - vtkm::worklet::DispatcherMapField dispatcher; - dispatcher.Invoke(vtkm::cont::make_ArrayHandleZip(inputValues, outputValues)); - - CheckPortal(outputValues.ReadPortal()); - } - }; - - using ScalarTypesToTest = vtkm::List; - - using VectorTypesToTest = vtkm::List; - - using ZipTypesToTest = vtkm::List, - vtkm::Pair, - vtkm::Pair>; - - using HandleTypesToTest = - vtkm::List; - - using CastTypesToTest = vtkm::List; - - struct TestAll - { - VTKM_CONT void operator()() const - { - std::cout << "Doing FancyArrayHandle tests" << std::endl; - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayPortalSOA" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestArrayPortalSOA(), ScalarTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleSOA as Input" << std::endl; - vtkm::testing::Testing::TryTypes(TestingFancyArrayHandles::TestSOAAsInput(), - VectorTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleSOA as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestSOAAsOutput(), VectorTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleCompositeVector as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestCompositeAsInput(), ScalarTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleConstant as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestConstantAsInput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleCounting as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestCountingAsInput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleImplicit as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestImplicitAsInput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandlePermutation as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestPermutationAsInput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleView as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestViewAsInput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleTransform as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestTransformAsInput(), HandleTypesToTest()); - -#ifndef VTKM_NO_DEPRECATED_VIRTUAL - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleTransform with virtual as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestTransformVirtualAsInput(), - HandleTypesToTest()); -#endif //VTKM_NO_DEPRECATED_VIRTUAL - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleTransform with Counting as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestCountingTransformAsInput(), - HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleCast as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestCastAsInput(), CastTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleCast as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestCastAsOutput(), CastTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleMultiplexer as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestMultiplexerAsInput(), CastTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleMultiplexer as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestMultiplexerAsOutput(), CastTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleGroupVec<3> as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestGroupVecAsInput<3>(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleGroupVec<4> as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestGroupVecAsInput<4>(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleGroupVec<2> as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestGroupVecAsOutput<2>(), ScalarTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleGroupVec<3> as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestGroupVecAsOutput<3>(), ScalarTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleGroupVecVariable as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestGroupVecVariableAsInput(), - ScalarTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleGroupVecVariable as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestGroupVecVariableAsOutput(), - ScalarTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleRecombineVec as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestRecombineVecAsInput(), HandleTypesToTest{}); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleRecombineVec as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestRecombineVecAsOutput(), - HandleTypesToTest{}); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleZip as Input" << std::endl; - vtkm::testing::Testing::TryTypes(TestingFancyArrayHandles::TestZipAsInput(), - ZipTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandlePermutation as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestPermutationAsOutput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleView as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestViewAsOutput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleTransform as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestTransformAsOutput(), HandleTypesToTest()); - -#ifndef VTKM_NO_DEPRECATED_VIRTUAL - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleTransform with virtual as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestTransformVirtualAsOutput(), - HandleTypesToTest()); -#endif //VTKM_NO_DEPRECATED_VIRTUAL - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleDiscard as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestDiscardAsOutput(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleZip as Output" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestZipAsOutput(), ZipTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleZip as In Place" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestZipAsInPlace(), HandleTypesToTest()); - - std::cout << "-------------------------------------------" << std::endl; - std::cout << "Testing ArrayHandleConcatenate as Input" << std::endl; - vtkm::testing::Testing::TryTypes( - TestingFancyArrayHandles::TestConcatenateAsInput(), HandleTypesToTest()); - } - }; - -public: - /// Run a suite of tests to check to see if a DeviceAdapter properly supports - /// all the fancy array handles that vtkm supports. Returns an - /// error code that can be returned from the main function of a test. - /// - static VTKM_CONT int Run(int argc, char* argv[]) - { - vtkm::cont::GetRuntimeDeviceTracker().ForceDevice(DeviceAdapterTag()); - return vtkm::cont::testing::Testing::Run(TestAll(), argc, argv); - } -}; -} -} -} // namespace vtkm::cont::testing - -#endif //vtk_m_cont_testing_TestingFancyArrayHandles_h diff --git a/vtkm/cont/testing/UnitTestArrayHandleCast.cxx b/vtkm/cont/testing/UnitTestArrayHandleCast.cxx new file mode 100644 index 000000000..31855bc43 --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleCast.cxx @@ -0,0 +1,113 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +struct TestCastAsInput +{ + template + VTKM_CONT void operator()(CastToType vtkmNotUsed(type)) const + { + vtkm::cont::Invoker invoke; + using InputArrayType = vtkm::cont::ArrayHandleIndex; + + InputArrayType input(ARRAY_SIZE); + vtkm::cont::ArrayHandleCast castArray = + vtkm::cont::make_ArrayHandleCast(input, CastToType()); + vtkm::cont::ArrayHandle result; + + invoke(PassThrough{}, castArray, result); + + // verify results + vtkm::Id length = ARRAY_SIZE; + auto resultPortal = result.ReadPortal(); + auto inputPortal = input.ReadPortal(); + for (vtkm::Id i = 0; i < length; ++i) + { + VTKM_TEST_ASSERT(resultPortal.Get(i) == static_cast(inputPortal.Get(i)), + "Casting ArrayHandle Failed"); + } + + castArray.ReleaseResources(); + } +}; + +struct TestCastAsOutput +{ + template + VTKM_CONT void operator()(CastFromType vtkmNotUsed(type)) const + { + vtkm::cont::Invoker invoke; + + using InputArrayType = vtkm::cont::ArrayHandleIndex; + using ResultArrayType = vtkm::cont::ArrayHandle; + + InputArrayType input(ARRAY_SIZE); + + ResultArrayType result; + vtkm::cont::ArrayHandleCast castArray = + vtkm::cont::make_ArrayHandleCast(result); + + invoke(PassThrough{}, input, castArray); + + // verify results + vtkm::Id length = ARRAY_SIZE; + auto inputPortal = input.ReadPortal(); + auto resultPortal = result.ReadPortal(); + for (vtkm::Id i = 0; i < length; ++i) + { + VTKM_TEST_ASSERT(inputPortal.Get(i) == static_cast(resultPortal.Get(i)), + "Casting ArrayHandle Failed"); + } + } +}; + +void Run() +{ + using CastTypesToTest = vtkm::List; + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleCast as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestCastAsInput(), CastTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleCast as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestCastAsOutput(), CastTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleCast(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleConcatenate.cxx b/vtkm/cont/testing/UnitTestArrayHandleConcatenate.cxx index e3d318c97..bcdc38ce1 100644 --- a/vtkm/cont/testing/UnitTestArrayHandleConcatenate.cxx +++ b/vtkm/cont/testing/UnitTestArrayHandleConcatenate.cxx @@ -10,16 +10,101 @@ #include #include +#include + +#include #include namespace { -constexpr vtkm::Id ARRAY_SIZE = 4; +constexpr vtkm::Id ARRAY_SIZE = 10; + +template +struct IndexSquared +{ + VTKM_EXEC_CONT + ValueType operator()(vtkm::Id index) const + { + using ComponentType = typename vtkm::VecTraits::ComponentType; + return ValueType(static_cast(index * index)); + } +}; + +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +VTKM_CONT void TestConcatInvoke() +{ + using ValueType = vtkm::Id; + using FunctorType = IndexSquared; + + using ValueHandleType = vtkm::cont::ArrayHandleImplicit; + using BasicArrayType = vtkm::cont::ArrayHandle; + using ConcatenateType = vtkm::cont::ArrayHandleConcatenate; + + FunctorType functor; + for (vtkm::Id start_pos = 0; start_pos < ARRAY_SIZE; start_pos += ARRAY_SIZE / 4) + { + vtkm::Id implicitLen = ARRAY_SIZE - start_pos; + vtkm::Id basicLen = start_pos; + + // make an implicit array + ValueHandleType implicit = vtkm::cont::make_ArrayHandleImplicit(functor, implicitLen); + // make a basic array + std::vector basicVec; + for (vtkm::Id i = 0; i < basicLen; i++) + { + basicVec.push_back(ValueType(i)); + } + BasicArrayType basic = vtkm::cont::make_ArrayHandle(basicVec, vtkm::CopyFlag::Off); + + // concatenate two arrays together + ConcatenateType concatenate = vtkm::cont::make_ArrayHandleConcatenate(implicit, basic); + + vtkm::cont::ArrayHandle result; + + vtkm::cont::Invoker invoke; + invoke(PassThrough{}, concatenate, result); + + //verify that the control portal works + auto resultPortal = result.ReadPortal(); + auto implicitPortal = implicit.ReadPortal(); + auto basicPortal = basic.ReadPortal(); + auto concatPortal = concatenate.ReadPortal(); + for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) + { + const ValueType result_v = resultPortal.Get(i); + ValueType correct_value; + if (i < implicitLen) + correct_value = implicitPortal.Get(i); + else + correct_value = basicPortal.Get(i - implicitLen); + const ValueType control_value = concatPortal.Get(i); + VTKM_TEST_ASSERT(test_equal(result_v, correct_value), + "ArrayHandleConcatenate as Input Failed"); + VTKM_TEST_ASSERT(test_equal(result_v, control_value), + "ArrayHandleConcatenate as Input Failed"); + } + + concatenate.ReleaseResources(); + } +} void TestConcatOfConcat() { + std::cout << "Test concat of concat" << std::endl; + vtkm::cont::ArrayHandleIndex array1(ARRAY_SIZE); vtkm::cont::ArrayHandleIndex array2(2 * ARRAY_SIZE); @@ -56,6 +141,8 @@ void TestConcatOfConcat() void TestConcatenateEmptyArray() { + std::cout << "Test empty array" << std::endl; + std::vector vec; for (vtkm::Id i = 0; i < ARRAY_SIZE; i++) { @@ -80,6 +167,8 @@ void TestConcatenateEmptyArray() void TestConcatenateFill() { + std::cout << "Test fill" << std::endl; + using T = vtkm::FloatDefault; vtkm::cont::ArrayHandle array1; vtkm::cont::ArrayHandle array2; @@ -117,6 +206,7 @@ void TestConcatenateFill() void TestArrayHandleConcatenate() { + TestConcatInvoke(); TestConcatOfConcat(); TestConcatenateEmptyArray(); TestConcatenateFill(); diff --git a/vtkm/cont/testing/UnitTestArrayHandleConstant.cxx b/vtkm/cont/testing/UnitTestArrayHandleConstant.cxx new file mode 100644 index 000000000..6d23202c8 --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleConstant.cxx @@ -0,0 +1,82 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +using HandleTypesToTest = vtkm::List; + +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +struct TestConstantAsInput +{ + template + VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const + { + const ValueType value = TestValue(43, ValueType()); + + vtkm::cont::ArrayHandleConstant constant = + vtkm::cont::make_ArrayHandleConstant(value, ARRAY_SIZE); + + VTKM_TEST_ASSERT(constant.GetValue() == value); + + vtkm::cont::ArrayHandle result; + + vtkm::cont::Invoker invoke; + invoke(PassThrough{}, constant, result); + + //verify that the control portal works + auto resultPortal = result.ReadPortal(); + auto constantPortal = constant.ReadPortal(); + for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) + { + const ValueType result_v = resultPortal.Get(i); + const ValueType control_value = constantPortal.Get(i); + VTKM_TEST_ASSERT(test_equal(result_v, value), "Counting Handle Failed"); + VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Counting Handle Control Failed"); + } + + constant.ReleaseResources(); + } +}; + +void Run() +{ + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleConstant as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestConstantAsInput(), HandleTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleConstant(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleGroupVec.cxx b/vtkm/cont/testing/UnitTestArrayHandleGroupVec.cxx new file mode 100644 index 000000000..63c6c5ef5 --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleGroupVec.cxx @@ -0,0 +1,150 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +struct PassThrough : vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +template +struct TestGroupVecAsInput +{ + template + VTKM_CONT void operator()(ComponentType) const + { + using ValueType = vtkm::Vec; + + vtkm::cont::ArrayHandle baseArray; + baseArray.Allocate(ARRAY_SIZE * NUM_COMPONENTS); + SetPortal(baseArray.WritePortal()); + + vtkm::cont::ArrayHandleGroupVec, NUM_COMPONENTS> + groupArray(baseArray); + VTKM_TEST_ASSERT(groupArray.GetNumberOfValues() == ARRAY_SIZE, + "Group array reporting wrong array size."); + + vtkm::cont::ArrayHandle resultArray; + + vtkm::worklet::DispatcherMapField dispatcher; + dispatcher.Invoke(groupArray, resultArray); + + VTKM_TEST_ASSERT(resultArray.GetNumberOfValues() == ARRAY_SIZE, "Got bad result array size."); + + //verify that the control portal works + vtkm::Id totalIndex = 0; + auto resultPortal = resultArray.ReadPortal(); + for (vtkm::Id index = 0; index < ARRAY_SIZE; ++index) + { + const ValueType result = resultPortal.Get(index); + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; componentIndex++) + { + const ComponentType expectedValue = TestValue(totalIndex, ComponentType()); + VTKM_TEST_ASSERT(test_equal(result[componentIndex], expectedValue), + "Result array got wrong value."); + totalIndex++; + } + } + + groupArray.ReleaseResources(); + } +}; + +template +struct TestGroupVecAsOutput +{ + template + VTKM_CONT void operator()(ComponentType) const + { + using ValueType = vtkm::Vec; + + vtkm::cont::ArrayHandle baseArray; + baseArray.Allocate(ARRAY_SIZE); + SetPortal(baseArray.WritePortal()); + + vtkm::cont::ArrayHandle resultArray; + + vtkm::cont::ArrayHandleGroupVec, NUM_COMPONENTS> + groupArray(resultArray); + + vtkm::worklet::DispatcherMapField dispatcher; + dispatcher.Invoke(baseArray, groupArray); + + VTKM_TEST_ASSERT(groupArray.GetNumberOfValues() == ARRAY_SIZE, + "Group array reporting wrong array size."); + + VTKM_TEST_ASSERT(resultArray.GetNumberOfValues() == ARRAY_SIZE * NUM_COMPONENTS, + "Got bad result array size."); + + //verify that the control portal works + vtkm::Id totalIndex = 0; + auto resultPortal = resultArray.ReadPortal(); + for (vtkm::Id index = 0; index < ARRAY_SIZE; ++index) + { + const ValueType expectedValue = TestValue(index, ValueType()); + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; componentIndex++) + { + const ComponentType result = resultPortal.Get(totalIndex); + VTKM_TEST_ASSERT(test_equal(result, expectedValue[componentIndex]), + "Result array got wrong value."); + totalIndex++; + } + } + } +}; + +void Run() +{ + using HandleTypesToTest = + vtkm::List; + using ScalarTypesToTest = vtkm::List; + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleGroupVec<3> as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestGroupVecAsInput<3>(), HandleTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleGroupVec<4> as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestGroupVecAsInput<4>(), HandleTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleGroupVec<2> as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestGroupVecAsOutput<2>(), ScalarTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleGroupVec<3> as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestGroupVecAsOutput<3>(), ScalarTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleGroupVec(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleGroupVecVariable.cxx b/vtkm/cont/testing/UnitTestArrayHandleGroupVecVariable.cxx new file mode 100644 index 000000000..685d1caaa --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleGroupVecVariable.cxx @@ -0,0 +1,155 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include +#include +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +// GroupVecVariable is a bit strange because it supports values of different +// lengths, so a simple pass through worklet will not work. Use custom +// worklets. +struct GroupVariableInputWorklet : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, WorkIndex, _2); + + template + VTKM_EXEC void operator()(const InputType& input, vtkm::Id workIndex, vtkm::Id& dummyOut) const + { + using ComponentType = typename InputType::ComponentType; + vtkm::IdComponent expectedSize = static_cast(workIndex); + if (expectedSize != input.GetNumberOfComponents()) + { + this->RaiseError("Got unexpected number of components."); + } + + vtkm::Id valueIndex = workIndex * (workIndex - 1) / 2; + dummyOut = valueIndex; + for (vtkm::IdComponent componentIndex = 0; componentIndex < expectedSize; componentIndex++) + { + ComponentType expectedValue = TestValue(valueIndex, ComponentType()); + if (vtkm::Abs(expectedValue - input[componentIndex]) > 0.000001) + { + this->RaiseError("Got bad value in GroupVariableInputWorklet."); + } + valueIndex++; + } + } +}; + +struct TestGroupVecVariableAsInput +{ + template + VTKM_CONT void operator()(ComponentType) const + { + vtkm::cont::Invoker invoke; + vtkm::Id sourceArraySize; + + vtkm::cont::ArrayHandle numComponentsArray; + vtkm::cont::ArrayCopy(vtkm::cont::ArrayHandleIndex(ARRAY_SIZE), numComponentsArray); + vtkm::cont::ArrayHandle offsetsArray = + vtkm::cont::ConvertNumComponentsToOffsets(numComponentsArray, sourceArraySize); + + vtkm::cont::ArrayHandle sourceArray; + sourceArray.Allocate(sourceArraySize); + SetPortal(sourceArray.WritePortal()); + + vtkm::cont::ArrayHandle dummyArray; + + auto groupVecArray = vtkm::cont::make_ArrayHandleGroupVecVariable(sourceArray, offsetsArray); + + invoke(GroupVariableInputWorklet{}, groupVecArray, dummyArray); + + dummyArray.ReadPortal(); + + groupVecArray.ReleaseResources(); + } +}; + +// GroupVecVariable is a bit strange because it supports values of different +// lengths, so a simple pass through worklet will not work. Use custom +// worklets. +struct GroupVariableOutputWorklet : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_2, WorkIndex); + + template + VTKM_EXEC void operator()(OutputType& output, vtkm::Id workIndex) const + { + using ComponentType = typename OutputType::ComponentType; + vtkm::IdComponent expectedSize = static_cast(workIndex); + if (expectedSize != output.GetNumberOfComponents()) + { + this->RaiseError("Got unexpected number of components."); + } + + vtkm::Id valueIndex = workIndex * (workIndex - 1) / 2; + for (vtkm::IdComponent componentIndex = 0; componentIndex < expectedSize; componentIndex++) + { + output[componentIndex] = TestValue(valueIndex, ComponentType()); + valueIndex++; + } + } +}; + +struct TestGroupVecVariableAsOutput +{ + template + VTKM_CONT void operator()(ComponentType) const + { + vtkm::Id sourceArraySize; + + vtkm::cont::ArrayHandle numComponentsArray; + vtkm::cont::ArrayCopy(vtkm::cont::ArrayHandleIndex(ARRAY_SIZE), numComponentsArray); + vtkm::cont::ArrayHandle offsetsArray = + vtkm::cont::ConvertNumComponentsToOffsets(numComponentsArray, sourceArraySize); + + vtkm::cont::ArrayHandle sourceArray; + sourceArray.Allocate(sourceArraySize); + + vtkm::worklet::DispatcherMapField dispatcher; + dispatcher.Invoke(vtkm::cont::ArrayHandleIndex(ARRAY_SIZE), + vtkm::cont::make_ArrayHandleGroupVecVariable(sourceArray, offsetsArray)); + + CheckPortal(sourceArray.ReadPortal()); + } +}; + +void Run() +{ + using ScalarTypesToTest = vtkm::List; + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleGroupVecVariable as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestGroupVecVariableAsInput(), ScalarTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleGroupVecVariable as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestGroupVecVariableAsOutput(), ScalarTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleGroupVecVariable(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleImplicit.cxx b/vtkm/cont/testing/UnitTestArrayHandleImplicit.cxx index cbde27750..e31d2b835 100644 --- a/vtkm/cont/testing/UnitTestArrayHandleImplicit.cxx +++ b/vtkm/cont/testing/UnitTestArrayHandleImplicit.cxx @@ -10,8 +10,11 @@ #include #include +#include #include +#include + #include #include @@ -32,6 +35,18 @@ struct IndexSquared } }; +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + struct ImplicitTests { template @@ -44,7 +59,7 @@ struct ImplicitTests ImplicitHandle implicit = vtkm::cont::make_ArrayHandleImplicit(functor, ARRAY_SIZE); - //verify that the control portal works + std::cout << "verify that the control portal works" << std::endl; auto implicitPortal = implicit.ReadPortal(); for (int i = 0; i < ARRAY_SIZE; ++i) { @@ -53,7 +68,7 @@ struct ImplicitTests VTKM_TEST_ASSERT(v == correct_value, "Implicit Handle Failed"); } - //verify that the execution portal works + std::cout << "verify that the execution portal works" << std::endl; vtkm::cont::Token token; using Device = vtkm::cont::DeviceAdapterTagSerial; using CEPortal = typename ImplicitHandle::ReadPortalType; @@ -64,6 +79,18 @@ struct ImplicitTests const ValueType correct_value = functor(i); VTKM_TEST_ASSERT(v == correct_value, "Implicit Handle Failed"); } + + std::cout << "verify that the array handle works in a worklet on the device" << std::endl; + vtkm::cont::Invoker invoke; + vtkm::cont::ArrayHandle result; + invoke(PassThrough{}, implicit, result); + auto resultPortal = result.ReadPortal(); + for (int i = 0; i < ARRAY_SIZE; ++i) + { + const ValueType value = resultPortal.Get(i); + const ValueType correctValue = functor(i); + VTKM_TEST_ASSERT(test_equal(value, correctValue)); + } } }; diff --git a/vtkm/cont/testing/UnitTestArrayHandleRecombineVec.cxx b/vtkm/cont/testing/UnitTestArrayHandleRecombineVec.cxx new file mode 100644 index 000000000..68a984a98 --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleRecombineVec.cxx @@ -0,0 +1,109 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +struct PassThrough : vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +struct TestRecombineVecAsInput +{ + template + VTKM_CONT void operator()(T) const + { + vtkm::cont::ArrayHandle baseArray; + baseArray.Allocate(ARRAY_SIZE); + SetPortal(baseArray.WritePortal()); + + using VTraits = vtkm::VecTraits; + vtkm::cont::ArrayHandleRecombineVec 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 outputArray; + vtkm::cont::Invoker invoke; + invoke(PassThrough{}, recombinedArray, outputArray); + + VTKM_TEST_ASSERT(test_equal_ArrayHandles(baseArray, outputArray)); + } +}; + +struct TestRecombineVecAsOutput +{ + template + VTKM_CONT void operator()(T) const + { + vtkm::cont::ArrayHandle baseArray; + baseArray.Allocate(ARRAY_SIZE); + SetPortal(baseArray.WritePortal()); + + vtkm::cont::ArrayHandle outputArray; + outputArray.Allocate(ARRAY_SIZE); // Cannot resize after recombine + + using VTraits = vtkm::VecTraits; + vtkm::cont::ArrayHandleRecombineVec 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(PassThrough{}, baseArray, recombinedArray); + + VTKM_TEST_ASSERT(test_equal_ArrayHandles(baseArray, outputArray)); + } +}; + +void Run() +{ + using HandleTypesToTest = + vtkm::List; + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleRecombineVec as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestRecombineVecAsInput(), HandleTypesToTest{}); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleRecombineVec as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestRecombineVecAsOutput(), HandleTypesToTest{}); +} + +} // anonymous namespace + +int UnitTestArrayHandleRecombineVec(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleSOA.cxx b/vtkm/cont/testing/UnitTestArrayHandleSOA.cxx new file mode 100644 index 000000000..e07b2f2fb --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleSOA.cxx @@ -0,0 +1,235 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +using ScalarTypesToTest = vtkm::List; +using VectorTypesToTest = vtkm::List; + +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +struct TestArrayPortalSOA +{ + template + VTKM_CONT void operator()(ComponentType) const + { + constexpr vtkm::IdComponent NUM_COMPONENTS = 4; + using ValueType = vtkm::Vec; + using ComponentArrayType = vtkm::cont::ArrayHandle; + using SOAPortalType = + vtkm::internal::ArrayPortalSOA; + + std::cout << "Test SOA portal reflects data in component portals." << std::endl; + SOAPortalType soaPortalIn(ARRAY_SIZE); + + std::array, NUM_COMPONENTS> implArrays; + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) + { + vtkm::cont::ArrayHandle array; + array.Allocate(ARRAY_SIZE); + auto portal = array.WritePortal(); + for (vtkm::IdComponent valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) + { + portal.Set(valueIndex, TestValue(valueIndex, ValueType{})[componentIndex]); + } + + soaPortalIn.SetPortal(componentIndex, portal); + + implArrays[static_cast(componentIndex)] = array; + } + + VTKM_TEST_ASSERT(soaPortalIn.GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(soaPortalIn); + + std::cout << "Test data set in SOA portal gets set in component portals." << std::endl; + { + SOAPortalType soaPortalOut(ARRAY_SIZE); + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) + { + vtkm::cont::ArrayHandle array; + array.Allocate(ARRAY_SIZE); + auto portal = array.WritePortal(); + soaPortalOut.SetPortal(componentIndex, portal); + + implArrays[static_cast(componentIndex)] = array; + } + + SetPortal(soaPortalOut); + } + + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) + { + auto portal = implArrays[static_cast(componentIndex)].ReadPortal(); + for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) + { + ComponentType x = TestValue(valueIndex, ValueType{})[componentIndex]; + VTKM_TEST_ASSERT(test_equal(x, portal.Get(valueIndex))); + } + } + } +}; + +struct TestSOAAsInput +{ + template + VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const + { + using VTraits = vtkm::VecTraits; + using ComponentType = typename VTraits::ComponentType; + constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS; + + { + vtkm::cont::ArrayHandleSOA soaArray; + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) + { + vtkm::cont::ArrayHandle componentArray; + componentArray.Allocate(ARRAY_SIZE); + auto componentPortal = componentArray.WritePortal(); + for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) + { + componentPortal.Set( + valueIndex, VTraits::GetComponent(TestValue(valueIndex, ValueType{}), componentIndex)); + } + soaArray.SetArray(componentIndex, componentArray); + } + + VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); + VTKM_TEST_ASSERT(soaArray.ReadPortal().GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(soaArray.ReadPortal()); + + vtkm::cont::ArrayHandle basicArray; + vtkm::cont::ArrayCopyDevice(soaArray, basicArray); + VTKM_TEST_ASSERT(basicArray.GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(basicArray.ReadPortal()); + } + + { + // Check constructors + using Vec3 = vtkm::Vec; + std::vector vector0; + std::vector vector1; + std::vector vector2; + for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) + { + Vec3 value = TestValue(valueIndex, Vec3{}); + vector0.push_back(value[0]); + vector1.push_back(value[1]); + vector2.push_back(value[2]); + } + + { + vtkm::cont::ArrayHandleSOA soaArray = + vtkm::cont::make_ArrayHandleSOA({ vector0, vector1, vector2 }); + VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(soaArray.ReadPortal()); + } + + { + vtkm::cont::ArrayHandleSOA soaArray = + vtkm::cont::make_ArrayHandleSOA(vtkm::CopyFlag::Off, vector0, vector1, vector2); + VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(soaArray.ReadPortal()); + + // Make sure calling ReleaseResources does not result in error. + soaArray.ReleaseResources(); + } + + { + vtkm::cont::ArrayHandleSOA soaArray = vtkm::cont::make_ArrayHandleSOA( + { vector0.data(), vector1.data(), vector2.data() }, ARRAY_SIZE, vtkm::CopyFlag::Off); + VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(soaArray.ReadPortal()); + } + + { + vtkm::cont::ArrayHandleSOA soaArray = vtkm::cont::make_ArrayHandleSOA( + ARRAY_SIZE, vtkm::CopyFlag::Off, vector0.data(), vector1.data(), vector2.data()); + VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); + CheckPortal(soaArray.ReadPortal()); + } + } + } +}; + +struct TestSOAAsOutput +{ + template + VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const + { + using VTraits = vtkm::VecTraits; + using ComponentType = typename VTraits::ComponentType; + constexpr vtkm::IdComponent NUM_COMPONENTS = VTraits::NUM_COMPONENTS; + + vtkm::cont::ArrayHandle basicArray; + basicArray.Allocate(ARRAY_SIZE); + SetPortal(basicArray.WritePortal()); + + vtkm::cont::ArrayHandleSOA soaArray; + vtkm::cont::Invoker{}(PassThrough{}, basicArray, soaArray); + + VTKM_TEST_ASSERT(soaArray.GetNumberOfValues() == ARRAY_SIZE); + for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; ++componentIndex) + { + vtkm::cont::ArrayHandle componentArray = soaArray.GetArray(componentIndex); + auto componentPortal = componentArray.ReadPortal(); + for (vtkm::Id valueIndex = 0; valueIndex < ARRAY_SIZE; ++valueIndex) + { + ComponentType expected = + VTraits::GetComponent(TestValue(valueIndex, ValueType{}), componentIndex); + ComponentType got = componentPortal.Get(valueIndex); + VTKM_TEST_ASSERT(test_equal(expected, got)); + } + } + } +}; + +static void Run() +{ + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayPortalSOA" << std::endl; + vtkm::testing::Testing::TryTypes(TestArrayPortalSOA(), ScalarTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleSOA as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestSOAAsInput(), VectorTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleSOA as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestSOAAsOutput(), VectorTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleSOA(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleTransform.cxx b/vtkm/cont/testing/UnitTestArrayHandleTransform.cxx index 22be669a9..930e45819 100644 --- a/vtkm/cont/testing/UnitTestArrayHandleTransform.cxx +++ b/vtkm/cont/testing/UnitTestArrayHandleTransform.cxx @@ -75,6 +75,51 @@ VTKM_CONT void CheckControlPortals(const OriginalArrayHandleType& originalArray, } } +struct ValueScale +{ + ValueScale() + : Factor(1.0) + { + } + + ValueScale(vtkm::Float64 factor) + : Factor(factor) + { + } + + template + VTKM_EXEC_CONT ValueType operator()(const ValueType& v) const + { + using Traits = vtkm::VecTraits; + using TTraits = vtkm::TypeTraits; + using ComponentType = typename Traits::ComponentType; + + ValueType result = TTraits::ZeroInitialization(); + for (vtkm::IdComponent i = 0; i < Traits::GetNumberOfComponents(v); ++i) + { + vtkm::Float64 vi = static_cast(Traits::GetComponent(v, i)); + vtkm::Float64 ri = vi * this->Factor; + Traits::SetComponent(result, i, static_cast(ri)); + } + return result; + } + +private: + vtkm::Float64 Factor; +}; + +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + template struct TransformTests { @@ -107,19 +152,13 @@ struct TransformTests std::cout << "Test a transform handle with a normal handle as the values" << std::endl; //we are going to connect the two handles up, and than fill - //the values and make the transform sees the new values in the handle + //the values and make sure the transform sees the new values in the handle vtkm::cont::ArrayHandle input; TransformHandle thandle(input, functor); using Portal = typename vtkm::cont::ArrayHandle::WritePortalType; input.Allocate(ARRAY_SIZE); - { - Portal portal = input.WritePortal(); - for (vtkm::Id index = 0; index < ARRAY_SIZE; ++index) - { - portal.Set(index, TestValue(index, InputValueType())); - } - } + SetPortal(input.WritePortal()); CheckControlPortals(input, thandle); @@ -139,6 +178,33 @@ struct TransformTests std::cout << " Verify that the execution portal works" << std::endl; invoke(CheckTransformWorklet{}, input, thandle); + + std::cout << "Write to a transformed array with an inverse transform" << std::endl; + { + ValueScale scaleUp(2.0); + ValueScale scaleDown(1.0 / 2.0); + + input.Allocate(ARRAY_SIZE); + SetPortal(input.WritePortal()); + + vtkm::cont::ArrayHandle output; + auto transformed = vtkm::cont::make_ArrayHandleTransform(output, scaleUp, scaleDown); + + invoke(PassThrough{}, input, transformed); + + //verify that the control portal works + auto outputPortal = output.ReadPortal(); + auto transformedPortal = transformed.ReadPortal(); + for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) + { + const InputValueType result_v = outputPortal.Get(i); + const InputValueType correct_value = scaleDown(TestValue(i, InputValueType())); + const InputValueType control_value = transformedPortal.Get(i); + VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Handle Failed"); + VTKM_TEST_ASSERT(test_equal(scaleUp(result_v), control_value), + "Transform Handle Control Failed"); + } + } } }; diff --git a/vtkm/cont/testing/UnitTestArrayHandleView.cxx b/vtkm/cont/testing/UnitTestArrayHandleView.cxx new file mode 100644 index 000000000..d7330e37d --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleView.cxx @@ -0,0 +1,145 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +template +struct IndexSquared +{ + VTKM_EXEC_CONT + ValueType operator()(vtkm::Id index) const + { + using ComponentType = typename vtkm::VecTraits::ComponentType; + return ValueType(static_cast(index * index)); + } +}; + +struct PassThrough : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +struct TestViewAsInput +{ + template + VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const + { + using FunctorType = IndexSquared; + + using ValueHandleType = vtkm::cont::ArrayHandleImplicit; + using ViewHandleType = vtkm::cont::ArrayHandleView; + + FunctorType functor; + for (vtkm::Id start_pos = 0; start_pos < ARRAY_SIZE; start_pos += ARRAY_SIZE / 4) + { + const vtkm::Id counting_ARRAY_SIZE = ARRAY_SIZE - start_pos; + + ValueHandleType implicit = vtkm::cont::make_ArrayHandleImplicit(functor, ARRAY_SIZE); + + ViewHandleType view = + vtkm::cont::make_ArrayHandleView(implicit, start_pos, counting_ARRAY_SIZE); + + vtkm::cont::ArrayHandle result; + + vtkm::cont::Invoker invoke; + invoke(PassThrough{}, view, result); + + //verify that the control portal works + auto resultPortal = result.ReadPortal(); + auto implicitPortal = implicit.ReadPortal(); + auto viewPortal = view.ReadPortal(); + for (vtkm::Id i = 0; i < counting_ARRAY_SIZE; ++i) + { + const vtkm::Id value_index = i; + const vtkm::Id key_index = start_pos + i; + + const ValueType result_v = resultPortal.Get(value_index); + const ValueType correct_value = implicitPortal.Get(key_index); + const ValueType control_value = viewPortal.Get(value_index); + VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Implicit Handle Failed"); + VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Implicit Handle Failed"); + } + + view.ReleaseResources(); + } + } +}; + +struct TestViewAsOutput +{ + template + VTKM_CONT void operator()(const ValueType vtkmNotUsed(v)) const + { + using ValueHandleType = vtkm::cont::ArrayHandle; + using ViewHandleType = vtkm::cont::ArrayHandleView; + + vtkm::cont::ArrayHandle input; + input.Allocate(ARRAY_SIZE); + SetPortal(input.WritePortal()); + + ValueHandleType values; + values.Allocate(ARRAY_SIZE * 2); + + ViewHandleType view = vtkm::cont::make_ArrayHandleView(values, ARRAY_SIZE, ARRAY_SIZE); + vtkm::cont::Invoker invoke; + invoke(PassThrough{}, input, view); + + //verify that the control portal works + CheckPortal(view.ReadPortal()); + + //verify that filling works + const ValueType expected = TestValue(20, ValueType{}); + view.Fill(expected); + auto valuesPortal = values.ReadPortal(); + for (vtkm::Id index = ARRAY_SIZE; index < 2 * ARRAY_SIZE; ++index) + { + VTKM_TEST_ASSERT(valuesPortal.Get(index) == expected); + } + } +}; + +void Run() +{ + using HandleTypesToTest = + vtkm::List; + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleView as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestViewAsInput(), HandleTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleView as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestViewAsOutput(), HandleTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleView(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +} diff --git a/vtkm/cont/testing/UnitTestArrayHandleZip.cxx b/vtkm/cont/testing/UnitTestArrayHandleZip.cxx new file mode 100644 index 000000000..1c776d5ba --- /dev/null +++ b/vtkm/cont/testing/UnitTestArrayHandleZip.cxx @@ -0,0 +1,204 @@ +//============================================================================ +// 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. +//============================================================================ + +#include + +#include + +#include + +#include + +namespace +{ + +constexpr vtkm::Id ARRAY_SIZE = 10; + +struct PassThrough : vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn, FieldOut); + using ExecutionSignature = void(_1, _2); + + template + VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const + { + outValue = inValue; + } +}; + +struct InplaceFunctorPair : vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldInOut); + using ExecutionSignature = void(_1); + + template + VTKM_EXEC void operator()(vtkm::Pair& value) const + { + value.second = value.first; + } +}; + +struct TestZipAsInput +{ + vtkm::cont::Invoker Invoke; + + template + VTKM_CONT void operator()(vtkm::Pair vtkmNotUsed(pair)) const + { + using PairType = vtkm::Pair; + using KeyComponentType = typename vtkm::VecTraits::ComponentType; + using ValueComponentType = typename vtkm::VecTraits::ComponentType; + + KeyType testKeys[ARRAY_SIZE]; + ValueType testValues[ARRAY_SIZE]; + + for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) + { + testKeys[i] = KeyType(static_cast(ARRAY_SIZE - i)); + testValues[i] = ValueType(static_cast(i)); + } + vtkm::cont::ArrayHandle keys = + vtkm::cont::make_ArrayHandle(testKeys, ARRAY_SIZE, vtkm::CopyFlag::Off); + vtkm::cont::ArrayHandle values = + vtkm::cont::make_ArrayHandle(testValues, ARRAY_SIZE, vtkm::CopyFlag::Off); + + vtkm::cont::ArrayHandleZip, vtkm::cont::ArrayHandle> + zip = vtkm::cont::make_ArrayHandleZip(keys, values); + + vtkm::cont::ArrayHandle result; + + this->Invoke(PassThrough{}, zip, result); + + //verify that the control portal works + auto resultPortal = result.ReadPortal(); + for (int i = 0; i < ARRAY_SIZE; ++i) + { + const PairType result_v = resultPortal.Get(i); + const PairType correct_value(KeyType(static_cast(ARRAY_SIZE - i)), + ValueType(static_cast(i))); + VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "ArrayHandleZip Failed as input"); + } + + zip.ReleaseResources(); + } +}; + +struct TestZipAsOutput +{ + vtkm::cont::Invoker Invoke; + + template + VTKM_CONT void operator()(vtkm::Pair vtkmNotUsed(pair)) const + { + using PairType = vtkm::Pair; + using KeyComponentType = typename vtkm::VecTraits::ComponentType; + using ValueComponentType = typename vtkm::VecTraits::ComponentType; + + PairType testKeysAndValues[ARRAY_SIZE]; + for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i) + { + testKeysAndValues[i] = PairType(KeyType(static_cast(ARRAY_SIZE - i)), + ValueType(static_cast(i))); + } + vtkm::cont::ArrayHandle input = + vtkm::cont::make_ArrayHandle(testKeysAndValues, ARRAY_SIZE, vtkm::CopyFlag::Off); + + vtkm::cont::ArrayHandle result_keys; + vtkm::cont::ArrayHandle result_values; + vtkm::cont::ArrayHandleZip, vtkm::cont::ArrayHandle> + result_zip = vtkm::cont::make_ArrayHandleZip(result_keys, result_values); + + this->Invoke(PassThrough{}, input, result_zip); + + //now the two arrays we have zipped should have data inside them + auto keysPortal = result_keys.ReadPortal(); + auto valsPortal = result_values.ReadPortal(); + for (int i = 0; i < ARRAY_SIZE; ++i) + { + const KeyType result_key = keysPortal.Get(i); + const ValueType result_value = valsPortal.Get(i); + + VTKM_TEST_ASSERT( + test_equal(result_key, KeyType(static_cast(ARRAY_SIZE - i))), + "ArrayHandleZip Failed as input for key"); + VTKM_TEST_ASSERT(test_equal(result_value, ValueType(static_cast(i))), + "ArrayHandleZip Failed as input for value"); + } + + // Test filling the zipped array. + vtkm::cont::printSummary_ArrayHandle(result_zip, std::cout, true); + PairType fillValue{ TestValue(1, KeyType{}), TestValue(2, ValueType{}) }; + result_zip.Fill(fillValue, 1); + vtkm::cont::printSummary_ArrayHandle(result_zip, std::cout, true); + keysPortal = result_keys.ReadPortal(); + valsPortal = result_values.ReadPortal(); + // First entry should be the same. + VTKM_TEST_ASSERT( + test_equal(keysPortal.Get(0), KeyType(static_cast(ARRAY_SIZE)))); + VTKM_TEST_ASSERT(test_equal(valsPortal.Get(0), ValueType(static_cast(0)))); + // The rest should be fillValue + for (vtkm::Id index = 1; index < ARRAY_SIZE; ++index) + { + const KeyType result_key = keysPortal.Get(index); + const ValueType result_value = valsPortal.Get(index); + + VTKM_TEST_ASSERT(test_equal(result_key, fillValue.first)); + VTKM_TEST_ASSERT(test_equal(result_value, fillValue.second)); + } + } +}; + +struct TestZipAsInPlace +{ + vtkm::cont::Invoker Invoke; + + template + VTKM_CONT void operator()(ValueType) const + { + vtkm::cont::ArrayHandle inputValues; + inputValues.Allocate(ARRAY_SIZE); + SetPortal(inputValues.WritePortal()); + + vtkm::cont::ArrayHandle outputValues; + outputValues.Allocate(ARRAY_SIZE); + + this->Invoke(InplaceFunctorPair{}, vtkm::cont::make_ArrayHandleZip(inputValues, outputValues)); + + CheckPortal(outputValues.ReadPortal()); + } +}; + +void Run() +{ + using ZipTypesToTest = vtkm::List, + vtkm::Pair, + vtkm::Pair>; + using HandleTypesToTest = + vtkm::List; + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleZip as Input" << std::endl; + vtkm::testing::Testing::TryTypes(TestZipAsInput(), ZipTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleZip as Output" << std::endl; + vtkm::testing::Testing::TryTypes(TestZipAsOutput(), ZipTypesToTest()); + + std::cout << "-------------------------------------------" << std::endl; + std::cout << "Testing ArrayHandleZip as In Place" << std::endl; + vtkm::testing::Testing::TryTypes(TestZipAsInPlace(), HandleTypesToTest()); +} + +} // anonymous namespace + +int UnitTestArrayHandleZip(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(Run, argc, argv); +}