VTK-m  2.2
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 
57  ArrayPortalCounting(ValueType start, ValueType step, vtkm::Id numValues)
58  : Start(start)
59  , Step(step)
60  , NumberOfValues(numValues)
61  {
62  }
63 
66  ValueType GetStart() const { return this->Start; }
67 
70  ValueType GetStep() const { return this->Step; }
71 
74  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
75 
78  ValueType Get(vtkm::Id index) const
79  {
80  return ValueType(this->Start + this->Step * ValueType(static_cast<ComponentType>(index)));
81  }
82 
83 private:
84  ValueType Start;
85  ValueType Step;
86  vtkm::Id NumberOfValues;
87 };
88 
89 namespace detail
90 {
91 
92 template <typename T>
93 struct CanCountImpl
94 {
95  using VTraits = vtkm::VecTraits<T>;
96  using BaseType = typename VTraits::BaseComponentType;
97  using TTraits = vtkm::TypeTraits<BaseType>;
98  static constexpr bool IsNumeric =
99  !std::is_same<typename TTraits::NumericTag, vtkm::TypeTraitsUnknownTag>::value;
100  static constexpr bool IsBool = std::is_same<BaseType, bool>::value;
101 
102  static constexpr bool value = IsNumeric && !IsBool;
103 };
104 
105 } // namespace detail
106 
107 // Not all types can be counted.
108 template <typename T>
109 struct CanCount
110 {
111  static constexpr bool value = detail::CanCountImpl<T>::value;
112 };
113 
114 template <typename T>
115 using StorageTagCountingSuperclass =
117 
118 template <typename T>
119 struct Storage<T, typename std::enable_if<CanCount<T>::value, vtkm::cont::StorageTagCounting>::type>
120  : Storage<T, StorageTagCountingSuperclass<T>>
121 {
122 };
123 
124 } // namespace internal
125 
129 template <typename CountingValueType>
131  : public vtkm::cont::ArrayHandle<CountingValueType, vtkm::cont::StorageTagCounting>
132 {
133 public:
137 
138  VTKM_CONT
139  ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
140  : Superclass(internal::PortalToArrayHandleImplicitBuffers(
141  internal::ArrayPortalCounting<CountingValueType>(start, step, length)))
142  {
143  }
144 
145  VTKM_CONT CountingValueType GetStart() const { return this->ReadPortal().GetStart(); }
146 
147  VTKM_CONT CountingValueType GetStep() const { return this->ReadPortal().GetStep(); }
148 };
149 
152 template <typename CountingValueType>
154 make_ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
155 {
156  return vtkm::cont::ArrayHandleCounting<CountingValueType>(start, step, length);
157 }
158 
159 namespace internal
160 {
161 
162 template <typename S>
163 struct ArrayRangeComputeImpl;
164 
165 template <>
166 struct VTKM_CONT_EXPORT ArrayRangeComputeImpl<vtkm::cont::StorageTagCounting>
167 {
168  template <typename T>
171  const vtkm::cont::ArrayHandle<vtkm::UInt8>& maskArray,
172  bool vtkmNotUsed(computeFiniteRange), // assume array produces only finite values
173  vtkm::cont::DeviceAdapterId device) const
174  {
175  using Traits = vtkm::VecTraits<vtkm::VecFlat<T>>;
177  result.Allocate(Traits::NUM_COMPONENTS);
178 
179  if (input.GetNumberOfValues() <= 0)
180  {
181  result.Fill(vtkm::Range{});
182  return result;
183  }
184 
185  vtkm::Id2 firstAndLast{ 0, input.GetNumberOfValues() - 1 };
186  if (maskArray.GetNumberOfValues() > 0)
187  {
188  firstAndLast = GetFirstAndLastUnmaskedIndices(maskArray, device);
189  }
190 
191  if (firstAndLast[1] < firstAndLast[0])
192  {
193  result.Fill(vtkm::Range{});
194  return result;
195  }
196 
197  auto portal = result.WritePortal();
198  // assume the values to be finite
199  auto first = make_VecFlat(input.ReadPortal().Get(firstAndLast[0]));
200  auto last = make_VecFlat(input.ReadPortal().Get(firstAndLast[1]));
201  for (vtkm::IdComponent cIndex = 0; cIndex < Traits::NUM_COMPONENTS; ++cIndex)
202  {
203  auto firstComponent = Traits::GetComponent(first, cIndex);
204  auto lastComponent = Traits::GetComponent(last, cIndex);
205  portal.Set(cIndex,
206  vtkm::Range(vtkm::Min(firstComponent, lastComponent),
207  vtkm::Max(firstComponent, lastComponent)));
208  }
209 
210  return result;
211  }
212 };
213 
214 } // namespace internal
215 
216 }
217 } // namespace vtkm::cont
218 
219 //=============================================================================
220 // Specializations of serialization related classes
222 namespace vtkm
223 {
224 namespace cont
225 {
226 
227 template <typename T>
228 struct SerializableTypeString<vtkm::cont::ArrayHandleCounting<T>>
229 {
230  static VTKM_CONT const std::string& Get()
231  {
232  static std::string name = "AH_Counting<" + SerializableTypeString<T>::Get() + ">";
233  return name;
234  }
235 };
236 
237 template <typename T>
238 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagCounting>>
239  : SerializableTypeString<vtkm::cont::ArrayHandleCounting<T>>
240 {
241 };
242 }
243 } // vtkm::cont
244 
245 namespace mangled_diy_namespace
246 {
247 
248 template <typename T>
249 struct Serialization<vtkm::cont::ArrayHandleCounting<T>>
250 {
251 private:
254 
255 public:
256  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
257  {
258  auto portal = obj.ReadPortal();
259  vtkmdiy::save(bb, portal.GetStart());
260  vtkmdiy::save(bb, portal.GetStep());
261  vtkmdiy::save(bb, portal.GetNumberOfValues());
262  }
263 
264  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
265  {
266  T start{}, step{};
267  vtkm::Id count = 0;
268 
269  vtkmdiy::load(bb, start);
270  vtkmdiy::load(bb, step);
271  vtkmdiy::load(bb, count);
272 
273  obj = vtkm::cont::make_ArrayHandleCounting(start, step, count);
274  }
275 };
276 
277 template <typename T>
278 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagCounting>>
279  : Serialization<vtkm::cont::ArrayHandleCounting<T>>
280 {
281 };
282 } // diy
284 
285 #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:554
vtkm::cont::ArrayHandleCounting::GetStart
CountingValueType GetStart() const
Definition: ArrayHandleCounting.h:145
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:468
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:154
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::ArrayHandleCounting::ArrayHandleCounting
ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
Definition: ArrayHandleCounting.h:139
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:130
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:147
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:433
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:490
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:454
vtkm::cont::ArrayHandleCounting::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< CountingValueType, StorageTagCounting >) >::type Superclass
Definition: ArrayHandleCounting.h:136
VecTraits.h
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31