From f35601162ed4c633c4792dd0a0891d25dd228ace Mon Sep 17 00:00:00 2001 From: Sujin Philip Date: Tue, 29 Sep 2015 10:36:11 -0400 Subject: [PATCH] Fix ArrayHandleTransform to use supplied functor object instead of a default --- vtkm/cont/ArrayHandleTransform.h | 11 ++++- vtkm/cont/testing/TestingFancyArrayHandles.h | 48 +++++++++++++++----- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/vtkm/cont/ArrayHandleTransform.h b/vtkm/cont/ArrayHandleTransform.h index 6a1dcb4fc..e0259287d 100644 --- a/vtkm/cont/ArrayHandleTransform.h +++ b/vtkm/cont/ArrayHandleTransform.h @@ -205,6 +205,11 @@ public: return this->Array; } + VTKM_CONT_EXPORT + const FunctorType &GetFunctor() const { + return this->Functor; + } + private: ArrayHandleType Array; FunctorType Functor; @@ -237,7 +242,8 @@ public: FunctorType> PortalConstExecution; VTKM_CONT_EXPORT - ArrayTransfer(StorageType *storage) : Array(storage->GetArray()) { } + ArrayTransfer(StorageType *storage) + : Array(storage->GetArray()), Functor(storage->GetFunctor()) { } VTKM_CONT_EXPORT vtkm::Id GetNumberOfValues() const { @@ -246,7 +252,7 @@ public: VTKM_CONT_EXPORT PortalConstExecution PrepareForInput(bool vtkmNotUsed(updateData)) { - return PortalConstExecution(this->Array.PrepareForInput(Device())); + return PortalConstExecution(this->Array.PrepareForInput(Device()), this->Functor); } VTKM_CONT_EXPORT @@ -283,6 +289,7 @@ public: private: ArrayHandleType Array; + FunctorType Functor; }; } // namespace internal diff --git a/vtkm/cont/testing/TestingFancyArrayHandles.h b/vtkm/cont/testing/TestingFancyArrayHandles.h index 0f5eea3fb..5bba87b5b 100644 --- a/vtkm/cont/testing/TestingFancyArrayHandles.h +++ b/vtkm/cont/testing/TestingFancyArrayHandles.h @@ -64,6 +64,35 @@ struct ValueSquared { return vtkm::dot(u, u); } }; +struct ValueScale +{ + ValueScale() : Factor() + { } + + ValueScale(vtkm::Float64 factor) : Factor(factor) + { } + + template + VTKM_EXEC_CONT_EXPORT + ValueType operator()(const ValueType &v) const + { + typedef vtkm::VecTraits Traits; + typedef typename Traits::ComponentType ComponentType; + + ValueType result; + for (vtkm::IdComponent i = 0; i < Traits::GetNumberOfComponents(v); ++i) + { + vtkm::Float64 vi = static_cast(Traits::GetComponent(v, i)); + vtkm::Float64 ri = vi * this->Factor; + Traits::SetComponent(result, i, static_cast(ri)); + } + return result; + } + +private: + vtkm::Float64 Factor; +}; + } namespace vtkm { @@ -292,19 +321,17 @@ private: template< typename ValueType> VTKM_CONT_EXPORT void operator()(const ValueType vtkmNotUsed(v)) const { - typedef typename vtkm::VecTraits::ComponentType OutputValueType; - typedef fancy_array_detail::ValueSquared FunctorType; + typedef fancy_array_detail::ValueScale FunctorType; const vtkm::Id length = ARRAY_SIZE; - FunctorType functor; + FunctorType functor(2.0); vtkm::cont::ArrayHandle input; vtkm::cont::ArrayHandleTransform< - OutputValueType, + ValueType, vtkm::cont::ArrayHandle, FunctorType> transformed = - vtkm::cont::make_ArrayHandleTransform(input, - functor); + vtkm::cont::make_ArrayHandleTransform(input, functor); typedef typename vtkm::cont::ArrayHandle::PortalControl Portal; input.Allocate(length); @@ -314,7 +341,7 @@ private: portal.Set(i, TestValue(i, ValueType()) ); } - vtkm::cont::ArrayHandle< OutputValueType > result; + vtkm::cont::ArrayHandle< ValueType > result; vtkm::worklet::DispatcherMapField< PassThrough, DeviceAdapterTag > dispatcher; dispatcher.Invoke(transformed, result); @@ -322,10 +349,9 @@ private: //verify that the control portal works for(vtkm::Id i=0; i < length; ++i) { - const OutputValueType result_v = result.GetPortalConstControl().Get(i); - const OutputValueType correct_value = functor(TestValue(i, ValueType())); - const OutputValueType control_value = - transformed.GetPortalConstControl().Get(i); + const ValueType result_v = result.GetPortalConstControl().Get(i); + const ValueType correct_value = functor(TestValue(i, ValueType())); + const ValueType control_value = transformed.GetPortalConstControl().Get(i); VTKM_TEST_ASSERT(test_equal(result_v, correct_value), "Transform Handle Failed"); VTKM_TEST_ASSERT(test_equal(result_v, control_value),