vtk-m/vtkm/cont/ArrayRangeCompute.hxx

121 lines
3.4 KiB
C++
Raw Normal View History

//============================================================================
// 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_ArrayRangeCompute_hxx
#define vtk_m_cont_ArrayRangeCompute_hxx
#include <vtkm/cont/ArrayRangeCompute.h>
#include <vtkm/BinaryOperators.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
#include <vtkm/cont/TryExecute.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
2017-05-18 14:29:41 +00:00
namespace detail
{
2017-05-18 14:29:41 +00:00
template <typename ArrayHandleType>
struct ArrayRangeComputeFunctor
{
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
ArrayHandleType InputArray;
vtkm::cont::ArrayHandle<vtkm::Range> RangeArray;
VTKM_CONT
2017-05-18 14:29:41 +00:00
ArrayRangeComputeFunctor(const ArrayHandleType& input)
: InputArray(input)
2017-05-18 14:29:41 +00:00
{
}
2017-05-18 14:29:41 +00:00
template <typename Device>
VTKM_CONT bool operator()(Device)
{
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
using ValueType = typename ArrayHandleType::ValueType;
using VecTraits = vtkm::VecTraits<ValueType>;
const vtkm::IdComponent NumberOfComponents = VecTraits::NUM_COMPONENTS;
typedef vtkm::cont::DeviceAdapterAlgorithm<Device> Algorithm;
this->RangeArray.Allocate(NumberOfComponents);
if (this->InputArray.GetNumberOfValues() < 1)
{
for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i)
{
this->RangeArray.GetPortalControl().Set(i, vtkm::Range());
}
return true;
}
2017-05-18 14:29:41 +00:00
vtkm::Vec<ValueType, 2> initial(this->InputArray.GetPortalConstControl().Get(0));
vtkm::Vec<ValueType, 2> result =
2017-05-18 14:29:41 +00:00
Algorithm::Reduce(this->InputArray, initial, vtkm::MinAndMax<ValueType>());
for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i)
{
this->RangeArray.GetPortalControl().Set(
2017-05-18 14:29:41 +00:00
i,
vtkm::Range(VecTraits::GetComponent(result[0], i), VecTraits::GetComponent(result[1], i)));
}
return true;
}
};
2017-05-18 14:29:41 +00:00
template <typename ArrayHandleType>
inline vtkm::cont::ArrayHandle<vtkm::Range> ArrayRangeComputeImpl(
const ArrayHandleType& input, vtkm::cont::RuntimeDeviceTracker tracker)
{
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
detail::ArrayRangeComputeFunctor<ArrayHandleType> functor(input);
if (!vtkm::cont::TryExecute(functor, tracker))
{
2017-05-18 14:29:41 +00:00
throw vtkm::cont::ErrorExecution("Failed to run ArrayRangeComputation on any device.");
}
return functor.RangeArray;
}
} // namespace detail
2017-05-18 14:29:41 +00:00
template <typename ArrayHandleType>
inline vtkm::cont::ArrayHandle<vtkm::Range> ArrayRangeCompute(
const ArrayHandleType& input, vtkm::cont::RuntimeDeviceTracker tracker)
{
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
return detail::ArrayRangeComputeImpl(input, tracker);
}
}
} // namespace vtkm::cont
#endif //vtk_m_cont_ArrayRangeCompute_hxx