vtk-m2/vtkm/cont/testing/TestingVirtualObjectHandle.h

233 lines
6.3 KiB
C
Raw Normal View History

2017-03-29 14:48:43 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
2017-03-29 14:48:43 +00:00
// 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.
//============================================================================
2017-10-23 13:38:33 +00:00
#ifndef vtk_m_cont_testing_TestingVirtualObjectHandle_h
#define vtk_m_cont_testing_TestingVirtualObjectHandle_h
2017-03-29 14:48:43 +00:00
2017-05-18 14:51:24 +00:00
#include <vtkm/Types.h>
2017-03-29 14:48:43 +00:00
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleTransform.h>
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
2017-10-23 13:38:33 +00:00
#include <vtkm/cont/VirtualObjectHandle.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/testing/Testing.h>
2017-03-29 14:48:43 +00:00
#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
2017-03-29 14:48:43 +00:00
#define ARRAY_LEN 8
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
namespace testing
{
2017-03-29 14:48:43 +00:00
namespace virtual_object_detail
{
2017-10-23 13:38:33 +00:00
class Transformer : public vtkm::VirtualObjectBase
2017-03-29 14:48:43 +00:00
{
public:
2017-10-23 13:38:33 +00:00
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)
2017-03-29 14:48:43 +00:00
{
2017-10-23 13:38:33 +00:00
this->Multiplicand = val;
this->Modified();
2017-03-29 14:48:43 +00:00
}
2017-10-23 13:38:33 +00:00
VTKM_CONT
vtkm::FloatDefault GetMultiplicand() const { return this->Multiplicand; }
2017-03-29 14:48:43 +00:00
VTKM_EXEC
2017-10-23 13:38:33 +00:00
vtkm::FloatDefault Eval(vtkm::FloatDefault val) const override
2017-03-29 14:48:43 +00:00
{
2017-10-23 13:38:33 +00:00
return val * this->Multiplicand;
2017-03-29 14:48:43 +00:00
}
private:
2017-10-23 13:38:33 +00:00
vtkm::FloatDefault Multiplicand = 0.0f;
2017-03-29 14:48:43 +00:00
};
2017-10-23 13:38:33 +00:00
class TransformerFunctor
2017-03-29 14:48:43 +00:00
{
2017-10-23 13:38:33 +00:00
public:
TransformerFunctor() = default;
explicit TransformerFunctor(const Transformer* impl)
: Impl(impl)
{
}
2017-03-29 14:48:43 +00:00
VTKM_EXEC
2017-10-23 13:38:33 +00:00
vtkm::FloatDefault operator()(vtkm::FloatDefault val) const { return this->Impl->Eval(val); }
2017-03-29 14:48:43 +00:00
2017-10-23 13:38:33 +00:00
private:
const Transformer* Impl;
2017-03-29 14:48:43 +00:00
};
} // virtual_object_detail
2017-05-18 14:29:41 +00:00
template <typename DeviceAdapterList>
2017-10-23 13:38:33 +00:00
class TestingVirtualObjectHandle
2017-03-29 14:48:43 +00:00
{
private:
using FloatArrayHandle = vtkm::cont::ArrayHandle<vtkm::FloatDefault>;
2017-05-30 14:00:01 +00:00
using ArrayTransform =
2017-10-23 13:38:33 +00:00
vtkm::cont::ArrayHandleTransform<FloatArrayHandle, virtual_object_detail::TransformerFunctor>;
using TransformerHandle = vtkm::cont::VirtualObjectHandle<virtual_object_detail::Transformer>;
2017-03-29 14:48:43 +00:00
class TestStage1
{
public:
2017-10-23 13:38:33 +00:00
TestStage1(const FloatArrayHandle& input, TransformerHandle& handle)
2017-05-18 14:29:41 +00:00
: Input(&input)
2017-10-23 13:38:33 +00:00
, Handle(&handle)
2017-05-18 14:29:41 +00:00
{
}
2017-03-29 14:48:43 +00:00
2017-05-18 14:29:41 +00:00
template <typename DeviceAdapter>
2017-03-29 14:48:43 +00:00
void operator()(DeviceAdapter device) const
{
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
2017-05-18 14:29:41 +00:00
std::cout << "\tDeviceAdapter: " << vtkm::cont::DeviceAdapterTraits<DeviceAdapter>::GetName()
2017-03-29 14:48:43 +00:00
<< std::endl;
for (int n = 0; n < 2; ++n)
{
vtkm::cont::Token token;
virtual_object_detail::TransformerFunctor tfnctr(
this->Handle->PrepareForExecution(device, token));
2017-10-23 13:38:33 +00:00
ArrayTransform transformed(*this->Input, tfnctr);
2017-03-29 14:48:43 +00:00
FloatArrayHandle output;
Algorithm::Copy(transformed, output);
auto portal = output.ReadPortal();
2017-03-29 14:48:43 +00:00
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));
2017-03-29 14:48:43 +00:00
}
std::cout << "\tSuccess." << std::endl;
if (n == 0)
{
std::cout << "\tReleaseResources and test again..." << std::endl;
2017-10-23 13:38:33 +00:00
this->Handle->ReleaseExecutionResources();
2017-03-29 14:48:43 +00:00
}
}
}
private:
2017-05-18 14:29:41 +00:00
const FloatArrayHandle* Input;
2017-10-23 13:38:33 +00:00
TransformerHandle* Handle;
2017-03-29 14:48:43 +00:00
};
class TestStage2
{
public:
TestStage2(const FloatArrayHandle& input,
virtual_object_detail::Multiply& mul,
2017-10-23 13:38:33 +00:00
TransformerHandle& handle)
2017-05-18 14:29:41 +00:00
: Input(&input)
, Mul(&mul)
2017-10-23 13:38:33 +00:00
, Handle(&handle)
2017-05-18 14:29:41 +00:00
{
}
2017-03-29 14:48:43 +00:00
2017-05-18 14:29:41 +00:00
template <typename DeviceAdapter>
2017-03-29 14:48:43 +00:00
void operator()(DeviceAdapter device) const
{
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
2017-05-18 14:29:41 +00:00
std::cout << "\tDeviceAdapter: " << vtkm::cont::DeviceAdapterTraits<DeviceAdapter>::GetName()
2017-03-29 14:48:43 +00:00
<< std::endl;
2017-10-23 13:38:33 +00:00
this->Mul->SetMultiplicand(2);
2017-03-29 14:48:43 +00:00
for (int n = 0; n < 2; ++n)
{
vtkm::cont::Token token;
virtual_object_detail::TransformerFunctor tfnctr(
this->Handle->PrepareForExecution(device, token));
2017-10-23 13:38:33 +00:00
ArrayTransform transformed(*this->Input, tfnctr);
2017-03-29 14:48:43 +00:00
FloatArrayHandle output;
Algorithm::Copy(transformed, output);
auto portal = output.ReadPortal();
2017-03-29 14:48:43 +00:00
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));
2017-03-29 14:48:43 +00:00
}
std::cout << "\tSuccess." << std::endl;
if (n == 0)
{
std::cout << "\tUpdate and test again..." << std::endl;
2017-10-23 13:38:33 +00:00
this->Mul->SetMultiplicand(3);
2017-03-29 14:48:43 +00:00
}
}
}
private:
2017-05-18 14:29:41 +00:00
const FloatArrayHandle* Input;
virtual_object_detail::Multiply* Mul;
2017-10-23 13:38:33 +00:00
TransformerHandle* Handle;
2017-03-29 14:48:43 +00:00
};
public:
static void Run()
{
vtkm::cont::ArrayHandle<vtkm::FloatDefault> input;
input.Allocate(ARRAY_LEN);
SetPortal(input.WritePortal());
2017-03-29 14:48:43 +00:00
2017-10-23 13:38:33 +00:00
TransformerHandle handle;
2017-03-29 14:48:43 +00:00
std::cout << "Testing with concrete type 1 (Square)..." << std::endl;
virtual_object_detail::Square sqr;
2017-10-23 13:38:33 +00:00
handle.Reset(&sqr, false, DeviceAdapterList());
vtkm::ListForEach(TestStage1(input, handle), DeviceAdapterList());
2017-03-29 14:48:43 +00:00
std::cout << "ReleaseResources..." << std::endl;
handle.ReleaseResources();
2017-03-29 14:48:43 +00:00
std::cout << "Testing with concrete type 2 (Multiply)..." << std::endl;
virtual_object_detail::Multiply mul;
2017-10-23 13:38:33 +00:00
handle.Reset(&mul, false, DeviceAdapterList());
vtkm::ListForEach(TestStage2(input, mul, handle), DeviceAdapterList());
2017-03-29 14:48:43 +00:00
}
};
}
}
} // vtkm::cont::testing
VTKM_DEPRECATED_SUPPRESS_END
2017-03-29 14:48:43 +00:00
#endif