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

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_scrambled_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_scrambled_sobol32.h Source File
rocrand_scrambled_sobol32.h
1 // Copyright (c) 2022-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_SCRAMBLED_SOBOL32_H_
22 #define ROCRAND_SCRAMBLED_SOBOL32_H_
23 
24 #include "rocrand/rocrand_sobol32.h"
25 
26 #include <hip/hip_runtime.h>
27 
28 namespace rocrand_device
29 {
30 
31 template<bool UseSharedVectors>
32 class scrambled_sobol32_engine
33 {
34 public:
35  __forceinline__ __device__ __host__ scrambled_sobol32_engine() : scramble_constant() {}
36 
37  __forceinline__ __device__ __host__
38  scrambled_sobol32_engine(const unsigned int* vectors,
39  const unsigned int scramble_constant,
40  const unsigned int offset)
41  : m_engine(vectors, 0), scramble_constant(scramble_constant)
42  {
43  discard(offset);
44  }
45 
47  __forceinline__ __device__ __host__ void discard(unsigned int offset)
48  {
49  m_engine.discard(offset);
50  }
51 
52  __forceinline__ __device__ __host__ void discard()
53  {
54  m_engine.discard();
55  }
56 
58  __forceinline__ __device__ __host__ void discard_stride(unsigned int stride)
59  {
60  m_engine.discard_stride(stride);
61  }
62 
63  __forceinline__ __device__ __host__ unsigned int operator()()
64  {
65  return this->next();
66  }
67 
68  __forceinline__ __device__ __host__ unsigned int next()
69  {
70  unsigned int p = m_engine.next();
71  return p ^ scramble_constant;
72  }
73 
74  __forceinline__ __device__ __host__ unsigned int current()
75  {
76  unsigned int p = m_engine.current();
77  return p ^ scramble_constant;
78  }
79 
80  __forceinline__ __device__ __host__ static constexpr bool uses_shared_vectors()
81  {
82  return UseSharedVectors;
83  }
84 
85 protected:
86  // Underlying sobol32 engine
87  sobol32_engine<UseSharedVectors> m_engine;
88  // scrambling constant
89  unsigned int scramble_constant;
90 
91 }; // scrambled_sobol32_engine class
92 
93 } // end namespace rocrand_device
94 
101 typedef rocrand_device::scrambled_sobol32_engine<false> rocrand_state_scrambled_sobol32;
103 
115 __forceinline__ __device__ __host__
116 void rocrand_init(const unsigned int* vectors,
117  const unsigned int scramble_constant,
118  const unsigned int offset,
119  rocrand_state_scrambled_sobol32* state)
120 {
121  *state = rocrand_state_scrambled_sobol32(vectors, scramble_constant, offset);
122 }
123 
136 __forceinline__ __device__ __host__
137 unsigned int rocrand(rocrand_state_scrambled_sobol32* state)
138 {
139  return state->next();
140 }
141 
150 __forceinline__ __device__ __host__
151 void skipahead(unsigned long long offset, rocrand_state_scrambled_sobol32* state)
152 {
153  return state->discard(offset);
154 }
155  // end of group rocranddevice
157 
158 #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:137
__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:116
__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:151