VTK-m  2.1
ArrayHandleCounting.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_ArrayHandleCounting_h
11 #define vtk_m_cont_ArrayHandleCounting_h
12 
14 
16 
17 #include <vtkm/Range.h>
18 #include <vtkm/TypeTraits.h>
19 #include <vtkm/VecFlat.h>
20 #include <vtkm/VecTraits.h>
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 
28 {
29 };
30 
31 namespace internal
32 {
33 
35 template <class CountingValueType>
36 class VTKM_ALWAYS_EXPORT ArrayPortalCounting
37 {
38  using ComponentType = typename vtkm::VecTraits<CountingValueType>::ComponentType;
39 
40 public:
41  using ValueType = CountingValueType;
42 
44  ArrayPortalCounting()
45  : Start(0)
46  , Step(1)
47  , NumberOfValues(0)
48  {
49  }
50 
52  ArrayPortalCounting(ValueType start, ValueType step, vtkm::Id numValues)
53  : Start(start)
54  , Step(step)
55  , NumberOfValues(numValues)
56  {
57  }
58 
60  ValueType GetStart() const { return this->Start; }
61 
63  ValueType GetStep() const { return this->Step; }
64 
66  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
67 
69  ValueType Get(vtkm::Id index) const
70  {
71  return ValueType(this->Start + this->Step * ValueType(static_cast<ComponentType>(index)));
72  }
73 
74 private:
75  ValueType Start;
76  ValueType Step;
77  vtkm::Id NumberOfValues;
78 };
79 
80 namespace detail
81 {
82 
83 template <typename T>
84 struct CanCountImpl
85 {
86  using VTraits = vtkm::VecTraits<T>;
87  using BaseType = typename VTraits::BaseComponentType;
88  using TTraits = vtkm::TypeTraits<BaseType>;
89  static constexpr bool IsNumeric =
90  !std::is_same<typename TTraits::NumericTag, vtkm::TypeTraitsUnknownTag>::value;
91  static constexpr bool IsBool = std::is_same<BaseType, bool>::value;
92 
93  static constexpr bool value = IsNumeric && !IsBool;
94 };
95 
96 } // namespace detail
97 
98 // Not all types can be counted.
99 template <typename T>
100 struct CanCount
101 {
102  static constexpr bool value = detail::CanCountImpl<T>::value;
103 };
104 
105 template <typename T>
106 using StorageTagCountingSuperclass =
108 
109 template <typename T>
110 struct Storage<T, typename std::enable_if<CanCount<T>::value, vtkm::cont::StorageTagCounting>::type>
111  : Storage<T, StorageTagCountingSuperclass<T>>
112 {
113 };
114 
115 } // namespace internal
116 
120 template <typename CountingValueType>
122  : public vtkm::cont::ArrayHandle<CountingValueType, vtkm::cont::StorageTagCounting>
123 {
124 public:
128 
129  VTKM_CONT
130  ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
131  : Superclass(internal::PortalToArrayHandleImplicitBuffers(
132  internal::ArrayPortalCounting<CountingValueType>(start, step, length)))
133  {
134  }
135 
136  VTKM_CONT CountingValueType GetStart() const { return this->ReadPortal().GetStart(); }
137 
138  VTKM_CONT CountingValueType GetStep() const { return this->ReadPortal().GetStep(); }
139 };
140 
143 template <typename CountingValueType>
145 make_ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
146 {
147  return vtkm::cont::ArrayHandleCounting<CountingValueType>(start, step, length);
148 }
149 
150 namespace internal
151 {
152 
153 template <typename S>
154 struct ArrayRangeComputeImpl;
155 
156 template <>
157 struct VTKM_CONT_EXPORT ArrayRangeComputeImpl<vtkm::cont::StorageTagCounting>
158 {
159  template <typename T>
162  const vtkm::cont::ArrayHandle<vtkm::UInt8>& maskArray,
163  bool vtkmNotUsed(computeFiniteRange), // assume array produces only finite values
164  vtkm::cont::DeviceAdapterId device) const
165  {
166  using Traits = vtkm::VecTraits<vtkm::VecFlat<T>>;
168  result.Allocate(Traits::NUM_COMPONENTS);
169 
170  if (input.GetNumberOfValues() <= 0)
171  {
172  result.Fill(vtkm::Range{});
173  return result;
174  }
175 
176  vtkm::Id2 firstAndLast{ 0, input.GetNumberOfValues() - 1 };
177  if (maskArray.GetNumberOfValues() > 0)
178  {
179  firstAndLast = GetFirstAndLastUnmaskedIndices(maskArray, device);
180  }
181 
182  if (firstAndLast[1] < firstAndLast[0])
183  {
184  result.Fill(vtkm::Range{});
185  return result;
186  }
187 
188  auto portal = result.WritePortal();
189  // assume the values to be finite
190  auto first = make_VecFlat(input.ReadPortal().Get(firstAndLast[0]));
191  auto last = make_VecFlat(input.ReadPortal().Get(firstAndLast[1]));
192  for (vtkm::IdComponent cIndex = 0; cIndex < Traits::NUM_COMPONENTS; ++cIndex)
193  {
194  auto firstComponent = Traits::GetComponent(first, cIndex);
195  auto lastComponent = Traits::GetComponent(last, cIndex);
196  portal.Set(cIndex,
197  vtkm::Range(vtkm::Min(firstComponent, lastComponent),
198  vtkm::Max(firstComponent, lastComponent)));
199  }
200 
201  return result;
202  }
203 };
204 
205 } // namespace internal
206 
207 }
208 } // namespace vtkm::cont
209 
210 //=============================================================================
211 // Specializations of serialization related classes
213 namespace vtkm
214 {
215 namespace cont
216 {
217 
218 template <typename T>
219 struct SerializableTypeString<vtkm::cont::ArrayHandleCounting<T>>
220 {
221  static VTKM_CONT const std::string& Get()
222  {
223  static std::string name = "AH_Counting<" + SerializableTypeString<T>::Get() + ">";
224  return name;
225  }
226 };
227 
228 template <typename T>
229 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagCounting>>
230  : SerializableTypeString<vtkm::cont::ArrayHandleCounting<T>>
231 {
232 };
233 }
234 } // vtkm::cont
235 
236 namespace mangled_diy_namespace
237 {
238 
239 template <typename T>
240 struct Serialization<vtkm::cont::ArrayHandleCounting<T>>
241 {
242 private:
245 
246 public:
247  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
248  {
249  auto portal = obj.ReadPortal();
250  vtkmdiy::save(bb, portal.GetStart());
251  vtkmdiy::save(bb, portal.GetStep());
252  vtkmdiy::save(bb, portal.GetNumberOfValues());
253  }
254 
255  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
256  {
257  T start{}, step{};
258  vtkm::Id count = 0;
259 
260  vtkmdiy::load(bb, start);
261  vtkmdiy::load(bb, step);
262  vtkmdiy::load(bb, count);
263 
264  obj = vtkm::cont::make_ArrayHandleCounting(start, step, count);
265  }
266 };
267 
268 template <typename T>
269 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagCounting>>
270  : Serialization<vtkm::cont::ArrayHandleCounting<T>>
271 {
272 };
273 } // diy
275 
276 #endif //vtk_m_cont_ArrayHandleCounting_h
VecFlat.h
vtkm::make_VecFlat
vtkm::VecFlat< T > make_VecFlat(const T &vec)
Converts a Vec-like object to a VecFlat.
Definition: VecFlat.h:281
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
vtkm::exec::arg::load
T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in VTKm (an...
Definition: TypeTraits.h:61
vtkm::Get
auto Get(const vtkm::Tuple< Ts... > &tuple)
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:81
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:243
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::cont::StorageTagImplicit
An implementation for read-only implicit arrays.
Definition: ArrayHandleImplicit.h:86
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandle::Fill
void Fill(const ValueType &fillValue, vtkm::Id startIndex, vtkm::Id endIndex, vtkm::cont::Token &token) const
Fills the array with a given value.
Definition: ArrayHandle.h:552
vtkm::cont::ArrayHandleCounting::GetStart
CountingValueType GetStart() const
Definition: ArrayHandleCounting.h:136
vtkm::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:71
vtkm::cont::ArrayHandle::GetNumberOfValues
vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:466
vtkm::cont::make_ArrayHandleCounting
vtkm::cont::ArrayHandleCounting< CountingValueType > make_ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
A convenience function for creating an ArrayHandleCounting.
Definition: ArrayHandleCounting.h:145
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::ArrayHandleCounting::ArrayHandleCounting
ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
Definition: ArrayHandleCounting.h:130
vtkm::VecTraits::BaseComponentType
T BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:78
TypeTraits.h
vtkm::cont::StorageTagCounting
Definition: ArrayHandleCounting.h:27
vtkm::cont::ArrayHandleCounting
ArrayHandleCounting is a specialization of ArrayHandle.
Definition: ArrayHandleCounting.h:121
VTKM_CONT_EXPORT
#define VTKM_CONT_EXPORT
Definition: vtkm_cont_export.h:44
Range.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::cont::ArrayHandleCounting::GetStep
CountingValueType GetStep() const
Definition: ArrayHandleCounting.h:138
vtkm::cont::ArrayHandle< CountingValueType, vtkm::cont::StorageTagCounting >::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:431
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::Vec< vtkm::Id, 2 >
vtkm::cont::ArrayHandle::Allocate
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:488
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
ArrayHandleImplicit.h
ArrayRangeComputeUtils.h
vtkm::cont::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:452
vtkm::cont::ArrayHandleCounting::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< CountingValueType, StorageTagCounting >) >::type Superclass
Definition: ArrayHandleCounting.h:127
VecTraits.h
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31