vtk-m2/vtkm/cont/testing/UnitTestArrayHandleImplicit.cxx

108 lines
3.2 KiB
C++
Raw Normal View History

2019-04-15 23:24:21 +00:00
//============================================================================
2015-02-13 06:24:43 +00:00
// 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.
2019-04-15 23:24:21 +00:00
//============================================================================
2015-02-13 06:24:43 +00:00
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleImplicit.h>
#include <vtkm/cont/Invoker.h>
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
2015-02-13 06:24:43 +00:00
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/VecTraits.h>
2015-02-13 06:24:43 +00:00
#include <vtkm/cont/testing/Testing.h>
2017-05-18 14:29:41 +00:00
namespace
{
2015-02-13 06:24:43 +00:00
const vtkm::Id ARRAY_SIZE = 10;
2017-05-18 14:29:41 +00:00
template <typename ValueType>
2015-02-13 06:24:43 +00:00
struct IndexSquared
{
VTKM_EXEC_CONT
2015-02-13 06:24:43 +00:00
ValueType operator()(vtkm::Id i) const
{
using ComponentType = typename vtkm::VecTraits<ValueType>::ComponentType;
2017-05-18 14:29:41 +00:00
return ValueType(static_cast<ComponentType>(i * i));
}
2015-02-13 06:24:43 +00:00
};
struct PassThrough : public vtkm::worklet::WorkletMapField
{
using ControlSignature = void(FieldIn, FieldOut);
using ExecutionSignature = void(_1, _2);
template <typename InValue, typename OutValue>
VTKM_EXEC void operator()(const InValue& inValue, OutValue& outValue) const
{
outValue = inValue;
}
};
2015-02-13 06:24:43 +00:00
struct ImplicitTests
{
2017-05-18 14:29:41 +00:00
template <typename ValueType>
2015-02-13 06:24:43 +00:00
void operator()(const ValueType) const
{
using FunctorType = IndexSquared<ValueType>;
2015-02-13 06:24:43 +00:00
FunctorType functor;
using ImplicitHandle = vtkm::cont::ArrayHandleImplicit<FunctorType>;
2015-02-13 06:24:43 +00:00
2020-05-12 19:43:40 +00:00
ImplicitHandle implicit = vtkm::cont::make_ArrayHandleImplicit(functor, ARRAY_SIZE);
2015-02-13 06:24:43 +00:00
std::cout << "verify that the control portal works" << std::endl;
2020-05-12 19:43:40 +00:00
auto implicitPortal = implicit.ReadPortal();
2017-05-18 14:29:41 +00:00
for (int i = 0; i < ARRAY_SIZE; ++i)
{
2020-05-12 19:43:40 +00:00
const ValueType v = implicitPortal.Get(i);
2015-02-13 06:24:43 +00:00
const ValueType correct_value = functor(i);
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(v == correct_value, "Implicit Handle Failed");
}
2015-02-13 06:24:43 +00:00
std::cout << "verify that the execution portal works" << std::endl;
vtkm::cont::Token token;
using Device = vtkm::cont::DeviceAdapterTagSerial;
using CEPortal = typename ImplicitHandle::ReadPortalType;
2020-05-12 19:43:40 +00:00
CEPortal execPortal = implicit.PrepareForInput(Device(), token);
2017-05-18 14:29:41 +00:00
for (int i = 0; i < ARRAY_SIZE; ++i)
{
2015-02-13 06:24:43 +00:00
const ValueType v = execPortal.Get(i);
const ValueType correct_value = functor(i);
VTKM_TEST_ASSERT(v == correct_value, "Implicit Handle Failed");
2017-05-18 14:29:41 +00:00
}
std::cout << "verify that the array handle works in a worklet on the device" << std::endl;
vtkm::cont::Invoker invoke;
vtkm::cont::ArrayHandle<ValueType> 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));
}
2015-02-13 06:24:43 +00:00
}
};
void TestArrayHandleImplicit()
{
vtkm::testing::Testing::TryTypes(ImplicitTests(), vtkm::TypeListCommon());
2015-02-13 06:24:43 +00:00
}
} // anonymous namespace
2015-02-13 06:24:43 +00:00
int UnitTestArrayHandleImplicit(int argc, char* argv[])
2015-02-13 06:24:43 +00:00
{
return vtkm::cont::testing::Testing::Run(TestArrayHandleImplicit, argc, argv);
2015-02-13 06:24:43 +00:00
}