VTK-m  2.0
Philox.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_random_Philox_h
11 #define vtk_m_random_Philox_h
12 
13 #include <vtkm/Types.h>
14 
15 namespace vtkm
16 {
17 namespace random
18 {
19 namespace detail
20 {
22 {
23  vtkm::UInt64 r = static_cast<vtkm::UInt64>(a) * b;
24  auto lo = static_cast<vtkm::UInt32>(r);
25  vtkm::UInt32 hi = static_cast<vtkm::UInt32>(r >> 32);
26  return { lo, hi };
27 }
28 
29 template <typename UIntType, std::size_t N, UIntType... consts>
30 struct philox_parameters;
31 
32 template <typename T, T M0, T C0>
33 struct philox_parameters<T, 2, M0, C0>
34 {
35  static constexpr Vec<T, 1> multipliers = { M0 };
36  static constexpr Vec<T, 1> round_consts = { C0 };
37 };
38 
39 template <typename T, T M0, T C0, T M1, T C1>
40 struct philox_parameters<T, 4, M0, C0, M1, C1>
41 {
42  static constexpr vtkm::Vec<T, 2> multipliers = { M0, M1 };
43  static constexpr vtkm::Vec<T, 2> round_consts = { C0, C1 };
44 };
45 
46 template <typename UIntType, std::size_t N, std::size_t R, UIntType... consts>
47 class philox_functor;
48 
49 template <typename UIntType, std::size_t R, UIntType... consts>
50 class philox_functor<UIntType, 2, R, consts...>
51 {
52 public:
53  using counters_type = vtkm::Vec<UIntType, 2>;
54  using keys_type = vtkm::Vec<UIntType, 1>;
55 
56  VTKM_EXEC_CONT counters_type operator()(counters_type counters, keys_type keys) const
57  {
58  for (std::size_t i = 0; i < R; ++i)
59  {
60  counters = round(counters, keys);
61  keys = bump_keys(keys);
62  }
63  return counters;
64  }
65 
66 private:
67  static VTKM_EXEC_CONT counters_type round(counters_type counters, keys_type round_keys)
68  {
69  auto constexpr multipliers = philox_parameters<UIntType, 2, consts...>::multipliers;
70  vtkm::Vec<UIntType, 2> r = mulhilo(multipliers[0], counters[0]);
71  return { r[1] ^ round_keys[0] ^ counters[1], r[0] };
72  }
73 
74  static VTKM_EXEC_CONT keys_type bump_keys(keys_type keys)
75  {
76  auto constexpr round_consts = philox_parameters<UIntType, 2, consts...>::round_consts;
77  return { keys[0] + round_consts[0] };
78  }
79 };
80 
81 template <typename UIntType, std::size_t R, UIntType... consts>
82 class philox_functor<UIntType, 4, R, consts...>
83 {
84  using counters_type = vtkm::Vec<UIntType, 4>;
85  using keys_type = vtkm::Vec<UIntType, 2>;
86 
87  static VTKM_EXEC_CONT counters_type round(counters_type counters, keys_type round_keys)
88  {
89  auto constexpr multipliers = philox_parameters<UIntType, 4, consts...>::multipliers;
90  vtkm::Vec<UIntType, 2> r0 = mulhilo(multipliers[0], counters[0]);
91  vtkm::Vec<UIntType, 2> r1 = mulhilo(multipliers[1], counters[2]);
92  return {
93  r1[1] ^ round_keys[0] ^ counters[1], r1[0], r0[1] ^ round_keys[1] ^ counters[3], r0[0]
94  };
95  }
96 
97  static VTKM_EXEC_CONT keys_type bump_key(keys_type keys)
98  {
99  auto constexpr round_consts = philox_parameters<UIntType, 4, consts...>::round_consts;
100  keys[0] += round_consts[0];
101  keys[1] += round_consts[1];
102  return keys;
103  }
104 
105 public:
106  VTKM_EXEC_CONT counters_type operator()(counters_type counters, keys_type keys) const
107  {
108  for (std::size_t i = 0; i < R; ++i)
109  {
110  counters = round(counters, keys);
111  keys = bump_key(keys);
112  }
113  return counters;
114  }
115 };
116 
117 } // namespace detail
118 
119 using PhiloxFunctor2x32x7 = detail::philox_functor<vtkm::UInt32, 2, 7, 0xD256D193, 0x9E3779B9>;
120 using PhiloxFunctor2x32x10 = detail::philox_functor<vtkm::UInt32, 2, 10, 0xD256D193, 0x9E3779B9>;
121 
122 } // namespace random
123 } // namespace vtkm
124 #endif //vtk_m_random_Philox_h
vtkm::random::PhiloxFunctor2x32x7
detail::philox_functor< vtkm::UInt32, 2, 7, 0xD256D193, 0x9E3779B9 > PhiloxFunctor2x32x7
Definition: Philox.h:119
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Vec< T, 1 >
Definition: Types.h:837
vtkm::Vec< T, 2 >
Definition: Types.h:859
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