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
10#include <utility>
11
13{
14 /** Philox state for vector generator
15 *
16 * @tparam TCounter Type of the Counter array
17 * @tparam TKey Type of the Key array
18 */
19 template<typename TCounter, typename TKey>
21 {
22 using Counter = TCounter;
23 using Key = TKey;
24
25 /// Counter array
27 /// Key array
29 };
30
31 /** Philox engine generating a vector of numbers
32 *
33 * This engine's operator() will return a vector of numbers corresponding to the full size of its counter.
34 * This is a convenience vs. memory size tradeoff since the user has to deal with the output array
35 * themselves, but the internal state comprises only of a single counter and a key.
36 *
37 * @tparam TParams Basic parameters for the Philox algorithm
38 */
39 template<typename TParams>
40 class PhiloxVector : public PhiloxBaseCommon<TParams, PhiloxVector<TParams>>
41 {
42 public:
44
45 /// Counter type
46 using Counter = typename Base::Counter;
47 /// Key type
48 using Key = typename Base::Key;
49 /// State type
51
52 template<typename TDistributionResultScalar>
54
56
57 protected:
58 /** Get the next array of random numbers and advance internal state
59 *
60 * @return The next array of random numbers
61 */
63 {
64 this->advanceCounter(state.counter);
65 return this->nRounds(state.counter, state.key);
66 }
67
68 /** Skips the next \a offset vectors
69 *
70 * Unlike its counterpart in \a PhiloxSingle, this function advances the state in multiples of the
71 * counter size thus skipping the entire array of numbers.
72 */
73 ALPAKA_FN_HOST_ACC void skip(uint64_t offset)
74 {
75 this->skip4(offset);
76 }
77
78 public:
79 /** Construct a new Philox engine with vector output
80 *
81 * @param seed Set the Philox generator key
82 * @param subsequence Select a subsequence of size 2^64
83 * @param offset Skip \a offset numbers form the start of the subsequence
84 */
85 ALPAKA_FN_HOST_ACC PhiloxVector(uint64_t seed = 0, uint64_t subsequence = 0, uint64_t offset = 0)
86 : state{{0, 0, 0, 0}, {low32Bits(seed), high32Bits(seed)}}
87 {
88 this->skipSubsequence(subsequence);
89 skip(offset);
90 nextVector();
91 }
92
93 /** Get the next vector of random numbers
94 *
95 * @return The next vector of random numbers
96 */
98 {
99 return nextVector();
100 }
101 };
102} // namespace alpaka::rand::engine
static ALPAKA_FN_HOST_ACC auto nRounds(Counter const &counter_in, Key const &key_in) -> Counter
ALPAKA_FN_HOST_ACC PhiloxVector(uint64_t seed=0, uint64_t subsequence=0, uint64_t offset=0)
typename Base::Counter Counter
Counter type.
typename Base::Key Key
Key type.
ALPAKA_FN_HOST_ACC auto operator()()
ALPAKA_FN_HOST_ACC void skip(uint64_t offset)
typename Base::template ResultContainer< TDistributionResultScalar > ResultContainer
ALPAKA_FN_HOST_ACC auto nextVector()
#define ALPAKA_FN_HOST_ACC
Definition Common.hpp:39
The random number generator engine specifics.
ALPAKA_FN_HOST_ACC constexpr auto high32Bits(std::uint64_t const x) -> std::uint32_t
Get high 32 bits of a 64-bit number.
ALPAKA_FN_HOST_ACC constexpr auto low32Bits(std::uint64_t const x) -> std::uint32_t
Get low 32 bits of a 64-bit number.