VTK-m  2.2
ArrayHandleRandomUniformReal.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_count_ArrayHandleRandomUniformReal_h
11 #define vtk_m_count_ArrayHandleRandomUniformReal_h
12 
15 
16 namespace vtkm
17 {
18 namespace cont
19 {
20 
21 namespace detail
22 {
23 template <typename Real>
24 struct CanonicalFunctor;
25 
26 template <>
27 struct CanonicalFunctor<vtkm::Float64>
28 {
31  // We take 53 bits (number of bits in mantissa in a double) from the 64 bits random source
32  // and divide it by (1 << 53).
33  static constexpr vtkm::Float64 DIVISOR = static_cast<vtkm::Float64>(vtkm::UInt64{ 1 } << 53);
34  static constexpr vtkm::UInt64 MASK = (vtkm::UInt64{ 1 } << 53) - vtkm::UInt64{ 1 };
35 
37  vtkm::Float64 operator()(vtkm::UInt64 bits) const { return (bits & MASK) / DIVISOR; }
38 };
39 
40 template <>
41 struct CanonicalFunctor<vtkm::Float32>
42 {
43  // We take 24 bits (number of bits in mantissa in a double) from the 64 bits random source
44  // and divide it by (1 << 24).
45  static constexpr vtkm::Float32 DIVISOR = static_cast<vtkm::Float32>(vtkm::UInt32{ 1 } << 24);
46  static constexpr vtkm::UInt32 MASK = (vtkm::UInt32{ 1 } << 24) - vtkm::UInt32{ 1 };
47 
49  vtkm::Float32 operator()(vtkm::UInt64 bits) const { return (bits & MASK) / DIVISOR; }
50 };
51 } // detail
52 
65 template <typename Real = vtkm::Float64>
67  : public vtkm::cont::ArrayHandleTransform<vtkm::cont::ArrayHandleRandomUniformBits,
68  detail::CanonicalFunctor<Real>>
69 {
70 public:
72 
77  detail::CanonicalFunctor<Real>>));
78 
86  explicit ArrayHandleRandomUniformReal(vtkm::Id length, SeedType seed = { std::random_device{}() })
87  : Superclass(vtkm::cont::ArrayHandleRandomUniformBits{ length, seed },
88  detail::CanonicalFunctor<Real>{})
89  {
90  }
91 };
92 
93 } // cont
94 } // vtkm
95 #endif //vtk_m_count_ArrayHandleRandomUniformReal_h
ArrayHandleRandomUniformBits.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
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::ArrayHandleRandomUniformReal::ArrayHandleRandomUniformReal
ArrayHandleRandomUniformReal(vtkm::Id length, SeedType seed={ std::random_device{}() })
Construct an ArrayHandleRandomUniformReal.
Definition: ArrayHandleRandomUniformReal.h:86
ArrayHandleTransform.h
vtkm::cont::ArrayHandleRandomUniformReal
An ArrayHandle that provides a source of random numbers with uniform distribution.
Definition: ArrayHandleRandomUniformReal.h:66
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::ArrayHandleTransform
Implicitly transform values of one array to another with a functor.
Definition: ArrayHandleTransform.h:447
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:185
vtkm::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:157
vtkm::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::cont::ArrayHandleRandomUniformBits
An ArrayHandle that provides a source of random bits.
Definition: ArrayHandleRandomUniformBits.h:71