Viskores  1.0
ArrayHandleRandomUniformBits.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_ArrayHandleRandomUniformBits_h
19 #define viskores_cont_ArrayHandleRandomUniformBits_h
20 
21 #include <random>
23 #include <viskores/random/Philox.h>
24 
25 namespace viskores
26 {
27 namespace cont
28 {
29 
30 namespace detail
31 {
32 struct PhiloxFunctor
33 {
34  using SeedType = viskores::Vec<viskores::UInt32, 1>;
35 
36  PhiloxFunctor() = default;
37 
38  explicit PhiloxFunctor(SeedType seed)
39  : Seed(seed)
40  {
41  }
42 
44  viskores::UInt64 operator()(viskores::Id index) const
45  {
46  using philox_functor = viskores::random::PhiloxFunctor2x32x10;
47  using counters_type = typename philox_functor::counters_type;
48 
49  auto idx = static_cast<viskores::UInt64>(index);
50  counters_type counters{ static_cast<viskores::UInt32>(idx),
51  static_cast<viskores::UInt32>(idx >> 32) };
52  counters_type result = philox_functor{}(counters, Seed);
53  return static_cast<viskores::UInt64>(result[0]) |
54  static_cast<viskores::UInt64>(result[1]) << 32;
55  }
56 
57 private:
58  // This is logically a const, however, this make the Functor non-copyable which is required
59  // by Viskores infrastructure (e.g. ArrayHandleTransform.)
60  SeedType Seed;
61 }; // class PhiloxFunctor
62 } // namespace detail
63 
81 class VISKORES_ALWAYS_EXPORT ArrayHandleRandomUniformBits
82  : public viskores::cont::ArrayHandleImplicit<detail::PhiloxFunctor>
83 {
84 public:
86 
89 
98  SeedType seed = { std::random_device{}() })
99  : Superclass(detail::PhiloxFunctor(seed), length)
100  {
101  }
102 }; // class ArrayHandleRandomUniformBits
103 }
104 } // namespace viskores::cont
105 
106 //=============================================================================
107 // Specializations of serialization related classes
109 
110 namespace viskores
111 {
112 namespace cont
113 {
114 }
115 } // namespace viskores::cont
117 #endif //viskores_cont_ArrayHandleRandomUniformBits_h
Philox.h
viskores::cont::ArrayHandleRandomUniformBits::ArrayHandleRandomUniformBits
ArrayHandleRandomUniformBits(viskores::Id length, SeedType seed={ std::random_device{}() })
Construct an ArrayHandleRandomUniformBits.
Definition: ArrayHandleRandomUniformBits.h:97
VISKORES_ARRAY_HANDLE_SUBCLASS_NT
#define VISKORES_ARRAY_HANDLE_SUBCLASS_NT(classname, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:279
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::random::PhiloxFunctor2x32x10
detail::philox_functor< viskores::UInt32, 2, 10, 0xD256D193, 0x9E3779B9 > PhiloxFunctor2x32x10
Definition: Philox.h:130
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::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::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:186
ArrayHandleImplicit.h
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