VTK-m  2.0
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 
82  explicit ArrayHandleRandomUniformBits(vtkm::Id length, SeedType seed = { std::random_device{}() })
83  : Superclass(detail::PhiloxFunctor(seed), length)
84  {
85  }
86 }; // class ArrayHandleRandomUniformBits
87 }
88 } // namespace vtkm::cont
89 
90 //=============================================================================
91 // Specializations of serialization related classes
93 
94 namespace vtkm
95 {
96 namespace cont
97 {
98 }
99 } // namespace vtkm::cont
101 #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:249
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{}() })
The type of seed is specifically designed to be an vtkm::Vec<> to provide type safety for the paramet...
Definition: ArrayHandleRandomUniformBits.h:82
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
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:767
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
ArrayHandleImplicit.h
vtkm::cont::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:174
vtkm::cont::ArrayHandleRandomUniformBits
An ArrayHandle that provides a source of random bits.
Definition: ArrayHandleRandomUniformBits.h:71