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

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

API library: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/latest/library/include/rocrand/rocrand_sobol32.h Source File
API library
rocrand_sobol32.h
1 // Copyright (c) 2017-2022 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 #ifndef FQUALIFIERS
25 #define FQUALIFIERS __forceinline__ __device__
26 #endif // FQUALIFIERS_
27 
28 #include "rocrand/rocrand_common.h"
29 
30 namespace rocrand_device {
31 
32 template<bool UseSharedVectors>
33 struct sobol32_state
34 {
35  unsigned int d;
36  unsigned int i;
37  unsigned int vectors[32];
38 
40  sobol32_state() : d(), i(), vectors() { }
41 
43  sobol32_state(const unsigned int d,
44  const unsigned int i,
45  const unsigned int * vectors)
46  : d(d), i(i)
47  {
48  for(int k = 0; k < 32; k++)
49  {
50  this->vectors[k] = vectors[k];
51  }
52  }
53 };
54 
55 template<>
56 struct sobol32_state<true>
57 {
58  unsigned int d;
59  unsigned int i;
60  const unsigned int * vectors;
61 
63  sobol32_state() : d(), i(), vectors() { }
64 
66  sobol32_state(const unsigned int d,
67  const unsigned int i,
68  const unsigned int * vectors)
69  : d(d), i(i), vectors(vectors) { }
70 };
71 
72 template<bool UseSharedVectors>
73 class sobol32_engine
74 {
75 public:
76 
77  typedef struct sobol32_state<UseSharedVectors> sobol32_state;
78 
80  sobol32_engine() { }
81 
83  sobol32_engine(const unsigned int * vectors,
84  const unsigned int offset)
85  : m_state(0, 0, vectors)
86  {
87  discard_state(offset);
88  }
89 
92  void discard(unsigned int offset)
93  {
94  discard_state(offset);
95  }
96 
98  void discard()
99  {
100  discard_state();
101  }
102 
105  void discard_stride(unsigned int stride)
106  {
107  discard_state_power2(stride);
108  }
109 
111  unsigned int operator()()
112  {
113  return this->next();
114  }
115 
117  unsigned int next()
118  {
119  unsigned int p = m_state.d;
120  discard_state();
121  return p;
122  }
123 
125  unsigned int current() const
126  {
127  return m_state.d;
128  }
129 
130 protected:
131  // Advances the internal state by offset times.
133  void discard_state(unsigned int offset)
134  {
135  m_state.i += offset;
136  const unsigned int g = m_state.i ^ (m_state.i >> 1);
137  m_state.d = 0;
138  for(int i = 0; i < 32; i++)
139  {
140  m_state.d ^= (g & (1U << i) ? m_state.vectors[i] : 0);
141  }
142  }
143 
144  // Advances the internal state to the next state
146  void discard_state()
147  {
148  m_state.d ^= m_state.vectors[rightmost_zero_bit(m_state.i)];
149  m_state.i++;
150  }
151 
153  void discard_state_power2(unsigned int stride)
154  {
155  // Leap frog
156  //
157  // T Bradley, J Toit, M Giles, R Tong, P Woodhams
158  // Parallelisation Techniques for Random Number Generators
159  // GPU Computing Gems, 2011
160  //
161  // For power of 2 jumps only 2 bits in Gray code change values
162  // All bits lower than log2(stride) flip 2, 4... times, i.e.
163  // do not change their values.
164 
165  // log2(stride) bit
166  m_state.d ^= m_state.vectors[rightmost_zero_bit(~stride) - 1];
167  // the rightmost zero bit of i, not including the lower log2(stride) bits
168  m_state.d ^= m_state.vectors[rightmost_zero_bit(m_state.i | (stride - 1))];
169  m_state.i += stride;
170  }
171 
172  // Returns the index of the rightmost zero bit in the binary expansion of
173  // x (Gray code of the current element's index)
175  unsigned int rightmost_zero_bit(unsigned int x)
176  {
177  #if defined(__HIP_DEVICE_COMPILE__)
178  unsigned int z = __ffs(~x);
179  return z ? z - 1 : 0;
180  #else
181  if(x == 0)
182  return 0;
183  unsigned int y = x;
184  unsigned int z = 1;
185  while(y & 1)
186  {
187  y >>= 1;
188  z++;
189  }
190  return z - 1;
191  #endif
192  }
193 
194 protected:
195  // State
196  sobol32_state m_state;
197 
198 }; // sobol32_engine class
199 
200 } // end namespace rocrand_device
201 
208 typedef rocrand_device::sobol32_engine<false> rocrand_state_sobol32;
210 
222 void rocrand_init(const unsigned int * vectors,
223  const unsigned int offset,
224  rocrand_state_sobol32 * state)
225 {
226  *state = rocrand_state_sobol32(vectors, offset);
227 }
228 
242 unsigned int rocrand(rocrand_state_sobol32 * state)
243 {
244  return state->next();
245 }
246 
256 void skipahead(unsigned long long offset, rocrand_state_sobol32 * state)
257 {
258  return state->discard(offset);
259 }
260  // end of group rocranddevice
262 
263 #endif // ROCRAND_SOBOL32_H_
FQUALIFIERS void skipahead(unsigned long long offset, rocrand_state_sobol32 *state)
Updates SOBOL32 state to skip ahead by offset elements.
Definition: rocrand_sobol32.h:256
FQUALIFIERS unsigned int rocrand(rocrand_state_sobol32 *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_sobol32.h:242
FQUALIFIERS void rocrand_init(const unsigned int *vectors, const unsigned int offset, rocrand_state_sobol32 *state)
Initialize SOBOL32 state.
Definition: rocrand_sobol32.h:222
#define FQUALIFIERS
Shorthand for commonly used function qualifiers.
Definition: rocrand_uniform.h:31