2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2015-04-24 00:45:24 +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-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandlePermutation.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
|
|
|
#include <vtkm/cont/ArrayHandleImplicit.h>
|
2022-07-06 19:36:00 +00:00
|
|
|
#include <vtkm/cont/Invoker.h>
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
#include <vtkm/worklet/WorkletMapField.h>
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace
|
|
|
|
{
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
const vtkm::Id ARRAY_SIZE = 10;
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
struct DoubleIndexFunctor
|
|
|
|
{
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Id operator()(vtkm::Id index) const { return 2 * index; }
|
2015-04-24 00:45:24 +00:00
|
|
|
};
|
|
|
|
|
2017-06-23 18:50:34 +00:00
|
|
|
using DoubleIndexArrayType = vtkm::cont::ArrayHandleImplicit<DoubleIndexFunctor>;
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
struct CheckPermutationWorklet : vtkm::worklet::WorkletMapField
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2022-07-06 19:36:00 +00:00
|
|
|
using ControlSignature = void(FieldIn permutationArray);
|
|
|
|
using ExecutionSignature = void(WorkIndex, _1);
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
template <typename T>
|
|
|
|
VTKM_EXEC void operator()(vtkm::Id index, const T& value) const
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
vtkm::Id permutedIndex = 2 * index;
|
2015-04-24 00:45:24 +00:00
|
|
|
T expectedValue = TestValue(permutedIndex, T());
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
if (!test_equal(value, expectedValue))
|
|
|
|
{
|
2015-04-24 00:45:24 +00:00
|
|
|
this->RaiseError("Encountered bad transformed value.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
struct InPlacePermutationWorklet : vtkm::worklet::WorkletMapField
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2022-07-06 19:36:00 +00:00
|
|
|
using ControlSignature = void(FieldInOut permutationArray);
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
template <typename T>
|
|
|
|
VTKM_EXEC void operator()(T& value) const
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2015-04-24 00:45:24 +00:00
|
|
|
value = value + T(1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PortalType>
|
|
|
|
VTKM_CONT void CheckInPlaceResult(PortalType portal)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using T = typename PortalType::ValueType;
|
2017-05-18 14:29:41 +00:00
|
|
|
for (vtkm::Id permutedIndex = 0; permutedIndex < 2 * ARRAY_SIZE; permutedIndex++)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
if (permutedIndex % 2 == 0)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
|
|
|
// This index was part of the permuted array; has a value changed
|
|
|
|
T expectedValue = TestValue(permutedIndex, T()) + T(1000);
|
2017-05-18 14:29:41 +00:00
|
|
|
T retrievedValue = portal.Get(permutedIndex);
|
|
|
|
VTKM_TEST_ASSERT(test_equal(expectedValue, retrievedValue), "Permuted set unexpected value.");
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This index was not part of the permuted array; has original value
|
|
|
|
T expectedValue = TestValue(permutedIndex, T());
|
2017-05-18 14:29:41 +00:00
|
|
|
T retrievedValue = portal.Get(permutedIndex);
|
2015-05-07 19:22:10 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(expectedValue, retrievedValue),
|
2015-04-24 00:45:24 +00:00
|
|
|
"Permuted array modified value it should not have.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
struct OutputPermutationWorklet : vtkm::worklet::WorkletMapField
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2022-07-06 19:36:00 +00:00
|
|
|
// Note: Using a FieldOut for the input domain is rare (and mostly discouraged),
|
|
|
|
// but it works as long as the array is allocated to the size desired.
|
|
|
|
using ControlSignature = void(FieldOut permutationArray);
|
|
|
|
using ExecutionSignature = void(WorkIndex, _1);
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
template <typename T>
|
|
|
|
VTKM_EXEC void operator()(vtkm::Id index, T& value) const
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2022-07-06 19:36:00 +00:00
|
|
|
value = TestValue(static_cast<vtkm::Id>(index), T());
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PortalType>
|
|
|
|
VTKM_CONT void CheckOutputResult(PortalType portal)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using T = typename PortalType::ValueType;
|
2017-05-18 14:29:41 +00:00
|
|
|
for (vtkm::IdComponent permutedIndex = 0; permutedIndex < 2 * ARRAY_SIZE; permutedIndex++)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
if (permutedIndex % 2 == 0)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
|
|
|
// This index was part of the permuted array; has a value changed
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Id originalIndex = permutedIndex / 2;
|
2015-04-24 00:45:24 +00:00
|
|
|
T expectedValue = TestValue(originalIndex, T());
|
2017-05-18 14:29:41 +00:00
|
|
|
T retrievedValue = portal.Get(permutedIndex);
|
|
|
|
VTKM_TEST_ASSERT(test_equal(expectedValue, retrievedValue), "Permuted set unexpected value.");
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This index was not part of the permuted array; has original value
|
|
|
|
T expectedValue = TestValue(permutedIndex, T());
|
2017-05-18 14:29:41 +00:00
|
|
|
T retrievedValue = portal.Get(permutedIndex);
|
2015-05-07 19:22:10 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(expectedValue, retrievedValue),
|
2015-04-24 00:45:24 +00:00
|
|
|
"Permuted array modified value it should not have.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename ValueType>
|
2015-04-24 00:45:24 +00:00
|
|
|
struct PermutationTests
|
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using IndexArrayType = vtkm::cont::ArrayHandleImplicit<DoubleIndexFunctor>;
|
2017-08-16 15:34:21 +00:00
|
|
|
using ValueArrayType = vtkm::cont::ArrayHandle<ValueType, vtkm::cont::StorageTagBasic>;
|
|
|
|
using PermutationArrayType = vtkm::cont::ArrayHandlePermutation<IndexArrayType, ValueArrayType>;
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
ValueArrayType MakeValueArray() const
|
|
|
|
{
|
2015-04-24 00:45:24 +00:00
|
|
|
// Allocate a buffer and set initial values
|
2017-05-18 14:29:41 +00:00
|
|
|
std::vector<ValueType> buffer(2 * ARRAY_SIZE);
|
|
|
|
for (vtkm::IdComponent index = 0; index < 2 * ARRAY_SIZE; index++)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2015-05-26 14:49:52 +00:00
|
|
|
vtkm::UInt32 i = static_cast<vtkm::UInt32>(index);
|
|
|
|
buffer[i] = TestValue(index, ValueType());
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an ArrayHandle from the buffer
|
2020-07-16 16:32:32 +00:00
|
|
|
return vtkm::cont::make_ArrayHandle(buffer, vtkm::CopyFlag::On);
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()() const
|
|
|
|
{
|
|
|
|
std::cout << "Create ArrayHandlePermutation" << std::endl;
|
|
|
|
IndexArrayType indexArray(DoubleIndexFunctor(), ARRAY_SIZE);
|
|
|
|
|
|
|
|
ValueArrayType valueArray = this->MakeValueArray();
|
|
|
|
|
|
|
|
PermutationArrayType permutationArray(indexArray, valueArray);
|
|
|
|
|
|
|
|
VTKM_TEST_ASSERT(permutationArray.GetNumberOfValues() == ARRAY_SIZE,
|
|
|
|
"Permutation array wrong size.");
|
2020-01-28 19:14:32 +00:00
|
|
|
VTKM_TEST_ASSERT(permutationArray.WritePortal().GetNumberOfValues() == ARRAY_SIZE,
|
2017-05-18 14:29:41 +00:00
|
|
|
"Permutation portal wrong size.");
|
2020-01-28 19:14:32 +00:00
|
|
|
VTKM_TEST_ASSERT(permutationArray.ReadPortal().GetNumberOfValues() == ARRAY_SIZE,
|
2017-05-18 14:29:41 +00:00
|
|
|
"Permutation portal wrong size.");
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2022-07-06 19:36:00 +00:00
|
|
|
vtkm::cont::Invoker invoke;
|
2020-01-21 20:18:03 +00:00
|
|
|
|
2015-04-24 00:45:24 +00:00
|
|
|
std::cout << "Test initial values in execution environment" << std::endl;
|
2022-07-06 19:36:00 +00:00
|
|
|
invoke(CheckPermutationWorklet{}, permutationArray);
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
std::cout << "Try in place operation" << std::endl;
|
2022-07-06 19:36:00 +00:00
|
|
|
invoke(InPlacePermutationWorklet{}, permutationArray);
|
2020-01-28 19:14:32 +00:00
|
|
|
CheckInPlaceResult(valueArray.WritePortal());
|
|
|
|
CheckInPlaceResult(valueArray.ReadPortal());
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
std::cout << "Try output operation" << std::endl;
|
2022-07-06 19:36:00 +00:00
|
|
|
invoke(OutputPermutationWorklet{}, permutationArray);
|
2020-01-28 19:14:32 +00:00
|
|
|
CheckOutputResult(valueArray.ReadPortal());
|
|
|
|
CheckOutputResult(valueArray.WritePortal());
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TryInputType
|
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename InputType>
|
|
|
|
void operator()(InputType) const
|
|
|
|
{
|
2015-04-24 00:45:24 +00:00
|
|
|
PermutationTests<InputType>()();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestArrayHandlePermutation()
|
|
|
|
{
|
2019-12-05 17:55:57 +00:00
|
|
|
vtkm::testing::Testing::TryTypes(TryInputType(), vtkm::TypeListCommon());
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 20:49:11 +00:00
|
|
|
} // anonymous namespace
|
2015-04-24 00:45:24 +00:00
|
|
|
|
2019-01-01 22:19:02 +00:00
|
|
|
int UnitTestArrayHandlePermutation(int argc, char* argv[])
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2019-01-01 22:19:02 +00:00
|
|
|
return vtkm::cont::testing::Testing::Run(TestArrayHandlePermutation, argc, argv);
|
2015-04-24 00:45:24 +00:00
|
|
|
}
|