Viskores  1.0
ArrayHandleCounting.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_ArrayHandleCounting_h
19 #define viskores_cont_ArrayHandleCounting_h
20 
22 
24 
25 #include <viskores/Range.h>
26 #include <viskores/TypeTraits.h>
27 #include <viskores/VecFlat.h>
28 #include <viskores/VecTraits.h>
29 
30 namespace viskores
31 {
32 namespace cont
33 {
34 
35 struct VISKORES_ALWAYS_EXPORT StorageTagCounting
36 {
37 };
38 
39 namespace internal
40 {
41 
43 template <class CountingValueType>
44 class VISKORES_ALWAYS_EXPORT ArrayPortalCounting
45 {
46  using ComponentType = typename viskores::VecTraits<CountingValueType>::ComponentType;
47 
48 public:
49  using ValueType = CountingValueType;
50 
52  ArrayPortalCounting()
53  : Start(0)
54  , Step(1)
55  , NumberOfValues(0)
56  {
57  }
58 
65  ArrayPortalCounting(ValueType start, ValueType step, viskores::Id numValues)
66  : Start(start)
67  , Step(step)
68  , NumberOfValues(numValues)
69  {
70  }
71 
74  ValueType GetStart() const { return this->Start; }
75 
78  ValueType GetStep() const { return this->Step; }
79 
82  viskores::Id GetNumberOfValues() const { return this->NumberOfValues; }
83 
86  ValueType Get(viskores::Id index) const
87  {
88  return ValueType(this->Start + this->Step * ValueType(static_cast<ComponentType>(index)));
89  }
90 
91 private:
92  ValueType Start;
93  ValueType Step;
94  viskores::Id NumberOfValues;
95 };
96 
97 namespace detail
98 {
99 
100 template <typename T>
101 struct CanCountImpl
102 {
103  using VTraits = viskores::VecTraits<T>;
104  using BaseType = typename VTraits::BaseComponentType;
105  using TTraits = viskores::TypeTraits<BaseType>;
106  static constexpr bool IsNumeric =
107  !std::is_same<typename TTraits::NumericTag, viskores::TypeTraitsUnknownTag>::value;
108  static constexpr bool IsBool = std::is_same<BaseType, bool>::value;
109 
110  static constexpr bool value = IsNumeric && !IsBool;
111 };
112 
113 } // namespace detail
114 
115 // Not all types can be counted.
116 template <typename T>
117 struct CanCount
118 {
119  static constexpr bool value = detail::CanCountImpl<T>::value;
120 };
121 
122 template <typename T>
123 using StorageTagCountingSuperclass =
125 
126 template <typename T>
127 struct Storage<
128  T,
129  typename std::enable_if<CanCount<T>::value, viskores::cont::StorageTagCounting>::type>
130  : Storage<T, StorageTagCountingSuperclass<T>>
131 {
132 };
133 
134 } // namespace internal
135 
139 template <typename CountingValueType>
141  : public viskores::cont::ArrayHandle<CountingValueType, viskores::cont::StorageTagCounting>
142 {
143 public:
148 
150  ArrayHandleCounting(CountingValueType start, CountingValueType step, viskores::Id length)
151  : Superclass(internal::PortalToArrayHandleImplicitBuffers(
152  internal::ArrayPortalCounting<CountingValueType>(start, step, length)))
153  {
154  }
155 
156  VISKORES_CONT CountingValueType GetStart() const { return this->ReadPortal().GetStart(); }
157 
158  VISKORES_CONT CountingValueType GetStep() const { return this->ReadPortal().GetStep(); }
159 };
160 
163 template <typename CountingValueType>
165 make_ArrayHandleCounting(CountingValueType start, CountingValueType step, viskores::Id length)
166 {
167  return viskores::cont::ArrayHandleCounting<CountingValueType>(start, step, length);
168 }
169 
170 namespace internal
171 {
172 
173 template <typename S>
174 struct ArrayRangeComputeImpl;
175 
176 template <>
177 struct VISKORES_CONT_EXPORT ArrayRangeComputeImpl<viskores::cont::StorageTagCounting>
178 {
179  template <typename T>
183  bool viskoresNotUsed(computeFiniteRange), // assume array produces only finite values
184  viskores::cont::DeviceAdapterId device) const
185  {
188  result.Allocate(Traits::NUM_COMPONENTS);
189 
190  if (input.GetNumberOfValues() <= 0)
191  {
192  result.Fill(viskores::Range{});
193  return result;
194  }
195 
196  viskores::Id2 firstAndLast{ 0, input.GetNumberOfValues() - 1 };
197  if (maskArray.GetNumberOfValues() > 0)
198  {
199  firstAndLast = GetFirstAndLastUnmaskedIndices(maskArray, device);
200  }
201 
202  if (firstAndLast[1] < firstAndLast[0])
203  {
204  result.Fill(viskores::Range{});
205  return result;
206  }
207 
208  auto portal = result.WritePortal();
209  // assume the values to be finite
210  auto first = make_VecFlat(input.ReadPortal().Get(firstAndLast[0]));
211  auto last = make_VecFlat(input.ReadPortal().Get(firstAndLast[1]));
212  for (viskores::IdComponent cIndex = 0; cIndex < Traits::NUM_COMPONENTS; ++cIndex)
213  {
214  auto firstComponent = Traits::GetComponent(first, cIndex);
215  auto lastComponent = Traits::GetComponent(last, cIndex);
216  portal.Set(cIndex,
217  viskores::Range(viskores::Min(firstComponent, lastComponent),
218  viskores::Max(firstComponent, lastComponent)));
219  }
220 
221  return result;
222  }
223 };
224 
225 } // namespace internal
226 
227 }
228 } // namespace viskores::cont
229 
230 //=============================================================================
231 // Specializations of serialization related classes
233 namespace viskores
234 {
235 namespace cont
236 {
237 
238 template <typename T>
239 struct SerializableTypeString<viskores::cont::ArrayHandleCounting<T>>
240 {
241  static VISKORES_CONT const std::string& Get()
242  {
243  static std::string name = "AH_Counting<" + SerializableTypeString<T>::Get() + ">";
244  return name;
245  }
246 };
247 
248 template <typename T>
249 struct SerializableTypeString<viskores::cont::ArrayHandle<T, viskores::cont::StorageTagCounting>>
250  : SerializableTypeString<viskores::cont::ArrayHandleCounting<T>>
251 {
252 };
253 }
254 } // viskores::cont
255 
256 namespace mangled_diy_namespace
257 {
258 
259 template <typename T>
260 struct Serialization<viskores::cont::ArrayHandleCounting<T>>
261 {
262 private:
265 
266 public:
267  static VISKORES_CONT void save(BinaryBuffer& bb, const BaseType& obj)
268  {
269  auto portal = obj.ReadPortal();
270  viskoresdiy::save(bb, portal.GetStart());
271  viskoresdiy::save(bb, portal.GetStep());
272  viskoresdiy::save(bb, portal.GetNumberOfValues());
273  }
274 
275  static VISKORES_CONT void load(BinaryBuffer& bb, BaseType& obj)
276  {
277  T start{}, step{};
278  viskores::Id count = 0;
279 
280  viskoresdiy::load(bb, start);
281  viskoresdiy::load(bb, step);
282  viskoresdiy::load(bb, count);
283 
284  obj = viskores::cont::make_ArrayHandleCounting(start, step, count);
285  }
286 };
287 
288 template <typename T>
289 struct Serialization<viskores::cont::ArrayHandle<T, viskores::cont::StorageTagCounting>>
290  : Serialization<viskores::cont::ArrayHandleCounting<T>>
291 {
292 };
293 } // diy
295 
296 #endif //viskores_cont_ArrayHandleCounting_h
viskores::exec::arg::load
T load(const U &u, viskores::Id v)
Definition: FetchTagArrayDirectIn.h:44
VecFlat.h
viskores::cont::ArrayHandle< CountingValueType, viskores::cont::StorageTagCounting >::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:447
viskores::cont::ArrayHandleCounting::GetStep
CountingValueType GetStep() const
Definition: ArrayHandleCounting.h:158
viskoresNotUsed
#define viskoresNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:136
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
mangled_diy_namespace
Definition: Particle.h:373
TypeTraits.h
viskores::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in Viskores...
Definition: TypeTraits.h:69
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores::cont::make_ArrayHandleCounting
viskores::cont::ArrayHandleCounting< CountingValueType > make_ArrayHandleCounting(CountingValueType start, CountingValueType step, viskores::Id length)
A convenience function for creating an ArrayHandleCounting.
Definition: ArrayHandleCounting.h:165
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::cont::ArrayHandle::Allocate
void Allocate(viskores::Id numberOfValues, viskores::CopyFlag preserve, viskores::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:504
Range.h
viskores::cont::ArrayHandle::GetNumberOfValues
viskores::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:482
viskores::cont::ArrayHandleCounting
ArrayHandleCounting is a specialization of ArrayHandle.
Definition: ArrayHandleCounting.h:140
viskores::cont::ArrayHandleCounting::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(viskores::cont::ArrayHandle< CountingValueType, StorageTagCounting >) >::type Superclass
Definition: ArrayHandleCounting.h:147
viskores::cont::StorageTagImplicit
An implementation for read-only implicit arrays.
Definition: ArrayHandleImplicit.h:94
viskores::cont::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:468
viskores::cont::ArrayHandleCounting::ArrayHandleCounting
ArrayHandleCounting(CountingValueType start, CountingValueType step, viskores::Id length)
Definition: ArrayHandleCounting.h:150
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
viskores::Range
Represent a continuous scalar range of values.
Definition: Range.h:39
viskores::make_VecFlat
viskores::VecFlat< T > make_VecFlat(const T &vec)
Converts a Vec-like object to a VecFlat.
Definition: VecFlat.h:297
VISKORES_ARRAY_HANDLE_SUBCLASS
#define VISKORES_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:256
viskores::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:79
viskores::cont::ArrayHandle::Fill
void Fill(const ValueType &fillValue, viskores::Id startIndex, viskores::Id endIndex, viskores::cont::Token &token) const
Fills the array with a given value.
Definition: ArrayHandle.h:568
viskores::cont::ArrayHandleCounting::GetStart
CountingValueType GetStart() const
Definition: ArrayHandleCounting.h:156
viskores::VecTraits::BaseComponentType
T BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:86
viskores::Get
auto Get(const viskores::Tuple< Ts... > &tuple)
Retrieve the object from a viskores::Tuple at the given index.
Definition: Tuple.h:89
ArrayHandleImplicit.h
ArrayRangeComputeUtils.h
viskores::cont::StorageTagCounting
Definition: ArrayHandleCounting.h:35
viskores::Vec< viskores::Id, 2 >
VecTraits.h