alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
PhiloxVector.hpp
Go to the documentation of this file.
1/* Copyright 2022 Jiri Vyskocil, Bernhard Manfred Gruber
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
9
10namespace alpaka::rand::engine::internal
11{
12 /** Philox engine generating a vector of numbers
13 *
14 * This engine's operator() will return a vector of numbers corresponding to the full size of its counter.
15 * This is a convenience vs. memory size tradeoff since the user has to deal with the output array
16 * themselves, but the internal state comprises only of a single counter and a key.
17 *
18 * @tparam T_Params Basic parameters for the Philox algorithm
19 */
20 template<typename T_Params>
21 class PhiloxVector : public PhiloxBaseCommon<T_Params, PhiloxVector<T_Params>>
22 {
23 public:
24 using Base = PhiloxBaseCommon<T_Params, PhiloxVector<T_Params>>;
25
26 /// Counter type
27 using Counter = typename Base::Counter;
28 /// Key type
29 using Key = typename Base::Key;
30 using State = PhiloxState<Counter, Key, PhiloxVector<T_Params>>;
31 template<typename TDistributionResultScalar>
32 using ResultContainer = typename Base::template ResultContainer<TDistributionResultScalar>;
33
34 protected:
35 /** Get the next array of random numbers and advance internal state
36 *
37 * @return The next array of random numbers
38 */
39 constexpr auto nextVector()
40 {
41 this->advanceCounter(this->state.counter);
42 return this->nRounds(this->state.counter, this->state.key);
43 }
44
45 /** Skips the next \a offset vectors
46 *
47 * Unlike its counterpart in \a PhiloxSingle, this function advances the state in multiples of the
48 * counter size thus skipping the entire array of numbers.
49 */
50 constexpr void skip(uint64_t offset)
51 {
52 this->skip4(offset);
53 }
54
55 public:
56 /** Construct a new Philox engine with vector output
57 *
58 * @param seed Set the Philox generator key
59 * @param subsequence Select a subsequence of size 2^64
60 * @param offset Skip \a offset numbers form the start of the subsequence
61 */
62 constexpr explicit PhiloxVector(uint64_t seed = 0, uint64_t subsequence = 0, uint64_t offset = 0)
63 : Base(State{{0, 0, 0, 0}, {low32Bits(seed), high32Bits(seed)}})
64 {
65 this->skipSubsequence(subsequence);
66 skip(offset);
67 nextVector();
68 }
69
70 /** Get the next vector of random numbers
71 *
72 * @return The next vector of random numbers
73 */
74 constexpr auto operator()()
75 {
76 return nextVector();
77 }
78 };
79} // namespace alpaka::rand::engine::internal