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

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

API library: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/docs-6.3.1/library/include/rocrand/rocrand_scrambled_sobol32.h Source File
rocrand_scrambled_sobol32.h
1 // Copyright (c) 2022-2024 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_SCRAMBLED_SOBOL32_H_
22 #define ROCRAND_SCRAMBLED_SOBOL32_H_
23 
24 #include "rocrand/rocrand_common.h"
25 #include "rocrand/rocrand_sobol32.h"
26 
27 namespace rocrand_device
28 {
29 
30 template<bool UseSharedVectors>
31 class scrambled_sobol32_engine
32 {
33 public:
34  __forceinline__ __device__ __host__ scrambled_sobol32_engine() : scramble_constant() {}
35 
36  __forceinline__ __device__ __host__
37  scrambled_sobol32_engine(const unsigned int* vectors,
38  const unsigned int scramble_constant,
39  const unsigned int offset)
40  : m_engine(vectors, 0), scramble_constant(scramble_constant)
41  {
42  discard(offset);
43  }
44 
46  __forceinline__ __device__ __host__ void discard(unsigned int offset)
47  {
48  m_engine.discard(offset);
49  }
50 
51  __forceinline__ __device__ __host__ void discard()
52  {
53  m_engine.discard();
54  }
55 
57  __forceinline__ __device__ __host__ void discard_stride(unsigned int stride)
58  {
59  m_engine.discard_stride(stride);
60  }
61 
62  __forceinline__ __device__ __host__ unsigned int operator()()
63  {
64  return this->next();
65  }
66 
67  __forceinline__ __device__ __host__ unsigned int next()
68  {
69  unsigned int p = m_engine.next();
70  return p ^ scramble_constant;
71  }
72 
73  __forceinline__ __device__ __host__ unsigned int current()
74  {
75  unsigned int p = m_engine.current();
76  return p ^ scramble_constant;
77  }
78 
79  __forceinline__ __device__ __host__ static constexpr bool uses_shared_vectors()
80  {
81  return UseSharedVectors;
82  }
83 
84 protected:
85  // Underlying sobol32 engine
86  sobol32_engine<UseSharedVectors> m_engine;
87  // scrambling constant
88  unsigned int scramble_constant;
89 
90 }; // scrambled_sobol32_engine class
91 
92 } // end namespace rocrand_device
93 
100 typedef rocrand_device::scrambled_sobol32_engine<false> rocrand_state_scrambled_sobol32;
102 
114 __forceinline__ __device__ __host__ void rocrand_init(const unsigned int* vectors,
115  const unsigned int scramble_constant,
116  const unsigned int offset,
117  rocrand_state_scrambled_sobol32* state)
118 {
119  *state = rocrand_state_scrambled_sobol32(vectors, scramble_constant, offset);
120 }
121 
134 __forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_scrambled_sobol32* state)
135 {
136  return state->next();
137 }
138 
147 __forceinline__ __device__ __host__ void skipahead(unsigned long long offset,
148  rocrand_state_scrambled_sobol32* state)
149 {
150  return state->discard(offset);
151 }
152  // end of group rocranddevice
154 
155 #endif // ROCRAND_SCRAMBLED_SOBOL32_H_
__forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_scrambled_sobol32 *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_scrambled_sobol32.h:134
__forceinline__ __device__ __host__ void rocrand_init(const unsigned int *vectors, const unsigned int scramble_constant, const unsigned int offset, rocrand_state_scrambled_sobol32 *state)
Initialize scrambled_sobol32 state.
Definition: rocrand_scrambled_sobol32.h:114
__forceinline__ __device__ __host__ void skipahead(unsigned long long offset, rocrand_state_scrambled_sobol32 *state)
Updates SCRAMBLED_SOBOL32 state to skip ahead by offset elements.
Definition: rocrand_scrambled_sobol32.h:147