mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-08 13:23:51 +00:00
Consolidate UnitTestVirtualObjectHandle
Previously, each device adapter implementation had their own version of this test by including a common header. Simplify this by making a single test in UnitTests_vtkm_cont_testing, which can now be compiled for and tested on a device.
This commit is contained in:
parent
896065044a
commit
d967205817
@ -16,10 +16,4 @@ set(unit_tests
|
||||
UnitTestCudaRuntimeDeviceConfiguration.cu
|
||||
)
|
||||
|
||||
if (NOT VTKm_NO_DEPRECATED_VIRTUAL)
|
||||
set(unit_tests ${unit_tests}
|
||||
UnitTestCudaVirtualObjectHandle.cu
|
||||
)
|
||||
endif()
|
||||
|
||||
vtkm_unit_tests(SOURCES ${unit_tests} LABEL "CUDA" LIBRARIES vtkm_worklet BACKEND cuda)
|
||||
|
@ -1,35 +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 <vtkm/cont/RuntimeDeviceTracker.h>
|
||||
#include <vtkm/cont/testing/TestingVirtualObjectHandle.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void TestVirtualObjectHandle()
|
||||
{
|
||||
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
|
||||
|
||||
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{});
|
||||
using DeviceAdapterList = vtkm::List<vtkm::cont::DeviceAdapterTagCuda>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList>::Run();
|
||||
|
||||
tracker.Reset();
|
||||
using DeviceAdapterList2 =
|
||||
vtkm::List<vtkm::cont::DeviceAdapterTagSerial, vtkm::cont::DeviceAdapterTagCuda>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList2>::Run();
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestCudaVirtualObjectHandle(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestVirtualObjectHandle, argc, argv);
|
||||
}
|
@ -13,12 +13,6 @@ set(unit_tests
|
||||
UnitTestKokkosRuntimeDeviceConfiguration.cxx
|
||||
)
|
||||
|
||||
if (NOT VTKm_NO_DEPRECATED_VIRTUAL)
|
||||
set(unit_tests ${unit_tests}
|
||||
UnitTestKokkosVirtualObjectHandle.cxx
|
||||
)
|
||||
endif()
|
||||
|
||||
vtkm_unit_tests(SOURCES ${unit_tests} LABEL "KOKKOS" LIBRARIES vtkm_worklet BACKEND kokkos)
|
||||
|
||||
if (TARGET vtkm::kokkos_cuda)
|
||||
|
@ -1,35 +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 <vtkm/cont/RuntimeDeviceTracker.h>
|
||||
#include <vtkm/cont/testing/TestingVirtualObjectHandle.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void TestVirtualObjectHandle()
|
||||
{
|
||||
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
|
||||
|
||||
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagKokkos{});
|
||||
using DeviceAdapterList = vtkm::List<vtkm::cont::DeviceAdapterTagKokkos>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList>::Run();
|
||||
|
||||
tracker.Reset();
|
||||
using DeviceAdapterList2 =
|
||||
vtkm::List<vtkm::cont::DeviceAdapterTagSerial, vtkm::cont::DeviceAdapterTagKokkos>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList2>::Run();
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestKokkosVirtualObjectHandle(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestVirtualObjectHandle, argc, argv);
|
||||
}
|
@ -13,12 +13,6 @@ set(unit_tests
|
||||
UnitTestOpenMPRuntimeDeviceConfiguration.cxx
|
||||
)
|
||||
|
||||
if (NOT VTKm_NO_DEPRECATED_VIRTUAL)
|
||||
set(unit_tests ${unit_tests}
|
||||
UnitTestOpenMPVirtualObjectHandle.cxx
|
||||
)
|
||||
endif()
|
||||
|
||||
vtkm_unit_tests(SOURCES ${unit_tests}
|
||||
LABEL "OPENMP"
|
||||
DEFINES VTKM_NO_ERROR_ON_MIXED_CUDA_CXX_TAG
|
||||
|
@ -1,37 +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 <vtkm/cont/RuntimeDeviceTracker.h>
|
||||
#include <vtkm/cont/openmp/DeviceAdapterOpenMP.h>
|
||||
#include <vtkm/cont/testing/TestingVirtualObjectHandle.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void TestVirtualObjectHandle()
|
||||
{
|
||||
using DeviceAdapterList = vtkm::List<vtkm::cont::DeviceAdapterTagOpenMP>;
|
||||
using DeviceAdapterList2 =
|
||||
vtkm::List<vtkm::cont::DeviceAdapterTagSerial, vtkm::cont::DeviceAdapterTagOpenMP>;
|
||||
|
||||
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
|
||||
|
||||
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagOpenMP{});
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList>::Run();
|
||||
|
||||
tracker.Reset();
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList2>::Run();
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestOpenMPVirtualObjectHandle(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestVirtualObjectHandle, argc, argv);
|
||||
}
|
@ -12,12 +12,6 @@ set(unit_tests
|
||||
UnitTestSerialDeviceAdapter.cxx
|
||||
)
|
||||
|
||||
if (NOT VTKm_NO_DEPRECATED_VIRTUAL)
|
||||
set(unit_tests ${unit_tests}
|
||||
UnitTestSerialVirtualObjectHandle.cxx
|
||||
)
|
||||
endif()
|
||||
|
||||
vtkm_unit_tests(SOURCES ${unit_tests}
|
||||
LABEL "SERIAL"
|
||||
DEFINES VTKM_NO_ERROR_ON_MIXED_CUDA_CXX_TAG
|
||||
|
@ -1,30 +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 <vtkm/cont/RuntimeDeviceTracker.h>
|
||||
#include <vtkm/cont/testing/TestingVirtualObjectHandle.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void TestVirtualObjectHandle()
|
||||
{
|
||||
using DeviceAdapterList = vtkm::List<vtkm::cont::DeviceAdapterTagSerial>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList>::Run();
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestSerialVirtualObjectHandle(int argc, char* argv[])
|
||||
{
|
||||
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
|
||||
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagSerial{});
|
||||
return vtkm::cont::testing::Testing::Run(TestVirtualObjectHandle, argc, argv);
|
||||
}
|
@ -13,12 +13,6 @@ set(unit_tests
|
||||
UnitTestTBBRuntimeDeviceConfiguration.cxx
|
||||
)
|
||||
|
||||
if (NOT VTKm_NO_DEPRECATED_VIRTUAL)
|
||||
set(unit_tests ${unit_tests}
|
||||
UnitTestTBBVirtualObjectHandle.cxx
|
||||
)
|
||||
endif()
|
||||
|
||||
vtkm_unit_tests(SOURCES ${unit_tests}
|
||||
LABEL "TBB"
|
||||
DEFINES VTKM_NO_ERROR_ON_MIXED_CUDA_CXX_TAG
|
||||
|
@ -1,37 +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 <vtkm/cont/RuntimeDeviceTracker.h>
|
||||
#include <vtkm/cont/testing/TestingVirtualObjectHandle.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void TestVirtualObjectHandle()
|
||||
{
|
||||
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
|
||||
|
||||
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagTBB{});
|
||||
using DeviceAdapterList = vtkm::List<vtkm::cont::DeviceAdapterTagTBB>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList>::Run();
|
||||
|
||||
tracker.Reset();
|
||||
using DeviceAdapterList2 =
|
||||
vtkm::List<vtkm::cont::DeviceAdapterTagSerial, vtkm::cont::DeviceAdapterTagTBB>;
|
||||
vtkm::cont::testing::TestingVirtualObjectHandle<DeviceAdapterList2>::Run();
|
||||
}
|
||||
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestTBBVirtualObjectHandle(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestVirtualObjectHandle, argc, argv);
|
||||
}
|
@ -15,7 +15,6 @@ set(headers
|
||||
TestingDeviceAdapter.h
|
||||
TestingRuntimeDeviceConfiguration.h
|
||||
TestingSerialization.h
|
||||
TestingVirtualObjectHandle.h
|
||||
)
|
||||
|
||||
set(unit_tests
|
||||
@ -110,6 +109,12 @@ set(unit_tests_device
|
||||
UnitTestPointLocatorSparseGrid.cxx
|
||||
)
|
||||
|
||||
if (NOT VTKm_NO_DEPRECATED_VIRTUAL)
|
||||
list(APPEND unit_tests_device
|
||||
UnitTestVirtualObjectHandle.cxx
|
||||
)
|
||||
endif()
|
||||
|
||||
set(library_sources
|
||||
TestEqualArrayHandles.cxx
|
||||
Testing.cxx
|
||||
|
@ -1,232 +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_TestingVirtualObjectHandle_h
|
||||
#define vtk_m_cont_testing_TestingVirtualObjectHandle_h
|
||||
|
||||
#include <vtkm/Types.h>
|
||||
#include <vtkm/cont/ArrayHandle.h>
|
||||
#include <vtkm/cont/ArrayHandleTransform.h>
|
||||
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
|
||||
#include <vtkm/cont/VirtualObjectHandle.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
#ifdef VTKM_NO_DEPRECATED_VIRTUAL
|
||||
#error "This test should be disabled if the VTKm_NO_DEPRECATED_VIRTUAL is true."
|
||||
#endif //VTKM_NO_DEPRECATED_VIRTUAL
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_BEGIN
|
||||
|
||||
#define ARRAY_LEN 8
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
namespace cont
|
||||
{
|
||||
namespace testing
|
||||
{
|
||||
|
||||
namespace virtual_object_detail
|
||||
{
|
||||
|
||||
class Transformer : public vtkm::VirtualObjectBase
|
||||
{
|
||||
public:
|
||||
VTKM_EXEC
|
||||
virtual vtkm::FloatDefault Eval(vtkm::FloatDefault val) const = 0;
|
||||
};
|
||||
|
||||
class Square : public Transformer
|
||||
{
|
||||
public:
|
||||
VTKM_EXEC
|
||||
vtkm::FloatDefault Eval(vtkm::FloatDefault val) const override { return val * val; }
|
||||
};
|
||||
|
||||
class Multiply : public Transformer
|
||||
{
|
||||
public:
|
||||
VTKM_CONT
|
||||
void SetMultiplicand(vtkm::FloatDefault val)
|
||||
{
|
||||
this->Multiplicand = val;
|
||||
this->Modified();
|
||||
}
|
||||
|
||||
VTKM_CONT
|
||||
vtkm::FloatDefault GetMultiplicand() const { return this->Multiplicand; }
|
||||
|
||||
VTKM_EXEC
|
||||
vtkm::FloatDefault Eval(vtkm::FloatDefault val) const override
|
||||
{
|
||||
return val * this->Multiplicand;
|
||||
}
|
||||
|
||||
private:
|
||||
vtkm::FloatDefault Multiplicand = 0.0f;
|
||||
};
|
||||
|
||||
class TransformerFunctor
|
||||
{
|
||||
public:
|
||||
TransformerFunctor() = default;
|
||||
explicit TransformerFunctor(const Transformer* impl)
|
||||
: Impl(impl)
|
||||
{
|
||||
}
|
||||
|
||||
VTKM_EXEC
|
||||
vtkm::FloatDefault operator()(vtkm::FloatDefault val) const { return this->Impl->Eval(val); }
|
||||
|
||||
private:
|
||||
const Transformer* Impl;
|
||||
};
|
||||
|
||||
} // virtual_object_detail
|
||||
|
||||
template <typename DeviceAdapterList>
|
||||
class TestingVirtualObjectHandle
|
||||
{
|
||||
private:
|
||||
using FloatArrayHandle = vtkm::cont::ArrayHandle<vtkm::FloatDefault>;
|
||||
using ArrayTransform =
|
||||
vtkm::cont::ArrayHandleTransform<FloatArrayHandle, virtual_object_detail::TransformerFunctor>;
|
||||
using TransformerHandle = vtkm::cont::VirtualObjectHandle<virtual_object_detail::Transformer>;
|
||||
|
||||
class TestStage1
|
||||
{
|
||||
public:
|
||||
TestStage1(const FloatArrayHandle& input, TransformerHandle& handle)
|
||||
: Input(&input)
|
||||
, Handle(&handle)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename DeviceAdapter>
|
||||
void operator()(DeviceAdapter device) const
|
||||
{
|
||||
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
|
||||
std::cout << "\tDeviceAdapter: " << vtkm::cont::DeviceAdapterTraits<DeviceAdapter>::GetName()
|
||||
<< std::endl;
|
||||
|
||||
for (int n = 0; n < 2; ++n)
|
||||
{
|
||||
vtkm::cont::Token token;
|
||||
virtual_object_detail::TransformerFunctor tfnctr(
|
||||
this->Handle->PrepareForExecution(device, token));
|
||||
ArrayTransform transformed(*this->Input, tfnctr);
|
||||
|
||||
FloatArrayHandle output;
|
||||
Algorithm::Copy(transformed, output);
|
||||
auto portal = output.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < ARRAY_LEN; ++i)
|
||||
{
|
||||
vtkm::FloatDefault expected = TestValue(i, vtkm::FloatDefault{});
|
||||
expected = expected * expected;
|
||||
VTKM_TEST_ASSERT(
|
||||
test_equal(portal.Get(i), expected), "Expected ", expected, " but got ", portal.Get(i));
|
||||
}
|
||||
std::cout << "\tSuccess." << std::endl;
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
std::cout << "\tReleaseResources and test again..." << std::endl;
|
||||
this->Handle->ReleaseExecutionResources();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const FloatArrayHandle* Input;
|
||||
TransformerHandle* Handle;
|
||||
};
|
||||
|
||||
class TestStage2
|
||||
{
|
||||
public:
|
||||
TestStage2(const FloatArrayHandle& input,
|
||||
virtual_object_detail::Multiply& mul,
|
||||
TransformerHandle& handle)
|
||||
: Input(&input)
|
||||
, Mul(&mul)
|
||||
, Handle(&handle)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename DeviceAdapter>
|
||||
void operator()(DeviceAdapter device) const
|
||||
{
|
||||
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
|
||||
std::cout << "\tDeviceAdapter: " << vtkm::cont::DeviceAdapterTraits<DeviceAdapter>::GetName()
|
||||
<< std::endl;
|
||||
|
||||
this->Mul->SetMultiplicand(2);
|
||||
for (int n = 0; n < 2; ++n)
|
||||
{
|
||||
vtkm::cont::Token token;
|
||||
virtual_object_detail::TransformerFunctor tfnctr(
|
||||
this->Handle->PrepareForExecution(device, token));
|
||||
ArrayTransform transformed(*this->Input, tfnctr);
|
||||
|
||||
FloatArrayHandle output;
|
||||
Algorithm::Copy(transformed, output);
|
||||
auto portal = output.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < ARRAY_LEN; ++i)
|
||||
{
|
||||
vtkm::FloatDefault expected =
|
||||
TestValue(i, vtkm::FloatDefault{}) * this->Mul->GetMultiplicand();
|
||||
VTKM_TEST_ASSERT(
|
||||
test_equal(portal.Get(i), expected), "Expected ", expected, " but got ", portal.Get(i));
|
||||
}
|
||||
std::cout << "\tSuccess." << std::endl;
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
std::cout << "\tUpdate and test again..." << std::endl;
|
||||
this->Mul->SetMultiplicand(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const FloatArrayHandle* Input;
|
||||
virtual_object_detail::Multiply* Mul;
|
||||
TransformerHandle* Handle;
|
||||
};
|
||||
|
||||
public:
|
||||
static void Run()
|
||||
{
|
||||
vtkm::cont::ArrayHandle<vtkm::FloatDefault> input;
|
||||
input.Allocate(ARRAY_LEN);
|
||||
SetPortal(input.WritePortal());
|
||||
|
||||
TransformerHandle handle;
|
||||
|
||||
std::cout << "Testing with concrete type 1 (Square)..." << std::endl;
|
||||
virtual_object_detail::Square sqr;
|
||||
handle.Reset(&sqr, false, DeviceAdapterList());
|
||||
vtkm::ListForEach(TestStage1(input, handle), DeviceAdapterList());
|
||||
|
||||
std::cout << "ReleaseResources..." << std::endl;
|
||||
handle.ReleaseResources();
|
||||
|
||||
std::cout << "Testing with concrete type 2 (Multiply)..." << std::endl;
|
||||
virtual_object_detail::Multiply mul;
|
||||
handle.Reset(&mul, false, DeviceAdapterList());
|
||||
vtkm::ListForEach(TestStage2(input, mul, handle), DeviceAdapterList());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
} // vtkm::cont::testing
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_END
|
||||
|
||||
#endif
|
215
vtkm/cont/testing/UnitTestVirtualObjectHandle.cxx
Normal file
215
vtkm/cont/testing/UnitTestVirtualObjectHandle.cxx
Normal file
@ -0,0 +1,215 @@
|
||||
//============================================================================
|
||||
// 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 <vtkm/Types.h>
|
||||
#include <vtkm/cont/ArrayHandle.h>
|
||||
#include <vtkm/cont/ArrayHandleTransform.h>
|
||||
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
|
||||
#include <vtkm/cont/TryExecute.h>
|
||||
#include <vtkm/cont/VirtualObjectHandle.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
#ifdef VTKM_NO_DEPRECATED_VIRTUAL
|
||||
#error "This test should be disabled if the VTKm_NO_DEPRECATED_VIRTUAL is true."
|
||||
#endif //VTKM_NO_DEPRECATED_VIRTUAL
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_BEGIN
|
||||
|
||||
#define ARRAY_LEN 8
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class Transformer : public vtkm::VirtualObjectBase
|
||||
{
|
||||
public:
|
||||
VTKM_EXEC
|
||||
virtual vtkm::FloatDefault Eval(vtkm::FloatDefault val) const = 0;
|
||||
};
|
||||
|
||||
class Square : public Transformer
|
||||
{
|
||||
public:
|
||||
VTKM_EXEC
|
||||
vtkm::FloatDefault Eval(vtkm::FloatDefault val) const override { return val * val; }
|
||||
};
|
||||
|
||||
class Multiply : public Transformer
|
||||
{
|
||||
public:
|
||||
VTKM_CONT
|
||||
void SetMultiplicand(vtkm::FloatDefault val)
|
||||
{
|
||||
this->Multiplicand = val;
|
||||
this->Modified();
|
||||
}
|
||||
|
||||
VTKM_CONT
|
||||
vtkm::FloatDefault GetMultiplicand() const { return this->Multiplicand; }
|
||||
|
||||
VTKM_EXEC
|
||||
vtkm::FloatDefault Eval(vtkm::FloatDefault val) const override
|
||||
{
|
||||
return val * this->Multiplicand;
|
||||
}
|
||||
|
||||
private:
|
||||
vtkm::FloatDefault Multiplicand = 0.0f;
|
||||
};
|
||||
|
||||
class TransformerFunctor
|
||||
{
|
||||
public:
|
||||
TransformerFunctor() = default;
|
||||
explicit TransformerFunctor(const Transformer* impl)
|
||||
: Impl(impl)
|
||||
{
|
||||
}
|
||||
|
||||
VTKM_EXEC
|
||||
vtkm::FloatDefault operator()(vtkm::FloatDefault val) const { return this->Impl->Eval(val); }
|
||||
|
||||
private:
|
||||
const Transformer* Impl;
|
||||
};
|
||||
|
||||
using FloatArrayHandle = vtkm::cont::ArrayHandle<vtkm::FloatDefault>;
|
||||
using ArrayTransform = vtkm::cont::ArrayHandleTransform<FloatArrayHandle, TransformerFunctor>;
|
||||
using TransformerHandle = vtkm::cont::VirtualObjectHandle<Transformer>;
|
||||
|
||||
class TestStage1
|
||||
{
|
||||
public:
|
||||
TestStage1(const FloatArrayHandle& input, TransformerHandle& handle)
|
||||
: Input(&input)
|
||||
, Handle(&handle)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename DeviceAdapter>
|
||||
bool operator()(DeviceAdapter device) const
|
||||
{
|
||||
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
|
||||
std::cout << "\tDeviceAdapter: " << vtkm::cont::DeviceAdapterTraits<DeviceAdapter>::GetName()
|
||||
<< std::endl;
|
||||
|
||||
for (int n = 0; n < 2; ++n)
|
||||
{
|
||||
vtkm::cont::Token token;
|
||||
TransformerFunctor tfnctr(this->Handle->PrepareForExecution(device, token));
|
||||
ArrayTransform transformed(*this->Input, tfnctr);
|
||||
|
||||
FloatArrayHandle output;
|
||||
Algorithm::Copy(transformed, output);
|
||||
auto portal = output.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < ARRAY_LEN; ++i)
|
||||
{
|
||||
vtkm::FloatDefault expected = TestValue(i, vtkm::FloatDefault{});
|
||||
expected = expected * expected;
|
||||
VTKM_TEST_ASSERT(
|
||||
test_equal(portal.Get(i), expected), "Expected ", expected, " but got ", portal.Get(i));
|
||||
}
|
||||
std::cout << "\tSuccess." << std::endl;
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
std::cout << "\tReleaseResources and test again..." << std::endl;
|
||||
this->Handle->ReleaseExecutionResources();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
const FloatArrayHandle* Input;
|
||||
TransformerHandle* Handle;
|
||||
};
|
||||
|
||||
class TestStage2
|
||||
{
|
||||
public:
|
||||
TestStage2(const FloatArrayHandle& input, Multiply& mul, TransformerHandle& handle)
|
||||
: Input(&input)
|
||||
, Mul(&mul)
|
||||
, Handle(&handle)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename DeviceAdapter>
|
||||
bool operator()(DeviceAdapter device) const
|
||||
{
|
||||
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
|
||||
std::cout << "\tDeviceAdapter: " << vtkm::cont::DeviceAdapterTraits<DeviceAdapter>::GetName()
|
||||
<< std::endl;
|
||||
|
||||
this->Mul->SetMultiplicand(2);
|
||||
for (int n = 0; n < 2; ++n)
|
||||
{
|
||||
vtkm::cont::Token token;
|
||||
TransformerFunctor tfnctr(this->Handle->PrepareForExecution(device, token));
|
||||
ArrayTransform transformed(*this->Input, tfnctr);
|
||||
|
||||
FloatArrayHandle output;
|
||||
Algorithm::Copy(transformed, output);
|
||||
auto portal = output.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < ARRAY_LEN; ++i)
|
||||
{
|
||||
vtkm::FloatDefault expected =
|
||||
TestValue(i, vtkm::FloatDefault{}) * this->Mul->GetMultiplicand();
|
||||
VTKM_TEST_ASSERT(
|
||||
test_equal(portal.Get(i), expected), "Expected ", expected, " but got ", portal.Get(i));
|
||||
}
|
||||
std::cout << "\tSuccess." << std::endl;
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
std::cout << "\tUpdate and test again..." << std::endl;
|
||||
this->Mul->SetMultiplicand(3);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
const FloatArrayHandle* Input;
|
||||
Multiply* Mul;
|
||||
TransformerHandle* Handle;
|
||||
};
|
||||
|
||||
void Run()
|
||||
{
|
||||
vtkm::cont::ArrayHandle<vtkm::FloatDefault> input;
|
||||
input.Allocate(ARRAY_LEN);
|
||||
SetPortal(input.WritePortal());
|
||||
|
||||
TransformerHandle handle;
|
||||
|
||||
std::cout << "Testing with concrete type 1 (Square)..." << std::endl;
|
||||
Square sqr;
|
||||
handle.Reset(&sqr, false);
|
||||
vtkm::cont::TryExecute(TestStage1(input, handle));
|
||||
|
||||
std::cout << "ReleaseResources..." << std::endl;
|
||||
handle.ReleaseResources();
|
||||
|
||||
std::cout << "Testing with concrete type 2 (Multiply)..." << std::endl;
|
||||
Multiply mul;
|
||||
handle.Reset(&mul, false);
|
||||
vtkm::cont::TryExecute(TestStage2(input, mul, handle));
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_END
|
||||
|
||||
int UnitTestVirtualObjectHandle(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(Run, argc, argv);
|
||||
}
|
Loading…
Reference in New Issue
Block a user