VTK-m  2.2
ArrayHandleRandomUniformBits.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_cont_ArrayHandleRandomUniformBits_h
11 #define vtk_m_cont_ArrayHandleRandomUniformBits_h
12 
13 #include <random>
15 #include <vtkm/random/Philox.h>
16 
17 namespace vtkm
18 {
19 namespace cont
20 {
21 
22 namespace detail
23 {
24 struct PhiloxFunctor
25 {
26  using SeedType = vtkm::Vec<vtkm::UInt32, 1>;
27 
28  PhiloxFunctor() = default;
29 
30  explicit PhiloxFunctor(SeedType seed)
31  : Seed(seed)
32  {
33  }
34 
36  vtkm::UInt64 operator()(vtkm::Id index) const
37  {
38  using philox_functor = vtkm::random::PhiloxFunctor2x32x10;
39  using counters_type = typename philox_functor::counters_type;
40 
41  auto idx = static_cast<vtkm::UInt64>(index);
42  counters_type counters{ static_cast<vtkm::UInt32>(idx), static_cast<vtkm::UInt32>(idx >> 32) };
43  counters_type result = philox_functor{}(counters, Seed);
44  return static_cast<vtkm::UInt64>(result[0]) | static_cast<vtkm::UInt64>(result[1]) << 32;
45  }
46 
47 private:
48  // This is logically a const, however, this make the Functor non-copyable which is required
49  // by VTKm infrastructure (e.g. ArrayHandleTransform.)
50  SeedType Seed;
51 }; // class PhiloxFunctor
52 } // namespace detail
53 
72  : public vtkm::cont::ArrayHandleImplicit<detail::PhiloxFunctor>
73 {
74 public:
76 
79 
87  explicit ArrayHandleRandomUniformBits(vtkm::Id length, SeedType seed = { std::random_device{}() })
88  : Superclass(detail::PhiloxFunctor(seed), length)
89  {
90  }
91 }; // class ArrayHandleRandomUniformBits
92 }
93 } // namespace vtkm::cont
94 
95 //=============================================================================
96 // Specializations of serialization related classes
98 
99 namespace vtkm
100 {
101 namespace cont
102 {
103 }
104 } // namespace vtkm::cont
106 #endif //vtk_m_cont_ArrayHandleRandomUniformBits_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ARRAY_HANDLE_SUBCLASS_NT
#define VTKM_ARRAY_HANDLE_SUBCLASS_NT(classname, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:266
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
Philox.h
vtkm::cont::ArrayHandleRandomUniformBits::ArrayHandleRandomUniformBits
ArrayHandleRandomUniformBits(vtkm::Id length, SeedType seed={ std::random_device{}() })
Construct an ArrayHandleRandomUniformBits.
Definition: ArrayHandleRandomUniformBits.h:87
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::random::PhiloxFunctor2x32x10
detail::philox_functor< vtkm::UInt32, 2, 10, 0xD256D193, 0x9E3779B9 > PhiloxFunctor2x32x10
Definition: Philox.h:120
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::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
ArrayHandleImplicit.h
vtkm::cont::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:179
vtkm::cont::ArrayHandleRandomUniformBits
An ArrayHandle that provides a source of random bits.
Definition: ArrayHandleRandomUniformBits.h:71