Viskores  1.0
ArrayHandleConstant.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_ArrayHandleConstant_h
19 #define viskores_cont_ArrayHandleConstant_h
20 
23 
25 
26 #include <viskores/Range.h>
27 #include <viskores/VecFlat.h>
29 
30 namespace viskores
31 {
32 namespace cont
33 {
34 
35 struct VISKORES_ALWAYS_EXPORT StorageTagConstant
36 {
37 };
38 
39 namespace internal
40 {
41 
42 template <typename ValueType>
43 struct VISKORES_ALWAYS_EXPORT ConstantFunctor
44 {
46  ConstantFunctor(const ValueType& value = ValueType())
47  : Value(value)
48  {
49  }
50 
52  ValueType operator()(viskores::Id viskoresNotUsed(index)) const { return this->Value; }
53 
54 private:
55  ValueType Value;
56 };
57 
58 template <typename T>
59 using StorageTagConstantSuperclass =
61 
62 template <typename T>
63 struct Storage<T, viskores::cont::StorageTagConstant> : Storage<T, StorageTagConstantSuperclass<T>>
64 {
65 };
66 
67 } // namespace internal
68 
77 template <typename T>
79  : public viskores::cont::ArrayHandle<T, viskores::cont::StorageTagConstant>
80 {
81 public:
86 
89  ArrayHandleConstant(T value, viskores::Id numberOfValues = 0)
90  : Superclass(internal::FunctorToArrayHandleImplicitBuffers(internal::ConstantFunctor<T>(value),
91  numberOfValues))
92  {
93  }
94 
99  VISKORES_CONT T GetValue() const { return this->ReadPortal().GetFunctor()(0); }
100 };
101 
104 template <typename T>
106  viskores::Id numberOfValues)
107 {
108  return viskores::cont::ArrayHandleConstant<T>(value, numberOfValues);
109 }
110 
111 namespace internal
112 {
113 
114 template <>
115 struct VISKORES_CONT_EXPORT ArrayExtractComponentImpl<viskores::cont::StorageTagConstant>
116 {
117  template <typename T>
118  VISKORES_CONT auto operator()(
120  viskores::IdComponent componentIndex,
121  viskores::CopyFlag allowCopy) const
122  {
123  if (allowCopy != viskores::CopyFlag::On)
124  {
126  "Cannot extract component of ArrayHandleConstant without copying. "
127  "(However, the whole array does not need to be copied.)");
128  }
129 
131 
132  viskores::VecFlat<T> vecValue{ srcArray.GetValue() };
133 
134  // Make a basic array with one entry (the constant value).
135  auto basicArray = viskores::cont::make_ArrayHandle({ vecValue[componentIndex] });
136 
137  // Set up a modulo = 1 so all indices go to this one value.
138  return viskores::cont::make_ArrayHandleStride(basicArray, src.GetNumberOfValues(), 1, 0, 1, 1);
139  }
140 };
141 
142 template <typename S>
143 struct ArrayRangeComputeImpl;
144 
145 template <>
146 struct VISKORES_CONT_EXPORT ArrayRangeComputeImpl<viskores::cont::StorageTagConstant>
147 {
148  template <typename T>
152  bool computeFiniteRange,
154  {
155  bool allMasked = false;
156  if (maskArray.GetNumberOfValues() != 0)
157  {
158  // Find if there is atleast one value that is not masked
159  auto ids = GetFirstAndLastUnmaskedIndices(maskArray, devId);
160  allMasked = (ids[1] < ids[0]);
161  }
162 
163  auto value = viskores::make_VecFlat(input.ReadPortal().Get(0));
164 
166  result.Allocate(value.GetNumberOfComponents());
167  auto resultPortal = result.WritePortal();
168  for (viskores::IdComponent index = 0; index < value.GetNumberOfComponents(); ++index)
169  {
170  auto comp = static_cast<viskores::Float64>(value[index]);
171  if (allMasked || (computeFiniteRange && !viskores::IsFinite(comp)))
172  {
173  resultPortal.Set(index, viskores::Range{});
174  }
175  else
176  {
177  resultPortal.Set(index, viskores::Range{ comp, comp });
178  }
179  }
180  return result;
181  }
182 };
183 
184 template <typename S>
185 struct ArrayRangeComputeMagnitudeImpl;
186 
187 template <>
188 struct VISKORES_CONT_EXPORT ArrayRangeComputeMagnitudeImpl<viskores::cont::StorageTagConstant>
189 {
190  template <typename T>
191  VISKORES_CONT viskores::Range operator()(
194  bool computeFiniteRange,
196  {
197  if (maskArray.GetNumberOfValues() != 0)
198  {
199  // Find if there is atleast one value that is not masked
200  auto ids = GetFirstAndLastUnmaskedIndices(maskArray, devId);
201  if (ids[1] < ids[0])
202  {
203  return viskores::Range{};
204  }
205  }
206 
207  auto value = input.ReadPortal().Get(0);
209  return (computeFiniteRange && !viskores::IsFinite(rangeValue))
210  ? viskores::Range{}
211  : viskores::Range{ rangeValue, rangeValue };
212  }
213 };
214 
215 } // namespace internal
216 
217 }
218 } // viskores::cont
219 
220 //=============================================================================
221 // Specializations of serialization related classes
223 namespace viskores
224 {
225 namespace cont
226 {
227 
228 template <typename T>
229 struct SerializableTypeString<viskores::cont::ArrayHandleConstant<T>>
230 {
231  static VISKORES_CONT const std::string& Get()
232  {
233  static std::string name = "AH_Constant<" + SerializableTypeString<T>::Get() + ">";
234  return name;
235  }
236 };
237 
238 template <typename T>
239 struct SerializableTypeString<viskores::cont::ArrayHandle<T, viskores::cont::StorageTagConstant>>
240  : SerializableTypeString<viskores::cont::ArrayHandleConstant<T>>
241 {
242 };
243 }
244 } // viskores::cont
245 
246 namespace mangled_diy_namespace
247 {
248 
249 template <typename T>
250 struct Serialization<viskores::cont::ArrayHandleConstant<T>>
251 {
252 private:
255 
256 public:
257  static VISKORES_CONT void save(BinaryBuffer& bb, const BaseType& obj)
258  {
259  viskoresdiy::save(bb, obj.GetNumberOfValues());
260  viskoresdiy::save(bb, obj.ReadPortal().Get(0));
261  }
262 
263  static VISKORES_CONT void load(BinaryBuffer& bb, BaseType& obj)
264  {
265  viskores::Id count = 0;
266  viskoresdiy::load(bb, count);
267 
268  T value;
269  viskoresdiy::load(bb, value);
270 
271  obj = viskores::cont::make_ArrayHandleConstant(value, count);
272  }
273 };
274 
275 template <typename T>
276 struct Serialization<viskores::cont::ArrayHandle<T, viskores::cont::StorageTagConstant>>
277  : Serialization<viskores::cont::ArrayHandleConstant<T>>
278 {
279 };
280 
281 } // diy
283 
284 #endif //viskores_cont_ArrayHandleConstant_h
viskores::exec::arg::load
T load(const U &u, viskores::Id v)
Definition: FetchTagArrayDirectIn.h:44
VecFlat.h
viskores::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:240
ArrayExtractComponent.h
viskores::cont::ArrayHandle< T, viskores::cont::StorageTagConstant >::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:447
viskores::cont::ArrayHandleConstant::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(viskores::cont::ArrayHandle< T, viskores::cont::StorageTagConstant >) >::type Superclass
Definition: ArrayHandleConstant.h:85
viskores::cont::StorageTagConstant
Definition: ArrayHandleConstant.h:35
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
viskores::cont::make_ArrayHandleConstant
viskores::cont::ArrayHandleConstant< T > make_ArrayHandleConstant(T value, viskores::Id numberOfValues)
make_ArrayHandleConstant is convenience function to generate an ArrayHandleImplicit.
Definition: ArrayHandleConstant.h:105
mangled_diy_namespace
Definition: Particle.h:373
viskores::cont::ArrayHandleConstant::ArrayHandleConstant
ArrayHandleConstant(T value, viskores::Id numberOfValues=0)
Construct a constant array containing the given value.
Definition: ArrayHandleConstant.h:89
VectorAnalysis.h
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::cont::ArrayHandleConstant::GetValue
T GetValue() const
Returns the constant value stored in this array.
Definition: ArrayHandleConstant.h:99
viskores::CopyFlag::On
@ On
viskores::cont::ArrayHandleConstant
An array handle with a constant value.
Definition: ArrayHandleConstant.h:78
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::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:468
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::cont::ErrorBadValue
This class is thrown when a Viskores function or method encounters an invalid value that inhibits pro...
Definition: ErrorBadValue.h:33
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::Magnitude
detail::FloatingPointReturnType< T >::Type Magnitude(const T &x)
Returns the magnitude of a vector.
Definition: VectorAnalysis.h:108
viskores::cont::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:186
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
viskores::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:25
viskores::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:169
ArrayRangeComputeUtils.h
viskores::cont::make_ArrayHandleStride
viskores::cont::ArrayHandleStride< T > make_ArrayHandleStride(const viskores::cont::ArrayHandle< T, viskores::cont::StorageTagBasic > &array, viskores::Id numValues, viskores::Id stride, viskores::Id offset, viskores::Id modulo=0, viskores::Id divisor=1)
Create an array by adding a stride to a basic array.
Definition: ArrayHandleStride.h:433
viskores::cont::make_ArrayHandle
viskores::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, viskores::Id numberOfValues, viskores::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:285