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

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

API library: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_philox4x32_10.h Source File
rocrand_philox4x32_10.h
1 // Copyright (c) 2017-2026 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 /*
22 Copyright 2010-2011, D. E. Shaw Research.
23 All rights reserved.
24 
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions are
27 met:
28 
29 * Redistributions of source code must retain the above copyright
30  notice, this list of conditions, and the following disclaimer.
31 
32 * Redistributions in binary form must reproduce the above copyright
33  notice, this list of conditions, and the following disclaimer in the
34  documentation and/or other materials provided with the distribution.
35 
36 * Neither the name of D. E. Shaw Research nor the names of its
37  contributors may be used to endorse or promote products derived from
38  this software without specific prior written permission.
39 
40 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 */
52 
53 #ifndef ROCRAND_PHILOX4X32_10_H_
54 #define ROCRAND_PHILOX4X32_10_H_
55 
56 #include "rocrand/rocrand_common.h"
57 
58 #include <hip/hip_runtime.h>
59 
60 // Constants from Random123
61 // See https://www.deshawresearch.com/resources_random123.html
62 #define ROCRAND_PHILOX_M4x32_0 0xD2511F53U
63 #define ROCRAND_PHILOX_M4x32_1 0xCD9E8D57U
64 #define ROCRAND_PHILOX_W32_0 0x9E3779B9U
65 #define ROCRAND_PHILOX_W32_1 0xBB67AE85U
66 
67 #if defined(__HIP_DEVICE_COMPILE__) && defined(__AMDGCN__)
68  #define ROCRAND_BUILTIN_ADDC(a, b, carry_in, carry_out) \
69  __builtin_addc(a, b, carry_in, carry_out)
70 #else
71  #define ROCRAND_BUILTIN_ADDC(a, b, carry_in, carry_out) \
72  ( \
73  [&]() \
74  { \
75  unsigned int sum = (a) + (b) + (carry_in); \
76  *carry_out = (sum < (a) || ((carry_in) && sum <= (b))) ? 1 : 0; \
77  return sum; \
78  }())
79 #endif
80 
89 #define ROCRAND_PHILOX4x32_DEFAULT_SEED 0xdeadbeefdeadbeefULL // end of group rocranddevice
91 
92 namespace rocrand_device
93 {
94 
95 class philox4x32_10_engine
96 {
97 public:
98  struct philox4x32_10_state
99  {
100  uint4 counter;
101  uint4 result;
102  uint2 key;
103  unsigned int substate;
104 
105 #ifndef ROCRAND_DETAIL_BM_NOT_IN_STATE
106  // The Box–Muller transform requires two inputs to convert uniformly
107  // distributed real values [0; 1] to normally distributed real values
108  // (with mean = 0, and stddev = 1). Often user wants only one
109  // normally distributed number, to save performance and random
110  // numbers the 2nd value is saved for future requests.
111  float boxmuller_float; // normally distributed float
112  double boxmuller_double; // normally distributed double
113 #endif
114  };
115 
116  __forceinline__ __device__ __host__
117  philox4x32_10_engine()
118  {
119  this->seed(ROCRAND_PHILOX4x32_DEFAULT_SEED, 0, 0);
120  }
121 
127  __forceinline__ __device__ __host__
128  philox4x32_10_engine(const unsigned long long seed,
129  const unsigned long long subsequence,
130  const unsigned long long offset)
131  {
132  this->seed(seed, subsequence, offset);
133  }
134 
140  __forceinline__ __device__ __host__
141  void seed(unsigned long long seed_value,
142  const unsigned long long subsequence,
143  const unsigned long long offset)
144  {
145  m_state.key.x = static_cast<unsigned int>(seed_value);
146  m_state.key.y = static_cast<unsigned int>(seed_value >> 32);
147  this->restart(subsequence, offset);
148  }
149 
151  __forceinline__ __device__ __host__
152  void discard(unsigned long long offset)
153  {
154  this->discard_impl(offset);
155  this->m_state.result = this->ten_rounds(m_state.counter, m_state.key);
156  }
157 
162  __forceinline__ __device__ __host__
163  void discard_subsequence(unsigned long long subsequence)
164  {
165  this->discard_subsequence_impl(subsequence);
166  m_state.result = this->ten_rounds(m_state.counter, m_state.key);
167  }
168 
169  __forceinline__ __device__ __host__
170  void restart(const unsigned long long subsequence, const unsigned long long offset)
171  {
172  m_state.counter = {0, 0, 0, 0};
173  m_state.result = {0, 0, 0, 0};
174  m_state.substate = 0;
175 #ifndef ROCRAND_DETAIL_BM_NOT_IN_STATE
176  m_state.boxmuller_float = ROCRAND_NAN_FLOAT;
177  m_state.boxmuller_double = ROCRAND_NAN_DOUBLE;
178 #endif
179  this->discard_subsequence_impl(subsequence);
180  this->discard_impl(offset);
181  m_state.result = this->ten_rounds(m_state.counter, m_state.key);
182  }
183 
184  __forceinline__ __device__ __host__
185  unsigned int operator()()
186  {
187  return this->next();
188  }
189 
190  __forceinline__ __device__ __host__
191  unsigned int next()
192  {
193  unsigned int s = m_state.substate;
194  unsigned int vs[4]
195  = {m_state.result.x, m_state.result.y, m_state.result.z, m_state.result.w};
196  unsigned int ret = vs[s];
197 
198  s++;
199  if(s == 4)
200  {
201  s = 0;
202  this->discard_state();
203  m_state.result = this->ten_rounds(m_state.counter, m_state.key);
204  }
205  m_state.substate = s;
206  return ret;
207  }
208 
209  __forceinline__ __device__ __host__
210  uint4 next4()
211  {
212  uint4 ret = m_state.result;
213  this->discard_state();
214  m_state.result = this->ten_rounds(m_state.counter, m_state.key);
215  return this->interleave(ret, m_state.result);
216  }
217 
218 protected:
219  // Advances the internal state to skip \p offset numbers.
220  // DOES NOT CALCULATE NEW 4 UINTs (m_state.result)
221  __forceinline__ __device__ __host__
222  void discard_impl(unsigned long long offset)
223  {
224  // Adjust offset for subset
225  m_state.substate += offset & 3;
226  unsigned long long counter_offset = offset / 4;
227  counter_offset += m_state.substate < 4 ? 0 : 1;
228  m_state.substate += m_state.substate < 4 ? 0 : -4;
229  // Discard states
230  this->discard_state(counter_offset);
231  }
232 
233  // DOES NOT CALCULATE NEW 4 UINTs (m_state.result)
234  __forceinline__ __device__ __host__
235  void discard_subsequence_impl(unsigned long long subsequence)
236  {
237  const unsigned int lo = static_cast<unsigned int>(subsequence);
238  const unsigned int hi = static_cast<unsigned int>(subsequence >> 32);
239  unsigned int carry = 0;
240 
241  m_state.counter.z = ROCRAND_BUILTIN_ADDC(m_state.counter.z, lo, 0, &carry);
242  m_state.counter.w = ROCRAND_BUILTIN_ADDC(m_state.counter.w, hi, carry, &carry);
243  }
244 
245  // Advances the internal state by offset times.
246  // DOES NOT CALCULATE NEW 4 UINTs (m_state.result)
247  __forceinline__ __device__ __host__
248  void discard_state(unsigned long long offset)
249  {
250  const unsigned int lo = static_cast<unsigned int>(offset);
251  const unsigned int hi = static_cast<unsigned int>(offset >> 32);
252  unsigned int carry = 0;
253 
254  m_state.counter.x = ROCRAND_BUILTIN_ADDC(m_state.counter.x, lo, 0, &carry);
255  m_state.counter.y = ROCRAND_BUILTIN_ADDC(m_state.counter.y, hi, carry, &carry);
256  m_state.counter.z = ROCRAND_BUILTIN_ADDC(m_state.counter.z, 0, carry, &carry);
257  m_state.counter.w = ROCRAND_BUILTIN_ADDC(m_state.counter.w, 0, carry, &carry);
258  }
259 
260  // Advances the internal state to the next state
261  // DOES NOT CALCULATE NEW 4 UINTs (m_state.result)
262  __forceinline__ __device__ __host__
263  void discard_state()
264  {
265  m_state.counter = this->bump_counter(m_state.counter);
266  }
267 
268  __forceinline__ __device__ __host__
269  static uint4 bump_counter(uint4 counter)
270  {
271  unsigned int carry = 0;
272  counter.x = ROCRAND_BUILTIN_ADDC(counter.x, 1, 0, &carry);
273  counter.y = ROCRAND_BUILTIN_ADDC(counter.y, 0, carry, &carry);
274  counter.z = ROCRAND_BUILTIN_ADDC(counter.z, 0, carry, &carry);
275  counter.w = ROCRAND_BUILTIN_ADDC(counter.w, 0, carry, &carry);
276  return counter;
277  }
278 
279  __forceinline__ __device__ __host__
280  uint4 interleave(const uint4 prev, const uint4 next) const
281  {
282  switch(m_state.substate)
283  {
284  case 0: return prev;
285  case 1: return uint4{prev.y, prev.z, prev.w, next.x};
286  case 2: return uint4{prev.z, prev.w, next.x, next.y};
287  case 3: return uint4{prev.w, next.x, next.y, next.z};
288  }
289  __builtin_unreachable();
290  }
291 
292  // 10 Philox4x32 rounds
293  __forceinline__ __device__ __host__
294  uint4 ten_rounds(uint4 counter, uint2 key)
295  {
296  counter = this->single_round(counter, key);
297  key = this->bumpkey(key); // 1
298  counter = this->single_round(counter, key);
299  key = this->bumpkey(key); // 2
300  counter = this->single_round(counter, key);
301  key = this->bumpkey(key); // 3
302  counter = this->single_round(counter, key);
303  key = this->bumpkey(key); // 4
304  counter = this->single_round(counter, key);
305  key = this->bumpkey(key); // 5
306  counter = this->single_round(counter, key);
307  key = this->bumpkey(key); // 6
308  counter = this->single_round(counter, key);
309  key = this->bumpkey(key); // 7
310  counter = this->single_round(counter, key);
311  key = this->bumpkey(key); // 8
312  counter = this->single_round(counter, key);
313  key = this->bumpkey(key); // 9
314  return this->single_round(counter, key); // 10
315  }
316 
317 private:
318  // Single Philox4x32 round
319  __forceinline__ __device__ __host__
320  static uint4 single_round(uint4 counter, uint2 key)
321  {
322  // Source: Random123
323  unsigned long long mul0 = detail::mul_u64_u32(ROCRAND_PHILOX_M4x32_0, counter.x);
324  unsigned int hi0 = static_cast<unsigned int>(mul0 >> 32);
325  unsigned int lo0 = static_cast<unsigned int>(mul0);
326  unsigned long long mul1 = detail::mul_u64_u32(ROCRAND_PHILOX_M4x32_1, counter.z);
327  unsigned int hi1 = static_cast<unsigned int>(mul1 >> 32);
328  unsigned int lo1 = static_cast<unsigned int>(mul1);
329  return uint4{hi1 ^ counter.y ^ key.x, lo1, hi0 ^ counter.w ^ key.y, lo0};
330  }
331 
332  __forceinline__ __device__ __host__
333  static uint2 bumpkey(uint2 key)
334  {
335  key.x += ROCRAND_PHILOX_W32_0;
336  key.y += ROCRAND_PHILOX_W32_1;
337  return key;
338  }
339 
340 protected:
341  // State
342  philox4x32_10_state m_state;
343 
344 #ifndef ROCRAND_DETAIL_BM_NOT_IN_STATE
345  friend struct detail::engine_boxmuller_helper<philox4x32_10_engine>;
346 #endif
347 
348 }; // philox4x32_10_engine class
349 
350 } // end namespace rocrand_device
351 
358 typedef rocrand_device::philox4x32_10_engine rocrand_state_philox4x32_10;
360 
372 __forceinline__ __device__ __host__
373 void rocrand_init(const unsigned long long seed,
374  const unsigned long long subsequence,
375  const unsigned long long offset,
376  rocrand_state_philox4x32_10* state)
377 {
378  *state = rocrand_state_philox4x32_10(seed, subsequence, offset);
379 }
380 
393 __forceinline__ __device__ __host__
394 unsigned int rocrand(rocrand_state_philox4x32_10* state)
395 {
396  return state->next();
397 }
398 
411 __forceinline__ __device__ __host__
412 uint4 rocrand4(rocrand_state_philox4x32_10* state)
413 {
414  return state->next4();
415 }
416 
425 __forceinline__ __device__ __host__
426 void skipahead(unsigned long long offset, rocrand_state_philox4x32_10* state)
427 {
428  return state->discard(offset);
429 }
430 
440 __forceinline__ __device__ __host__
441 void skipahead_subsequence(unsigned long long subsequence, rocrand_state_philox4x32_10* state)
442 {
443  return state->discard_subsequence(subsequence);
444 }
445 
455 __forceinline__ __device__ __host__
456 void skipahead_sequence(unsigned long long sequence, rocrand_state_philox4x32_10* state)
457 {
458  return state->discard_subsequence(sequence);
459 }
460  // end of group rocranddevice
462 
463 #endif // ROCRAND_PHILOX4X32_10_H_
__forceinline__ __device__ __host__ void skipahead(unsigned long long offset, rocrand_state_philox4x32_10 *state)
Updates Philox state to skip ahead by offset elements.
Definition: rocrand_philox4x32_10.h:426
__forceinline__ __device__ __host__ void rocrand_init(const unsigned long long seed, const unsigned long long subsequence, const unsigned long long offset, rocrand_state_philox4x32_10 *state)
Initializes Philox state.
Definition: rocrand_philox4x32_10.h:373
#define ROCRAND_PHILOX4x32_DEFAULT_SEED
Default seed for PHILOX4x32 PRNG.
Definition: rocrand_philox4x32_10.h:89
__forceinline__ __device__ __host__ uint4 rocrand4(rocrand_state_philox4x32_10 *state)
Returns four uniformly distributed random unsigned int values from [0; 2^32 - 1] range.
Definition: rocrand_philox4x32_10.h:412
__forceinline__ __device__ __host__ void skipahead_sequence(unsigned long long sequence, rocrand_state_philox4x32_10 *state)
Updates Philox state to skip ahead by sequence sequences.
Definition: rocrand_philox4x32_10.h:456
__forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_philox4x32_10 *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_philox4x32_10.h:394
__forceinline__ __device__ __host__ void skipahead_subsequence(unsigned long long subsequence, rocrand_state_philox4x32_10 *state)
Updates Philox state to skip ahead by subsequence subsequences.
Definition: rocrand_philox4x32_10.h:441