Add ArrayHandleTransform.

This commit is contained in:
Kenneth Moreland 2015-04-13 14:43:18 -06:00
parent e6d9e120eb
commit f7daaf36a8
4 changed files with 502 additions and 0 deletions

@ -0,0 +1,322 @@
//=============================================================================
//
// 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.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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.
//
//=============================================================================
#ifndef vtk_m_cont_ArrayHandleTransform_h
#define vtk_m_cont_ArrayHandleTransform_h
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/Assert.h>
#include <vtkm/cont/ErrorControlBadType.h>
#include <vtkm/cont/ErrorControlInternal.h>
namespace vtkm {
namespace cont {
namespace internal {
/// \brief An array portal that transforms a value from another portal.
///
template<typename ValueType_, typename PortalType_, typename FunctorType_>
class ArrayPortalTransform
{
public:
typedef PortalType_ PortalType;
typedef ValueType_ ValueType;
typedef FunctorType_ FunctorType;
VTKM_CONT_EXPORT
ArrayPortalTransform(const PortalType &portal = PortalType(),
vtkm::Id size = 0,
const FunctorType &functor = FunctorType())
: Portal(portal), NumberOfValues(size), Functor(functor)
{ }
/// Copy constructor for any other ArrayPortalTransform with an iterator
/// type that can be copied to this iterator type. This allows us to do any
/// type casting that the iterators do (like the non-const to const cast).
///
template<class OtherV, class OtherP, class OtherF>
VTKM_CONT_EXPORT
ArrayPortalTransform(const ArrayPortalTransform<OtherV,OtherP,OtherF> &src)
: Portal(src.GetPortal()),
NumberOfValues(src.GetNumberOfValues()),
Functor(src.GetFunctor())
{ }
VTKM_EXEC_CONT_EXPORT
vtkm::Id GetNumberOfValues() const { return this->GetNumberOfValues(); }
VTKM_EXEC_EXPORT
ValueType Get(vtkm::Id index) const {
return this->Functor(this->Portal.Get(index));
}
VTKM_EXEC_CONT_EXPORT
const PortalType &GetPortal() const { return this->Portal; }
VTKM_EXEC_CONT_EXPORT
const FunctorType &GetFunctor() const { return this->Functor; }
private:
PortalType Portal;
vtkm::Id NumberOfValues;
FunctorType Functor;
};
template<typename ValueType, typename ArrayHandleType, typename FunctorType>
struct StorageTagTransform;
template<typename T, typename ArrayHandleType, typename FunctorType>
class Storage<T, StorageTagTransform<T, ArrayHandleType, FunctorType > >
{
public:
typedef T ValueType;
typedef ArrayPortalTransform<
ValueType, typename ArrayHandleType::PortalControl, FunctorType>
PortalType;
typedef ArrayPortalTransform<
ValueType, typename ArrayHandleType::PortalConstControl, FunctorType>
PortalConstType;
VTKM_CONT_EXPORT
Storage() : Valid(false) { }
VTKM_CONT_EXPORT
Storage(const ArrayHandleType &array,
const FunctorType &functor = FunctorType())
: Array(array), Functor(functor), Valid(true) { }
VTKM_CONT_EXPORT
PortalType GetPortal() {
VTKM_ASSERT_CONT(this->Valid);
return PortalType(this->Array.GetPortalControl(),
this->GetNumberOfValues(),
this->Functor);
}
VTKM_CONT_EXPORT
PortalType GetPortalConst() const {
VTKM_ASSERT_CONT(this->Valid);
return PortalConstType(this->Array.GetPortalConstControl(),
this->GetNumberOfValues(),
this->Functor);
}
VTKM_CONT_EXPORT
vtkm::Id GetNumberOfValues() const {
VTKM_ASSERT_CONT(this->Valid);
return this->Array.GetNumberOfValues();
}
VTKM_CONT_EXPORT
void Allocate(vtkm::Id vtkmNotUsed(numberOfValues)) {
throw vtkm::cont::ErrorControlBadType(
"ArrayHandleTransform is read only. It cannot be allocated.");
}
VTKM_CONT_EXPORT
void Shrink(vtkm::Id vtkmNotUsed(numberOfValues)) {
throw vtkm::cont::ErrorControlBadType(
"ArrayHandleTransform is read only. It cannot shrink.");
}
VTKM_CONT_EXPORT
void ReleaseResources() {
// This request is ignored since it is asking to release the resources
// of the delegate array, which may be used elsewhere. Should the behavior
// be different?
}
VTKM_CONT_EXPORT
const ArrayHandleType &GetArray() const {
VTKM_ASSERT_CONT(this->Valid);
return this->Array;
}
private:
ArrayHandleType Array;
FunctorType Functor;
bool Valid;
};
template<typename T,
typename ArrayHandleType,
typename FunctorType,
typename Device>
class ArrayTransfer<
T, StorageTagTransform<T,ArrayHandleType,FunctorType>, Device>
{
typedef StorageTagTransform<T,ArrayHandleType,FunctorType> StorageTag;
typedef vtkm::cont::internal::Storage<T, StorageTag> StorageType;
public:
typedef T ValueType;
typedef typename StorageType::PortalType PortalControl;
typedef typename StorageType::PortalConstType PortalConstControl;
typedef ArrayPortalTransform<
ValueType,
typename ArrayHandleType::template ExecutionTypes<Device>::Portal,
FunctorType> PortalExecution;
typedef ArrayPortalTransform<
ValueType,
typename ArrayHandleType::template ExecutionTypes<Device>::PortalConst,
FunctorType> PortalConstExecution;
VTKM_CONT_EXPORT
ArrayTransfer() : ArrayValid(false), ExecutionPortalConstValid(false) { }
VTKM_CONT_EXPORT
vtkm::Id GetNumberOfValues() const {
VTKM_ASSERT_CONT(this->ArrayValid);
return this->Array.GetNumberOfValues();
}
VTKM_CONT_EXPORT
void LoadDataForInput(PortalConstControl vtkmNotUsed(portal)) {
throw vtkm::cont::ErrorControlInternal(
"Wrong version of LoadDataForInput called. "
"ArrayHandle must be in a bad state.");
}
VTKM_CONT_EXPORT
void LoadDataForInput(const StorageType &storage) {
this->Array = storage.GetArray();
this->ArrayValid = true;
this->ExecutionPortalConst =
PortalConstExecution(this->Array.PrepareForInput(Device()));
this->ExecutionPortalConstValid = true;
}
VTKM_CONT_EXPORT
void LoadDataForInPlace(StorageType &vtkmNotUsed(storage)) {
throw vtkm::cont::ErrorControlBadType(
"ArrayHandleTransform read only. "
"Cannot be used for in-place operations.");
}
VTKM_CONT_EXPORT
void AllocateArrayForOutput(StorageType &vtkmNotUsed(storage),
vtkm::Id vtkmNotUsed(numberOfValues)) {
throw vtkm::cont::ErrorControlBadType(
"ArrayHandleTransform read only. Cannot be used as output.");
}
VTKM_CONT_EXPORT
void RetrieveOutputData(StorageType &vtkmNotUsed(storage)) const {
throw vtkm::cont::ErrorControlInternal(
"ArrayHandleTransform read only. "
"There should be no occurance of the ArrayHandle trying to pull "
"data from the execution environment.");
}
VTKM_CONT_EXPORT
void Shrink(vtkm::Id vtkmNotUsed(numberOfValues)) {
throw vtkm::cont::ErrorControlBadType(
"ArrayHandleTransform read only. Cannot shrink.");
}
VTKM_CONT_EXPORT
PortalExecution GetPortalExecution() {
throw vtkm::cont::ErrorControlInternal(
"ArrayHandleTransform read only. Cannot get writable array portal. "
"(Should have detected that by now.)");
}
VTKM_CONT_EXPORT
PortalConstExecution GetPortalConstExecution() const {
VTKM_ASSERT_CONT(this->ExecutionPortalConstValid);
return this->ExecutionPortalConst;
}
VTKM_CONT_EXPORT
void ReleaseResources() {
VTKM_ASSERT_CONT(this->ArrayValid);
this->Array.ReleaseResourcesExecution();
this->ExecutionPortalConstValid = false;
}
private:
ArrayHandleType Array;
bool ArrayValid;
PortalConstExecution ExecutionPortalConst;
bool ExecutionPortalConstValid;
};
} // namespace internal
/// \brief Implicitly transform values of one array to another with a functor.
///
/// ArrayHandleTransforms is a specialization of ArrayHandle. It takes a
/// delegate array handle and makes a new handle that calls a given unary
/// functor with the element at a given index and returns the result of the
/// functor as the value of this array at that position. This transformation is
/// done on demand. That is, rather than make a new copy of the array with new
/// values, the transformation is done as values are read from the array. Thus,
/// the functor operator should work in both the control and execution
/// environments.
///
template <typename ValueType,
class ArrayHandleType,
class FunctorType>
class ArrayHandleTransform
: public vtkm::cont::ArrayHandle<
ValueType,
internal::StorageTagTransform<ValueType, ArrayHandleType, FunctorType> >
{
// If the following line gives a compile error, then the ArrayHandleType
// template argument is not a valid ArrayHandle type.
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
typedef internal::StorageTagTransform<ValueType, ArrayHandleType, FunctorType>
StorageTag;
typedef vtkm::cont::internal::Storage<ValueType, StorageTag> StorageType;
public:
typedef vtkm::cont::ArrayHandle<ValueType, StorageTag> Superclass;
ArrayHandleTransform() : Superclass( ) { }
ArrayHandleTransform(const ArrayHandleType &handle,
const FunctorType &functor = FunctorType())
: Superclass(StorageType(handle, functor)) { }
};
/// make_ArrayHandleTransform is convenience function to generate an
/// ArrayHandleTransform. It takes in an ArrayHandle and a functor
/// to apply to each element of the Handle.
template <typename T, typename HandleType, typename FunctorType>
VTKM_CONT_EXPORT
vtkm::cont::ArrayHandleTransform<T, HandleType, FunctorType>
make_ArrayHandleTransform(HandleType handle, FunctorType functor)
{
return ArrayHandleTransform<T,HandleType,FunctorType>(handle,functor);
}
}
} // namespace vtkm::cont
#endif //vtk_m_cont_ArrayHandleTransform_h

@ -25,6 +25,7 @@ set(headers
ArrayHandleCompositeVector.h
ArrayHandleCounting.h
ArrayHandleImplicit.h
ArrayHandleTransform.h
ArrayHandleUniformPointCoordinates.h
ArrayPortal.h
ArrayPortalToIterators.h

@ -30,6 +30,7 @@ set(unit_tests
UnitTestArrayHandleCompositeVector.cxx
UnitTestArrayHandleCounting.cxx
UnitTestArrayHandleImplicit.cxx
UnitTestArrayHandleTransform.cxx
UnitTestArrayHandleUniformPointCoordinates.cxx
UnitTestArrayPortalToIterators.cxx
UnitTestContTesting.cxx

@ -0,0 +1,178 @@
//=============================================================================
//
// 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.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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/ArrayHandleTransform.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/exec/FunctorBase.h>
#include <vtkm/cont/testing/Testing.h>
namespace {
const vtkm::Id ARRAY_SIZE = 10;
template<typename ValueType>
struct MySquare
{
template<typename U>
VTKM_EXEC_EXPORT
ValueType operator()(U u) const
{ return vtkm::dot(u, u); }
};
template<typename OriginalPortalType, typename TransformedPortalType>
struct CheckTransformFunctor : vtkm::exec::FunctorBase
{
OriginalPortalType OriginalPortal;
TransformedPortalType TransformedPortal;
VTKM_EXEC_EXPORT
void operator()(vtkm::Id index) const {
typedef typename TransformedPortalType::ValueType T;
typename OriginalPortalType::ValueType original =
this->OriginalPortal.Get(index);
T transformed = this->TransformedPortal.Get(index);
if (!test_equal(transformed, MySquare<T>()(original))) {
this->RaiseError("Encountered bad transformed value.");
}
}
};
template<typename OriginalArrayHandleType,
typename TransformedArrayHandleType,
typename Device>
VTKM_CONT_EXPORT
CheckTransformFunctor<
typename OriginalArrayHandleType::template ExecutionTypes<Device>::PortalConst,
typename TransformedArrayHandleType::template ExecutionTypes<Device>::PortalConst>
make_CheckTransformFunctor(const OriginalArrayHandleType &originalArray,
const TransformedArrayHandleType &transformedArray,
Device)
{
typedef typename OriginalArrayHandleType::template ExecutionTypes<Device>::PortalConst OriginalPortalType;
typedef typename TransformedArrayHandleType::template ExecutionTypes<Device>::PortalConst TransformedPortalType;
CheckTransformFunctor<OriginalPortalType, TransformedPortalType> functor;
functor.OriginalPortal = originalArray.PrepareForInput(Device());
functor.TransformedPortal = transformedArray.PrepareForInput(Device());
return functor;
}
template<typename InputValueType>
struct TransformTests
{
typedef typename vtkm::VecTraits<InputValueType>::ComponentType
OutputValueType;
typedef MySquare<OutputValueType> FunctorType;
typedef vtkm::cont::ArrayHandleTransform<OutputValueType,
vtkm::cont::ArrayHandle<InputValueType>,
FunctorType> TransformHandle;
typedef vtkm::cont::ArrayHandleTransform<OutputValueType,
vtkm::cont::ArrayHandleCounting<InputValueType>,
FunctorType> CountingTransformHandle;
typedef VTKM_DEFAULT_DEVICE_ADAPTER_TAG Device;
typedef vtkm::cont::DeviceAdapterAlgorithm<Device> Algorithm;
void operator()() const
{
FunctorType functor;
//test the make_ArrayHandleTransform method
//test a transform handle with a counting handle as the values
vtkm::cont::ArrayHandleCounting<InputValueType> counting =
vtkm::cont::make_ArrayHandleCounting(InputValueType(OutputValueType(0)),
ARRAY_SIZE);
CountingTransformHandle countingTransformed =
vtkm::cont::make_ArrayHandleTransform<OutputValueType>(counting, functor);
// Verify that the execution portal works.
Algorithm::Schedule(
make_CheckTransformFunctor(counting, countingTransformed, Device()),
ARRAY_SIZE);
//test a transform handle with a normal handle as the values
//we are going to connect the two handles up, and than fill
//the values and make the transform sees the new values in the handle
vtkm::cont::ArrayHandle<InputValueType> input;
TransformHandle thandle(input,functor);
typedef typename vtkm::cont::ArrayHandle<InputValueType>::PortalControl
Portal;
input.Allocate(ARRAY_SIZE);
Portal portal = input.GetPortalControl();
for(vtkm::Id index=0; index < ARRAY_SIZE; ++index)
{
portal.Set(index, InputValueType(index+2) );
}
// Verify that the execution portal works.
Algorithm::Schedule(
make_CheckTransformFunctor(input, thandle, Device()),
ARRAY_SIZE);
//now update the array handle values again, so that
//we can verify the transform handle gets these updated values
for(vtkm::Id index=0; index < ARRAY_SIZE; ++index)
{
portal.Set(index, InputValueType(index*index));
}
// Verify that the execution portal works.
Algorithm::Schedule(
make_CheckTransformFunctor(input, thandle, Device()),
ARRAY_SIZE);
}
};
struct TryInputType
{
template<typename InputType>
void operator()(InputType) const {
TransformTests<InputType>()();
}
};
void TestArrayHandleTransform()
{
// vtkm::testing::Testing::TryAllTypes(TryInputType());
vtkm::testing::Testing::TryTypes(TryInputType(), vtkm::TypeListTagCommon());
}
} // annonymous namespace
int UnitTestArrayHandleTransform(int, char *[])
{
return vtkm::cont::testing::Testing::Run(TestArrayHandleTransform);
}