/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_sobol32.h Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_sobol32.h Source File#

API library: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_sobol32.h Source File
rocrand_sobol32.h
1 // Copyright (c) 2017-2025 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCRAND_SOBOL32_H_
22 #define ROCRAND_SOBOL32_H_
23 
24 #include <hip/hip_runtime.h>
25 
26 namespace rocrand_device
27 {
28 
29 template<bool UseSharedVectors>
30 struct sobol32_state
31 {
32  unsigned int d;
33  unsigned int i;
34  unsigned int vectors[32];
35 
36  __forceinline__ __device__ __host__
37  sobol32_state()
38  : d(), i(), vectors()
39  {}
40 
41  __forceinline__ __device__ __host__
42  sobol32_state(const unsigned int d, const unsigned int i, const unsigned int* vectors)
43  : d(d), i(i)
44  {
45  for(int k = 0; k < 32; k++)
46  {
47  this->vectors[k] = vectors[k];
48  }
49  }
50 };
51 
52 template<>
53 struct sobol32_state<true>
54 {
55  unsigned int d;
56  unsigned int i;
57  const unsigned int* vectors;
58 
59  __forceinline__ __device__ __host__
60  sobol32_state()
61  : d(), i(), vectors()
62  {}
63 
64  __forceinline__ __device__ __host__
65  sobol32_state(const unsigned int d, const unsigned int i, const unsigned int* vectors)
66  : d(d), i(i), vectors(vectors)
67  {}
68 };
69 
70 template<bool UseSharedVectors>
71 class sobol32_engine
72 {
73 public:
74  typedef struct sobol32_state<UseSharedVectors> sobol32_state;
75 
76  __forceinline__ __device__ __host__
77  sobol32_engine()
78  {}
79 
80  __forceinline__ __device__ __host__
81  sobol32_engine(const unsigned int* vectors, const unsigned int offset)
82  : m_state(0, 0, vectors)
83  {
84  discard_state(offset);
85  }
86 
88  __forceinline__ __device__ __host__
89  void discard(unsigned int offset)
90  {
91  discard_state(offset);
92  }
93 
94  __forceinline__ __device__ __host__
95  void discard()
96  {
97  discard_state();
98  }
99 
101  __forceinline__ __device__ __host__
102  void discard_stride(unsigned int stride)
103  {
104  discard_state_power2(stride);
105  }
106 
107  __forceinline__ __device__ __host__
108  unsigned int operator()()
109  {
110  return this->next();
111  }
112 
113  __forceinline__ __device__ __host__
114  unsigned int next()
115  {
116  unsigned int p = m_state.d;
117  discard_state();
118  return p;
119  }
120 
121  __forceinline__ __device__ __host__
122  unsigned int current() const
123  {
124  return m_state.d;
125  }
126 
127  __forceinline__ __device__ __host__
128  static constexpr bool uses_shared_vectors()
129  {
130  return UseSharedVectors;
131  }
132 
133 protected:
134  // Advances the internal state by offset times.
135  __forceinline__ __device__ __host__
136  void discard_state(unsigned int offset)
137  {
138  m_state.i += offset;
139  const unsigned int g = m_state.i ^ (m_state.i >> 1);
140  m_state.d = 0;
141  for(int i = 0; i < 32; i++)
142  {
143  m_state.d ^= (g & (1U << i) ? m_state.vectors[i] : 0);
144  }
145  }
146 
147  // Advances the internal state to the next state
148  __forceinline__ __device__ __host__
149  void discard_state()
150  {
151  m_state.d ^= m_state.vectors[rightmost_zero_bit(m_state.i)];
152  m_state.i++;
153  }
154 
155  __forceinline__ __device__ __host__
156  void discard_state_power2(unsigned int stride)
157  {
158  // Leap frog
159  //
160  // T Bradley, J Toit, M Giles, R Tong, P Woodhams
161  // Parallelisation Techniques for Random Number Generators
162  // GPU Computing Gems, 2011
163  //
164  // For power of 2 jumps only 2 bits in Gray code change values
165  // All bits lower than log2(stride) flip 2, 4... times, i.e.
166  // do not change their values.
167 
168  // log2(stride) bit
169  m_state.d ^= m_state.vectors[rightmost_zero_bit(~stride) - 1];
170  // the rightmost zero bit of i, not including the lower log2(stride) bits
171  m_state.d ^= m_state.vectors[rightmost_zero_bit(m_state.i | (stride - 1))];
172  m_state.i += stride;
173  }
174 
175  // Returns the index of the rightmost zero bit in the binary expansion of
176  // x (Gray code of the current element's index)
177  // If x has no zero bit (x == 0xFFFFFFFF), returns 31: advancing past the end of
178  // the 2^32 period wraps the index to 0, which flips bit 31 of the Gray code.
179  __forceinline__ __device__ __host__
180  unsigned int rightmost_zero_bit(unsigned int x)
181  {
182 #if defined(__HIP_DEVICE_COMPILE__)
183  unsigned int z = __ffs(~x);
184  return z ? z - 1 : 31;
185 #else
186  if(x == 0xFFFFFFFFu)
187  return 31;
188  if(x == 0)
189  return 0;
190  unsigned int y = x;
191  unsigned int z = 1;
192  while(y & 1)
193  {
194  y >>= 1;
195  z++;
196  }
197  return z - 1;
198 #endif
199  }
200 
201 protected:
202  // State
203  sobol32_state m_state;
204 
205 }; // sobol32_engine class
206 
207 } // end namespace rocrand_device
208 
215 typedef rocrand_device::sobol32_engine<false> rocrand_state_sobol32;
217 
228 __forceinline__ __device__ __host__
229 void rocrand_init(const unsigned int* vectors,
230  const unsigned int offset,
231  rocrand_state_sobol32* state)
232 {
233  *state = rocrand_state_sobol32(vectors, offset);
234 }
235 
248 __forceinline__ __device__ __host__
249 unsigned int rocrand(rocrand_state_sobol32* state)
250 {
251  return state->next();
252 }
253 
262 __forceinline__ __device__ __host__
263 void skipahead(unsigned long long offset, rocrand_state_sobol32* state)
264 {
265  return state->discard(offset);
266 }
267  // end of group rocranddevice
269 
270 #endif // ROCRAND_SOBOL32_H_
__forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_sobol32 *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_sobol32.h:249
__forceinline__ __device__ __host__ void rocrand_init(const unsigned int *vectors, const unsigned int offset, rocrand_state_sobol32 *state)
Initialize SOBOL32 state.
Definition: rocrand_sobol32.h:229
__forceinline__ __device__ __host__ void skipahead(unsigned long long offset, rocrand_state_sobol32 *state)
Updates SOBOL32 state to skip ahead by offset elements.
Definition: rocrand_sobol32.h:263