Viskores  1.0
ArrayHandleRandomUniformReal.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_count_ArrayHandleRandomUniformReal_h
19 #define viskores_count_ArrayHandleRandomUniformReal_h
20 
23 
24 namespace viskores
25 {
26 namespace cont
27 {
28 
29 namespace detail
30 {
31 template <typename Real>
32 struct CanonicalFunctor;
33 
34 template <>
35 struct CanonicalFunctor<viskores::Float64>
36 {
39  // We take 53 bits (number of bits in mantissa in a double) from the 64 bits random source
40  // and divide it by (1 << 53).
41  static constexpr viskores::Float64 DIVISOR =
42  static_cast<viskores::Float64>(viskores::UInt64{ 1 } << 53);
43  static constexpr viskores::UInt64 MASK = (viskores::UInt64{ 1 } << 53) - viskores::UInt64{ 1 };
44 
46  viskores::Float64 operator()(viskores::UInt64 bits) const { return (bits & MASK) / DIVISOR; }
47 };
48 
49 template <>
50 struct CanonicalFunctor<viskores::Float32>
51 {
52  // We take 24 bits (number of bits in mantissa in a double) from the 64 bits random source
53  // and divide it by (1 << 24).
54  static constexpr viskores::Float32 DIVISOR =
55  static_cast<viskores::Float32>(viskores::UInt32{ 1 } << 24);
56  static constexpr viskores::UInt32 MASK = (viskores::UInt32{ 1 } << 24) - viskores::UInt32{ 1 };
57 
59  viskores::Float32 operator()(viskores::UInt64 bits) const { return (bits & MASK) / DIVISOR; }
60 };
61 } // detail
62 
75 template <typename Real = viskores::Float64>
76 class VISKORES_ALWAYS_EXPORT ArrayHandleRandomUniformReal
77  : public viskores::cont::ArrayHandleTransform<viskores::cont::ArrayHandleRandomUniformBits,
78  detail::CanonicalFunctor<Real>>
79 {
80 public:
82 
87  detail::CanonicalFunctor<Real>>));
88 
97  SeedType seed = { std::random_device{}() })
98  : Superclass(viskores::cont::ArrayHandleRandomUniformBits{ length, seed },
99  detail::CanonicalFunctor<Real>{})
100  {
101  }
102 };
103 
104 } // cont
105 } // viskores
106 #endif //viskores_count_ArrayHandleRandomUniformReal_h
ArrayHandleRandomUniformBits.h
ArrayHandleTransform.h
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:165
viskores::cont::ArrayHandleRandomUniformBits
An ArrayHandle that provides a source of random bits.
Definition: ArrayHandleRandomUniformBits.h:81
viskores::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:215
viskores::cont::ArrayHandleRandomUniformReal
An ArrayHandle that provides a source of random numbers with uniform distribution.
Definition: ArrayHandleRandomUniformReal.h:76
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::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:169
viskores::cont::ArrayHandleTransform
Implicitly transform values of one array to another with a functor.
Definition: ArrayHandleTransform.h:461
viskores::Vec
A short fixed-length array.
Definition: Types.h:365
viskores::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:193
viskores::cont::ArrayHandleRandomUniformReal::ArrayHandleRandomUniformReal
ArrayHandleRandomUniformReal(viskores::Id length, SeedType seed={ std::random_device{}() })
Construct an ArrayHandleRandomUniformReal.
Definition: ArrayHandleRandomUniformReal.h:96