VTK-m  1.8
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 #ifndef VTKM_NO_DEPRECATED_VIRTUAL
22 #endif
23 
24 #include <limits>
25 
26 namespace vtkm
27 {
28 namespace cont
29 {
30 
31 namespace detail
32 {
33 
34 struct ArrayRangeComputeFunctor
35 {
36  template <typename Device, typename T, typename S>
37  VTKM_CONT bool operator()(Device,
38  const vtkm::cont::ArrayHandle<T, S>& handle,
39  const vtkm::Vec<T, 2>& initialValue,
40  vtkm::Vec<T, 2>& result) const
41  {
44  result = Algorithm::Reduce(handle, initialValue, vtkm::MinAndMax<T>());
45  return true;
46  }
47 };
48 
49 template <typename T, typename S>
50 inline vtkm::cont::ArrayHandle<vtkm::Range> ArrayRangeComputeImpl(
51  const vtkm::cont::ArrayHandle<T, S>& input,
53 {
54  VTKM_LOG_SCOPE(vtkm::cont::LogLevel::Perf, "ArrayRangeCompute");
55 
56  using VecTraits = vtkm::VecTraits<T>;
57  using CT = typename VecTraits::ComponentType;
58  //We want to minimize the amount of code that we do in try execute as
59  //it is repeated for each
62 
63  if (input.GetNumberOfValues() < 1)
64  {
65  auto portal = range.WritePortal();
66  for (vtkm::IdComponent i = 0; i < VecTraits::NUM_COMPONENTS; ++i)
67  {
68  portal.Set(i, vtkm::Range());
69  }
70  }
71  else
72  {
73  //We used the limits, so that we don't need to sync the array handle
74  //
75  vtkm::Vec<T, 2> result;
76  vtkm::Vec<T, 2> initial;
77  initial[0] = T(std::numeric_limits<CT>::max());
78  initial[1] = T(std::numeric_limits<CT>::lowest());
79 
80  const bool rangeComputed = vtkm::cont::TryExecuteOnDevice(
81  device, detail::ArrayRangeComputeFunctor{}, input, initial, result);
82  if (!rangeComputed)
83  {
85  }
86  else
87  {
88  auto portal = range.WritePortal();
89  for (vtkm::IdComponent i = 0; i < VecTraits::NUM_COMPONENTS; ++i)
90  {
91  portal.Set(i,
93  VecTraits::GetComponent(result[1], i)));
94  }
95  }
96  }
97  return range;
98 }
99 
100 } // namespace detail
101 
102 
103 #ifndef VTKM_NO_DEPRECATED_VIRTUAL
105 VTKM_CONT
107  const vtkm::cont::ArrayHandleVirtual<vtkm::Vec3f>& input,
109 {
110  using UniformHandleType = ArrayHandleUniformPointCoordinates;
111  using RectilinearHandleType =
115 
116  if (input.IsType<UniformHandleType>())
117  {
118  using T = typename UniformHandleType::ValueType;
119  using S = typename UniformHandleType::StorageTag;
120  const vtkm::cont::internal::detail::StorageVirtual* storage =
121  input.GetStorage().GetStorageVirtual();
122  const auto* castStorage =
123  storage->Cast<vtkm::cont::internal::detail::StorageVirtualImpl<T, S>>();
124 
125  return ArrayRangeCompute(castStorage->GetHandle(), device);
126  }
127  else if (input.IsType<RectilinearHandleType>())
128  {
129  using T = typename RectilinearHandleType::ValueType;
130  using S = typename RectilinearHandleType::StorageTag;
131  const vtkm::cont::internal::detail::StorageVirtual* storage =
132  input.GetStorage().GetStorageVirtual();
133  const auto* castStorage =
134  storage->Cast<vtkm::cont::internal::detail::StorageVirtualImpl<T, S>>();
135 
136  return ArrayRangeCompute(castStorage->GetHandle(), device);
137  }
138  else
139  {
140  return detail::ArrayRangeComputeImpl(input, device);
141  }
142 }
144 #endif //VTKM_NO_DEPRECATED_VIRTUAL
145 
146 template <typename ArrayHandleType>
148  const ArrayHandleType& input,
149  vtkm::cont::DeviceAdapterId device = vtkm::cont::DeviceAdapterTagAny{})
150 {
151  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
152  return detail::ArrayRangeComputeImpl(input, device);
153 }
154 }
155 } // namespace vtkm::cont
156 
157 #endif //vtk_m_cont_ArrayRangeComputeTemplate_h
VTKM_LOG_SCOPE
#define VTKM_LOG_SCOPE(level,...)
Definition: Logging.h:257
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:497
vtkm::cont::ArrayHandle< T, S >
vtkm
VTKM_NO_DEPRECATED_VIRTUAL.
Definition: Algorithms.h:18
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:514
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::ArrayHandleCartesianProduct
ArrayHandleCartesianProduct is a specialization of ArrayHandle.
Definition: ArrayHandleCartesianProduct.h:311
vtkm::cont::ArrayHandle::GetStorage
VTKM_CONT StorageType GetStorage() const
Get the storage.
Definition: ArrayHandle.h:430
VTKM_DEPRECATED_SUPPRESS_END
#define VTKM_DEPRECATED_SUPPRESS_END
Definition: Deprecated.h:123
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:172
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:133
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:487
vtkm::cont::DeviceAdapterAlgorithm
Struct containing device adapter algorithms.
Definition: DeviceAdapterAlgorithm.h:41
VTKM_DEPRECATED_SUPPRESS_BEGIN
#define VTKM_DEPRECATED_SUPPRESS_BEGIN
Definition: Deprecated.h:122
BinaryOperators.h
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::cont::ArrayHandleUniformPointCoordinates
ArrayHandleUniformPointCoordinates is a specialization of ArrayHandle.
Definition: ArrayHandleUniformPointCoordinates.h:45
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,...
ArrayHandleVirtual.h
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