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.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2015-04-24 00:45:24 +00:00
|
|
|
// Copyright 2015 UT-Battelle, LLC.
|
2015-05-21 12:09:22 +00:00
|
|
|
// Copyright 2015 Los Alamos National Security.
|
2015-04-24 00:45:24 +00:00
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2015-04-24 00:45:24 +00:00
|
|
|
// the U.S. Government retains certain rights in this software.
|
|
|
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
|
|
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
|
|
|
// this software.
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandlePermutation.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
|
|
|
#include <vtkm/cont/ArrayHandleImplicit.h>
|
|
|
|
#include <vtkm/cont/DeviceAdapter.h>
|
|
|
|
#include <vtkm/cont/StorageBasic.h>
|
|
|
|
|
|
|
|
#include <vtkm/exec/FunctorBase.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PermutedPortalType>
|
2015-04-24 00:45:24 +00:00
|
|
|
struct CheckPermutationFunctor : vtkm::exec::FunctorBase
|
|
|
|
{
|
|
|
|
PermutedPortalType PermutedPortal;
|
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC
|
2017-05-18 14:29:41 +00:00
|
|
|
void operator()(vtkm::Id index) const
|
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using T = typename PermutedPortalType::ValueType;
|
2015-04-24 00:45:24 +00:00
|
|
|
T value = this->PermutedPortal.Get(index);
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PermutedArrayHandleType, typename Device>
|
|
|
|
VTKM_CONT CheckPermutationFunctor<
|
|
|
|
typename PermutedArrayHandleType::template ExecutionTypes<Device>::PortalConst>
|
|
|
|
make_CheckPermutationFunctor(const PermutedArrayHandleType& permutedArray, Device)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using PermutedPortalType =
|
|
|
|
typename PermutedArrayHandleType::template ExecutionTypes<Device>::PortalConst;
|
2015-04-24 00:45:24 +00:00
|
|
|
CheckPermutationFunctor<PermutedPortalType> functor;
|
|
|
|
functor.PermutedPortal = permutedArray.PrepareForInput(Device());
|
|
|
|
return functor;
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PermutedPortalType>
|
2015-04-24 00:45:24 +00:00
|
|
|
struct InPlacePermutationFunctor : vtkm::exec::FunctorBase
|
|
|
|
{
|
|
|
|
PermutedPortalType PermutedPortal;
|
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC
|
2017-05-18 14:29:41 +00:00
|
|
|
void operator()(vtkm::Id index) const
|
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using T = typename PermutedPortalType::ValueType;
|
2015-04-24 00:45:24 +00:00
|
|
|
T value = this->PermutedPortal.Get(index);
|
|
|
|
|
|
|
|
value = value + T(1000);
|
|
|
|
|
|
|
|
this->PermutedPortal.Set(index, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PermutedArrayHandleType, typename Device>
|
|
|
|
VTKM_CONT InPlacePermutationFunctor<
|
|
|
|
typename PermutedArrayHandleType::template ExecutionTypes<Device>::Portal>
|
|
|
|
make_InPlacePermutationFunctor(PermutedArrayHandleType& permutedArray, Device)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using PermutedPortalType =
|
|
|
|
typename PermutedArrayHandleType::template ExecutionTypes<Device>::Portal;
|
2015-04-24 00:45:24 +00:00
|
|
|
InPlacePermutationFunctor<PermutedPortalType> functor;
|
|
|
|
functor.PermutedPortal = permutedArray.PrepareForInPlace(Device());
|
|
|
|
return functor;
|
|
|
|
}
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename PermutedPortalType>
|
2015-04-24 00:45:24 +00:00
|
|
|
struct OutputPermutationFunctor : vtkm::exec::FunctorBase
|
|
|
|
{
|
|
|
|
PermutedPortalType PermutedPortal;
|
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC
|
2017-05-18 14:29:41 +00:00
|
|
|
void operator()(vtkm::Id index) const
|
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using T = typename PermutedPortalType::ValueType;
|
2015-05-26 14:49:52 +00:00
|
|
|
this->PermutedPortal.Set(index, 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 PermutedArrayHandleType, typename Device>
|
|
|
|
VTKM_CONT OutputPermutationFunctor<
|
|
|
|
typename PermutedArrayHandleType::template ExecutionTypes<Device>::Portal>
|
|
|
|
make_OutputPermutationFunctor(PermutedArrayHandleType& permutedArray, Device)
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
2017-06-23 18:50:34 +00:00
|
|
|
using PermutedPortalType =
|
|
|
|
typename PermutedArrayHandleType::template ExecutionTypes<Device>::Portal;
|
2015-04-24 00:45:24 +00:00
|
|
|
OutputPermutationFunctor<PermutedPortalType> functor;
|
|
|
|
functor.PermutedPortal = permutedArray.PrepareForOutput(ARRAY_SIZE, Device());
|
|
|
|
return functor;
|
|
|
|
}
|
|
|
|
|
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-06-23 18:50:34 +00:00
|
|
|
using Device = VTKM_DEFAULT_DEVICE_ADAPTER_TAG;
|
|
|
|
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<Device>;
|
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
|
2015-05-01 04:01:02 +00:00
|
|
|
ValueArrayType array = vtkm::cont::make_ArrayHandle(buffer);
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
// Copy the array so that the data is not destroyed when we return from
|
|
|
|
// this method.
|
|
|
|
ValueArrayType arrayCopy;
|
|
|
|
Algorithm::Copy(array, arrayCopy);
|
|
|
|
|
|
|
|
return arrayCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.");
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(permutationArray.GetPortalControl().GetNumberOfValues() == ARRAY_SIZE,
|
|
|
|
"Permutation portal wrong size.");
|
|
|
|
VTKM_TEST_ASSERT(permutationArray.GetPortalConstControl().GetNumberOfValues() == ARRAY_SIZE,
|
|
|
|
"Permutation portal wrong size.");
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
std::cout << "Test initial values in execution environment" << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
Algorithm::Schedule(make_CheckPermutationFunctor(permutationArray, Device()), ARRAY_SIZE);
|
2015-04-24 00:45:24 +00:00
|
|
|
|
|
|
|
std::cout << "Try in place operation" << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
Algorithm::Schedule(make_InPlacePermutationFunctor(permutationArray, Device()), ARRAY_SIZE);
|
2015-04-24 00:45:24 +00:00
|
|
|
CheckInPlaceResult(valueArray.GetPortalControl());
|
|
|
|
CheckInPlaceResult(valueArray.GetPortalConstControl());
|
|
|
|
|
|
|
|
std::cout << "Try output operation" << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
Algorithm::Schedule(make_OutputPermutationFunctor(permutationArray, Device()), ARRAY_SIZE);
|
2015-04-24 00:45:24 +00:00
|
|
|
CheckOutputResult(valueArray.GetPortalConstControl());
|
|
|
|
CheckOutputResult(valueArray.GetPortalControl());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
vtkm::testing::Testing::TryTypes(TryInputType(), vtkm::TypeListTagCommon());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // annonymous namespace
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
int UnitTestArrayHandlePermutation(int, char* [])
|
2015-04-24 00:45:24 +00:00
|
|
|
{
|
|
|
|
return vtkm::cont::testing::Testing::Run(TestArrayHandlePermutation);
|
|
|
|
}
|