Add ArrayHandleExtractComponent.

This is part of #43, which will ultimately simplify the
ArrayHandleCompositeVector to a new implementation that can be easily
written to. Part of this effort will remove the ability to pull a single
component from a vector-typed input ArrayHandle for use in the
CompositeVector, and this new class makes sure we can still support that
usecase.
This commit is contained in:
Allison Vacanti 2017-07-26 16:59:48 -04:00
parent c077f16fb9
commit 84588c803f
4 changed files with 552 additions and 0 deletions

@ -0,0 +1,303 @@
//=============================================================================
//
// 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 2017 Sandia Corporation.
// Copyright 2017 UT-Battelle, LLC.
// Copyright 2017 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_ArrayHandleExtractComponent_h
#define vtk_m_cont_ArrayHandleExtractComponent_h
#include <vtkm/VecTraits.h>
#include <vtkm/cont/ArrayHandle.h>
namespace vtkm
{
namespace cont
{
namespace internal
{
template <typename PortalType, vtkm::IdComponent Component>
class VTKM_ALWAYS_EXPORT ArrayPortalExtractComponent
{
public:
using VectorType = typename PortalType::ValueType;
using Traits = vtkm::VecTraits<VectorType>;
using ComponentType = typename Traits::ComponentType;
static const vtkm::IdComponent COMPONENT = Component;
VTKM_EXEC_CONT
ArrayPortalExtractComponent()
: Portal()
{
}
VTKM_EXEC_CONT
ArrayPortalExtractComponent(const PortalType& portal)
: Portal(portal)
{
}
// Copy constructor
VTKM_EXEC_CONT ArrayPortalExtractComponent(
const ArrayPortalExtractComponent<PortalType, Component>& src)
: Portal(src.GetPortal())
{
}
VTKM_EXEC_CONT
vtkm::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues(); }
VTKM_EXEC_CONT
ComponentType Get(vtkm::Id index) const
{
return Traits::GetComponent(this->Portal.Get(index), Component);
}
VTKM_EXEC_CONT
void Set(vtkm::Id index, const ComponentType& value) const
{
VectorType vec = this->Portal.Get(index);
Traits::SetComponent(vec, Component, value);
this->Portal.Set(index, vec);
}
VTKM_EXEC_CONT
const PortalType& GetPortal() const { return this->Portal; }
private:
PortalType Portal;
}; // class ArrayPortalExtractComponent
} // namespace internal
template <typename ArrayHandleType, vtkm::IdComponent Component>
class StorageTagExtractComponent
{
static const vtkm::IdComponent COMPONENT = Component;
};
namespace internal
{
template <typename ArrayHandleType, vtkm::IdComponent Component>
class Storage<typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
StorageTagExtractComponent<ArrayHandleType, Component>>
{
public:
using PortalType =
ArrayPortalExtractComponent<typename ArrayHandleType::PortalControl, Component>;
using PortalConstType =
ArrayPortalExtractComponent<typename ArrayHandleType::PortalConstControl, Component>;
using ValueType = typename PortalType::ComponentType;
VTKM_CONT
Storage()
: Valid(false)
{
}
VTKM_CONT
Storage(const ArrayHandleType& array)
: Array(array)
, Valid(true)
{
}
VTKM_CONT
PortalConstType GetPortalConst() const
{
VTKM_ASSERT(this->Valid);
return PortalConstType(this->Array.GetPortalConstControl());
}
VTKM_CONT
PortalType GetPortal()
{
VTKM_ASSERT(this->Valid);
return PortalType(this->Array.GetPortalControl());
}
VTKM_CONT
vtkm::Id GetNumberOfValues() const
{
VTKM_ASSERT(this->Valid);
return this->Array.GetNumberOfValues();
}
VTKM_CONT
void Allocate(vtkm::Id numberOfValues)
{
VTKM_ASSERT(this->Valid);
this->Array.Allocate(numberOfValues);
}
VTKM_CONT
void Shrink(vtkm::Id numberOfValues)
{
VTKM_ASSERT(this->Valid);
this->Array.Shrink(numberOfValues);
}
VTKM_CONT
void ReleaseResources()
{
VTKM_ASSERT(this->Valid);
this->Array.ReleaseResources();
}
VTKM_CONT
const ArrayHandleType& GetArray() const
{
VTKM_ASSERT(this->Valid);
return this->Array;
}
private:
ArrayHandleType Array;
bool Valid;
}; // class Storage
template <typename ArrayHandleType, vtkm::IdComponent Component, typename Device>
class ArrayTransfer<typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
StorageTagExtractComponent<ArrayHandleType, Component>,
Device>
{
public:
using ValueType = typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType;
private:
using StorageTag = StorageTagExtractComponent<ArrayHandleType, Component>;
using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
using ArrayValueType = typename ArrayHandleType::ValueType;
using ArrayStorageTag = typename ArrayHandleType::StorageTag;
using ArrayStorageType =
vtkm::cont::internal::Storage<typename ArrayHandleType::ValueType, ArrayStorageTag>;
public:
using PortalControl = typename StorageType::PortalType;
using PortalConstControl = typename StorageType::PortalConstType;
using ExecutionTypes = typename ArrayHandleType::template ExecutionTypes<Device>;
using PortalExecution = ArrayPortalExtractComponent<typename ExecutionTypes::Portal, Component>;
using PortalConstExecution =
ArrayPortalExtractComponent<typename ExecutionTypes::PortalConst, Component>;
VTKM_CONT
ArrayTransfer(StorageType* storage)
: Array(storage->GetArray())
{
}
VTKM_CONT
vtkm::Id GetNumberOfValues() const { return this->Array.GetNumberOfValues(); }
VTKM_CONT
PortalConstExecution PrepareForInput(bool vtkmNotUsed(updateData))
{
return PortalConstExecution(this->Array.PrepareForInput(Device()));
}
VTKM_CONT
PortalExecution PrepareForInPlace(bool vtkmNotUsed(updateData))
{
return PortalExecution(this->Array.PrepareForInPlace(Device()));
}
VTKM_CONT
PortalExecution PrepareForOutput(vtkm::Id numberOfValues)
{
return PortalExecution(this->Array.PrepareForOutput(numberOfValues, Device()));
}
VTKM_CONT
void RetrieveOutputData(StorageType* vtkmNotUsed(storage)) const
{
// Implementation of this method should be unnecessary. The internal
// array handle should automatically retrieve the output data as
// necessary.
}
VTKM_CONT
void Shrink(vtkm::Id numberOfValues) { this->Array.Shrink(numberOfValues); }
VTKM_CONT
void ReleaseResources() { this->Array.ReleaseResourcesExecution(); }
private:
ArrayHandleType Array;
};
}
}
} // namespace vtkm::cont::internal
namespace vtkm
{
namespace cont
{
/// \brief A fancy ArrayHandle that turns a vector array into a scalar array by
/// slicing out a single component of each vector.
///
/// ArrayHandleExtractComponent is a specialization of ArrayHandle. It takes an
/// input ArrayHandle with a vtkm::Vec ValueType and a component index
/// and uses this information to expose a scalar array consisting of the
/// specified component across all vectors in the input ArrayHandle. So for a
/// given index i, ArrayHandleExtractComponent looks up the i-th vtkm::Vec in
/// the index array and reads or writes to the specified component, leave all
/// other components unmodified. This is done on the fly rather than creating a
/// copy of the array.
template <typename ArrayHandleType, vtkm::IdComponent Component>
class ArrayHandleExtractComponent
: public vtkm::cont::ArrayHandle<
typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
StorageTagExtractComponent<ArrayHandleType, Component>>
{
public:
static const vtkm::IdComponent COMPONENT = Component;
VTKM_ARRAY_HANDLE_SUBCLASS(
ArrayHandleExtractComponent,
(ArrayHandleExtractComponent<ArrayHandleType, Component>),
(vtkm::cont::ArrayHandle<
typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
StorageTagExtractComponent<ArrayHandleType, Component>>));
protected:
using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
public:
VTKM_CONT
ArrayHandleExtractComponent(const ArrayHandleType& array)
: Superclass(StorageType(array))
{
}
};
/// make_ArrayHandleExtractComponent is convenience function to generate an
/// ArrayHandleExtractComponent.
template <vtkm::IdComponent Component, typename ArrayHandleType>
VTKM_CONT ArrayHandleExtractComponent<ArrayHandleType, Component> make_ArrayHandleExtractComponent(
const ArrayHandleType& array)
{
return ArrayHandleExtractComponent<ArrayHandleType, Component>(array);
}
}
} // namespace vtkm::cont
#endif // vtk_m_cont_ArrayHandleExtractComponent_h

@ -25,6 +25,7 @@ set(headers
ArrayHandleCompositeVector.h
ArrayHandleConstant.h
ArrayHandleCounting.h
ArrayHandleExtractComponent.h
ArrayHandleDiscard.h
ArrayHandleGroupVec.h
ArrayHandleGroupVecVariable.h

@ -39,6 +39,7 @@ set(unit_tests
UnitTestArrayHandleCompositeVector.cxx
UnitTestArrayHandleCounting.cxx
UnitTestArrayHandleDiscard.cxx
UnitTestArrayHandleExtractComponent.cxx
UnitTestArrayHandleImplicit.cxx
UnitTestArrayHandleIndex.cxx
UnitTestArrayHandleReverse.cxx

@ -0,0 +1,247 @@
//=============================================================================
//
// 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/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleCompositeVector.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/ArrayHandleExtractComponent.h>
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
#include <vtkm/cont/testing/Testing.h>
namespace
{
template <typename ValueType>
struct ExtractComponentTests
{
using InputArray = vtkm::cont::ArrayHandle<vtkm::Vec<ValueType, 4>>;
template <vtkm::IdComponent Component>
using ExtractArray = vtkm::cont::ArrayHandleExtractComponent<InputArray, Component>;
using ReferenceComponentArray = vtkm::cont::ArrayHandleCounting<ValueType>;
using ReferenceCompositeArray =
typename vtkm::cont::ArrayHandleCompositeVectorType<ReferenceComponentArray,
ReferenceComponentArray,
ReferenceComponentArray,
ReferenceComponentArray>::type;
using DeviceTag = VTKM_DEFAULT_DEVICE_ADAPTER_TAG;
using Algo = vtkm::cont::DeviceAdapterAlgorithm<DeviceTag>;
// This is used to build a ArrayHandleExtractComponent's internal array.
ReferenceCompositeArray RefComposite;
void ConstructReferenceArray()
{
// Build the Ref array
const vtkm::Id numValues = 32;
ReferenceComponentArray c1 = vtkm::cont::make_ArrayHandleCounting<ValueType>(3, 2, numValues);
ReferenceComponentArray c2 = vtkm::cont::make_ArrayHandleCounting<ValueType>(2, 3, numValues);
ReferenceComponentArray c3 = vtkm::cont::make_ArrayHandleCounting<ValueType>(4, 4, numValues);
ReferenceComponentArray c4 = vtkm::cont::make_ArrayHandleCounting<ValueType>(1, 3, numValues);
this->RefComposite = vtkm::cont::make_ArrayHandleCompositeVector(c1, 0, c2, 0, c3, 0, c4, 0);
}
InputArray BuildInputArray() const
{
InputArray result;
Algo::Copy(this->RefComposite, result);
return result;
}
template <vtkm::IdComponent Component>
void SanityCheck() const
{
InputArray composite = this->BuildInputArray();
ExtractArray<Component> extract =
vtkm::cont::make_ArrayHandleExtractComponent<Component>(composite);
VTKM_TEST_ASSERT(composite.GetNumberOfValues() == extract.GetNumberOfValues(),
"Number of values in copied ExtractComponent array does not match input.");
}
template <vtkm::IdComponent Component>
void ReadTestComponentExtraction() const
{
// Test that the expected values are read from an ExtractComponent array.
InputArray composite = this->BuildInputArray();
ExtractArray<Component> extract =
vtkm::cont::make_ArrayHandleExtractComponent<Component>(composite);
// Test reading the data back in the control env:
this->ValidateReadTestArray<Component>(extract);
// Copy the extract array in the execution environment to test reading:
vtkm::cont::ArrayHandle<ValueType> execCopy;
Algo::Copy(extract, execCopy);
this->ValidateReadTestArray<Component>(execCopy);
}
template <vtkm::IdComponent Component, typename ArrayHandleType>
void ValidateReadTestArray(ArrayHandleType testArray) const
{
using RefVectorType = typename ReferenceCompositeArray::ValueType;
using Traits = vtkm::VecTraits<RefVectorType>;
auto testPortal = testArray.GetPortalConstControl();
auto refPortal = this->RefComposite.GetPortalConstControl();
VTKM_TEST_ASSERT(testPortal.GetNumberOfValues() == refPortal.GetNumberOfValues(),
"Number of values in read test output do not match input.");
for (vtkm::Id i = 0; i < testPortal.GetNumberOfValues(); ++i)
{
VTKM_TEST_ASSERT(
test_equal(testPortal.Get(i), Traits::GetComponent(refPortal.Get(i), Component), 0.),
"Value mismatch in read test.");
}
}
// Doubles the specified component (reading from RefVectorType).
template <typename PortalType, typename RefPortalType, vtkm::IdComponent Component>
struct WriteTestFunctor : vtkm::exec::FunctorBase
{
using RefVectorType = typename RefPortalType::ValueType;
using Traits = vtkm::VecTraits<RefVectorType>;
PortalType Portal;
RefPortalType RefPortal;
VTKM_CONT
WriteTestFunctor(const PortalType& portal, const RefPortalType& ref)
: Portal(portal)
, RefPortal(ref)
{
}
VTKM_EXEC_CONT
void operator()(vtkm::Id index) const
{
this->Portal.Set(index, Traits::GetComponent(this->RefPortal.Get(index), Component) * 2);
}
};
template <vtkm::IdComponent Component>
void WriteTestComponentExtraction() const
{
// Control test:
{
InputArray composite = this->BuildInputArray();
ExtractArray<Component> extract =
vtkm::cont::make_ArrayHandleExtractComponent<Component>(composite);
WriteTestFunctor<typename ExtractArray<Component>::PortalControl,
typename ReferenceCompositeArray::PortalConstControl,
Component>
functor(extract.GetPortalControl(), this->RefComposite.GetPortalConstControl());
for (vtkm::Id i = 0; i < extract.GetNumberOfValues(); ++i)
{
functor(i);
}
this->ValidateWriteTestArray<Component>(composite);
}
// Exec test:
{
InputArray composite = this->BuildInputArray();
ExtractArray<Component> extract =
vtkm::cont::make_ArrayHandleExtractComponent<Component>(composite);
using Portal = typename ExtractArray<Component>::template ExecutionTypes<DeviceTag>::Portal;
using RefPortal =
typename ReferenceCompositeArray::template ExecutionTypes<DeviceTag>::PortalConst;
WriteTestFunctor<Portal, RefPortal, Component> functor(
extract.PrepareForInPlace(DeviceTag()), this->RefComposite.PrepareForInput(DeviceTag()));
Algo::Schedule(functor, extract.GetNumberOfValues());
this->ValidateWriteTestArray<Component>(composite);
}
}
template <vtkm::IdComponent Component>
void ValidateWriteTestArray(InputArray testArray) const
{
using VectorType = typename ReferenceCompositeArray::ValueType;
using Traits = vtkm::VecTraits<VectorType>;
// Check that the indicated component is twice the reference value.
auto refPortal = this->RefComposite.GetPortalConstControl();
auto portal = testArray.GetPortalConstControl();
VTKM_TEST_ASSERT(portal.GetNumberOfValues() == refPortal.GetNumberOfValues(),
"Number of values in write test output do not match input.");
for (vtkm::Id i = 0; i < portal.GetNumberOfValues(); ++i)
{
auto value = portal.Get(i);
auto refValue = refPortal.Get(i);
Traits::SetComponent(refValue, Component, Traits::GetComponent(refValue, Component) * 2);
VTKM_TEST_ASSERT(test_equal(refValue, value, 0.), "Value mismatch in write test.");
}
}
template <vtkm::IdComponent Component>
void TestComponent() const
{
this->SanityCheck<Component>();
this->ReadTestComponentExtraction<Component>();
this->WriteTestComponentExtraction<Component>();
}
void operator()()
{
this->ConstructReferenceArray();
this->TestComponent<0>();
this->TestComponent<1>();
this->TestComponent<2>();
this->TestComponent<3>();
}
};
struct ArgToTemplateType
{
template <typename ValueType>
void operator()(ValueType) const
{
ExtractComponentTests<ValueType>()();
}
};
void TestArrayHandleExtractComponent()
{
using TestTypes = vtkm::ListTagBase<vtkm::Int32, vtkm::Int64, vtkm::Float32, vtkm::Float64>;
vtkm::testing::Testing::TryTypes(ArgToTemplateType(), TestTypes());
}
} // end anon namespace
int UnitTestArrayHandleExtractComponent(int, char* [])
{
return vtkm::cont::testing::Testing::Run(TestArrayHandleExtractComponent);
}