VTK-m  2.0
ArrayRangeComputeTemplate.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_ArrayRangeComputeTemplate_h
11 #define vtk_m_cont_ArrayRangeComputeTemplate_h
12 
14 
15 #include <vtkm/BinaryOperators.h>
16 #include <vtkm/VecTraits.h>
17 
18 #include <vtkm/cont/Algorithm.h>
19 
20 #include <limits>
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 
27 namespace detail
28 {
29 
30 struct ArrayRangeComputeFunctor
31 {
32  template <typename Device, typename T, typename S>
33  VTKM_CONT bool operator()(Device,
34  const vtkm::cont::ArrayHandle<T, S>& handle,
35  const vtkm::Vec<T, 2>& initialValue,
36  vtkm::Vec<T, 2>& result) const
37  {
40  result = Algorithm::Reduce(handle, initialValue, vtkm::MinAndMax<T>());
41  return true;
42  }
43 };
44 
45 template <typename T, typename S>
46 inline vtkm::cont::ArrayHandle<vtkm::Range> ArrayRangeComputeImpl(
47  const vtkm::cont::ArrayHandle<T, S>& input,
49 {
50  VTKM_LOG_SCOPE(vtkm::cont::LogLevel::Perf, "ArrayRangeCompute");
51 
52  using VecTraits = vtkm::VecTraits<T>;
53  using CT = typename VecTraits::ComponentType;
54  //We want to minimize the amount of code that we do in try execute as
55  //it is repeated for each
58 
59  if (input.GetNumberOfValues() < 1)
60  {
61  auto portal = range.WritePortal();
62  for (vtkm::IdComponent i = 0; i < VecTraits::NUM_COMPONENTS; ++i)
63  {
64  portal.Set(i, vtkm::Range());
65  }
66  }
67  else
68  {
69  //We used the limits, so that we don't need to sync the array handle
70  //
71  vtkm::Vec<T, 2> result;
72  vtkm::Vec<T, 2> initial;
73  initial[0] = T(std::numeric_limits<CT>::max());
74  initial[1] = T(std::numeric_limits<CT>::lowest());
75 
76  const bool rangeComputed = vtkm::cont::TryExecuteOnDevice(
77  device, detail::ArrayRangeComputeFunctor{}, input, initial, result);
78  if (!rangeComputed)
79  {
81  }
82  else
83  {
84  auto portal = range.WritePortal();
85  for (vtkm::IdComponent i = 0; i < VecTraits::NUM_COMPONENTS; ++i)
86  {
87  portal.Set(i,
89  VecTraits::GetComponent(result[1], i)));
90  }
91  }
92  }
93  return range;
94 }
95 
96 } // namespace detail
97 
98 
99 template <typename ArrayHandleType>
101  const ArrayHandleType& input,
102  vtkm::cont::DeviceAdapterId device = vtkm::cont::DeviceAdapterTagAny{})
103 {
104  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
105  return detail::ArrayRangeComputeImpl(input, device);
106 }
107 }
108 } // namespace vtkm::cont
109 
110 #endif //vtk_m_cont_ArrayRangeComputeTemplate_h
VTKM_LOG_SCOPE
#define VTKM_LOG_SCOPE(level,...)
Definition: Logging.h:265
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::cont::ArrayHandle< T, S >
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
ArrayRangeCompute.h
vtkm::MinAndMax
Binary Predicate that takes two arguments argument x, and y and returns a vtkm::Vec<T,...
Definition: BinaryOperators.h:112
vtkm::cont::ArrayHandle::Allocate
VTKM_CONT void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:465
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::TryExecuteOnDevice
VTKM_CONT bool TryExecuteOnDevice(vtkm::cont::DeviceAdapterId devId, Functor &&functor)
Try to execute a functor on a specific device selected at runtime.
Definition: TryExecute.h:178
vtkm::VecTraits::GetComponent
static const VTKM_EXEC_CONT ComponentType & GetComponent(const typename std::remove_const< VecType >::type &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Algorithm.h
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Definition: ArrayHandle.h:132
vtkm::cont::ThrowArrayRangeComputeFailed
VTKM_CONT_EXPORT void ThrowArrayRangeComputeFailed()
vtkm::Vec< T, 2 >
Definition: Types.h:859
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::ArrayHandle::WritePortal
VTKM_CONT WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:435
vtkm::cont::DeviceAdapterAlgorithm
Struct containing device adapter algorithms.
Definition: DeviceAdapterAlgorithm.h:41
BinaryOperators.h
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::cont::Algorithm::Reduce
static VTKM_CONT U Reduce(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, U initialValue)
Definition: Algorithm.h:656
vtkm::VecTraits::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:73
vtkm::VecTraits< T >
vtkm::cont::ArrayRangeCompute
VTKM_CONT_EXPORT vtkm::cont::ArrayHandle< vtkm::Range > ArrayRangeCompute(const vtkm::cont::UnknownArrayHandle &array, vtkm::cont::DeviceAdapterId device=vtkm::cont::DeviceAdapterTagAny{})
Compute the range of the data in an array handle.
VecTraits.h
vtkm::cont::LogLevel::Perf
@ Perf
General timing data and algorithm flow information, such as filter execution, worklet dispatches,...
vtkm::VecTraits::NUM_COMPONENTS
static constexpr vtkm::IdComponent NUM_COMPONENTS
Number of components in the vector.
Definition: VecTraits.h:86
VTKM_IS_DEVICE_ADAPTER_TAG
#define VTKM_IS_DEVICE_ADAPTER_TAG(tag)
Checks that the argument is a proper device adapter tag.
Definition: DeviceAdapterTag.h:164
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31