Add ArrayHandleCast

This commit is contained in:
Sujin Philip 2015-09-01 17:42:45 -04:00
parent 1b01d824c6
commit a1c74bd15a
3 changed files with 128 additions and 0 deletions

@ -0,0 +1,86 @@
//=============================================================================
//
// 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_ArrayHandleCast_h
#define vtk_m_cont_ArrayHandleCast_h
#include <vtkm/cont/ArrayHandleTransform.h>
namespace vtkm {
namespace cont {
namespace internal {
template<typename FromType, typename ToType>
struct Cast
{
VTKM_EXEC_CONT_EXPORT
ToType operator()(const FromType &val) const
{
return static_cast<ToType>(val);
}
};
} // namespace internal
/// \brief Cast the values of an array to the specified type, on demand.
///
/// ArrayHandleCast is a specialization of ArrayHandleTransform. Given an ArrayHandle
/// and a type, it creates a new handle that returns the elements of the array cast
/// to the specified type.
///
template <typename ValueType, typename ArrayHandleType>
class ArrayHandleCast :
public vtkm::cont::ArrayHandleTransform<
ValueType,
ArrayHandleType,
internal::Cast<typename ArrayHandleType::ValueType, ValueType> >
{
public:
typedef vtkm::cont::ArrayHandleTransform<
ValueType,
ArrayHandleType,
internal::Cast<typename ArrayHandleType::ValueType, ValueType> > SuperClass;
ArrayHandleCast() : SuperClass()
{ }
ArrayHandleCast(const ArrayHandleType &handle)
: SuperClass(handle)
{ }
};
/// make_ArrayHandleCast is convenience function to generate an
/// ArrayHandleCast.
///
template <typename T, typename HandleType>
VTKM_CONT_EXPORT
ArrayHandleCast<T, HandleType> make_ArrayHandleCast(const HandleType &handle,
const T&)
{
return ArrayHandleCast<T, HandleType>(handle);
}
}
} // namespace vtkm::cont
#endif // vtk_m_cont_ArrayHandleCast_h

@ -22,6 +22,7 @@ include_directories(${Boost_INCLUDE_DIRS})
set(headers
ArrayHandle.h
ArrayHandleCast.h
ArrayHandleCompositeVector.h
ArrayHandleConstant.h
ArrayHandleCounting.h

@ -23,6 +23,7 @@
#define vtk_m_cont_testing_TestingFancyArrayHandles_h
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleCast.h>
#include <vtkm/cont/ArrayHandleCompositeVector.h>
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/ArrayHandleCounting.h>
@ -452,6 +453,34 @@ private:
}
};
struct TestCastAsInput
{
template<typename CastToType>
VTKM_CONT_EXPORT
void operator()(CastToType vtkmNotUsed(type)) const
{
typedef vtkm::cont::ArrayHandleCounting<vtkm::Int64> InputArrayType;
InputArrayType input(0, ARRAY_SIZE);
vtkm::cont::ArrayHandleCast<CastToType, InputArrayType> castArray =
vtkm::cont::make_ArrayHandleCast(input, CastToType());
vtkm::cont::ArrayHandle<CastToType> result;
vtkm::worklet::DispatcherMapField< PassThrough, DeviceAdapterTag > dispatcher;
dispatcher.Invoke(castArray, result);
// verify results
vtkm::Id length = ARRAY_SIZE;
for (vtkm::Id i = 0; i < length; ++i)
{
VTKM_TEST_ASSERT(
result.GetPortalConstControl().Get(i) ==
static_cast<CastToType>(input.GetPortalConstControl().Get(i)),
"Casting ArrayHandle Failed");
}
}
};
struct TestZipAsInput
{
@ -617,6 +646,12 @@ private:
>
{ };
struct CastTypesToTest
: vtkm::ListTagBase< vtkm::Int32,
vtkm::UInt32
>
{ };
struct TestAll
{
@ -666,6 +701,12 @@ private:
TestingFancyArrayHandles<DeviceAdapterTag>::TestCountingTransformAsInput(),
HandleTypesToTest());
std::cout << "-------------------------------------------" << std::endl;
std::cout << "Testing ArrayHandleCast as Input" << std::endl;
vtkm::testing::Testing::TryTypes(
TestingFancyArrayHandles<DeviceAdapterTag>::TestCastAsInput(),
CastTypesToTest());
std::cout << "-------------------------------------------" << std::endl;
std::cout << "Testing ArrayHandleZip as Input" << std::endl;
vtkm::testing::Testing::TryTypes(