OpenQMC API
Loading...
Searching...
No Matches
stochastic.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Contributors to the OpenQMC Project.
3
11
12#pragma once
13
14#include "lookup.h"
15#include "pcg.h"
16
17#include <cassert>
18#include <cstdint>
19
20namespace oqmc
21{
22
32inline void stochasticPmjInit(int nsamples, std::uint32_t table[][4])
33{
34 [[maybe_unused]] constexpr auto maxIndexSize =
35 0x10000; // 2^16 index upper limit.
36
37 assert(nsamples >= 1);
38 assert(nsamples <= maxIndexSize);
39
40 // clang-format off
41 constexpr std::uint16_t pmjXors[2][16] = {
42 {
43 0b0000000000000000,
44 0b0000000000000000,
45 0b0000000000000010,
46 0b0000000000000110,
47 0b0000000000000110,
48 0b0000000000001110,
49 0b0000000000110110,
50 0b0000000001001110,
51 0b0000000000010110,
52 0b0000000000101110,
53 0b0000001001110110,
54 0b0000011011001110,
55 0b0000011100010110,
56 0b0000110000101110,
57 0b0011000001110110,
58 0b0100000011001110,
59 },
60
61 {
62 0b0000000000000000,
63 0b0000000000000001,
64 0b0000000000000011,
65 0b0000000000000011,
66 0b0000000000000111,
67 0b0000000000011011,
68 0b0000000000100111,
69 0b0000000000001011,
70 0b0000000000010111,
71 0b0000000100111011,
72 0b0000001101100111,
73 0b0000001110001011,
74 0b0000011000010111,
75 0b0001100000111011,
76 0b0010000001100111,
77 0b0000000010001011,
78 },
79 };
80 // clang-format on
81
82 const auto buffer = new std::uint32_t[nsamples][2];
83
84 auto state = pcg::init();
85
86 for(int k = 0; k < 2; ++k)
87 {
88 buffer[0][k] = pcg::rng(state);
89 }
90
91 for(int prevLen = 1, logN = 0; prevLen < nsamples; prevLen *= 2, ++logN)
92 {
93 for(int i1 = 0, i2 = prevLen; i1 < prevLen && i2 < nsamples; ++i1, ++i2)
94 {
95 for(int k = 0; k < 2; ++k)
96 {
97 const auto swapBit = 0x80000000u >> logN;
98 const auto bitMask = swapBit - 1;
99
100 const auto j = i1 ^ pmjXors[k][logN];
101
102 const auto prevStratum = buffer[j][k] & ~bitMask;
103 const auto nextStratum = prevStratum ^ swapBit;
104
106 }
107 }
108 }
109
110 for(int i = 0; i < nsamples; ++i)
111 {
114 }
115
116 delete[] buffer;
117}
118
119} // namespace oqmc
constexpr std::uint32_t hash(std::uint32_t key)
Compute a hash value based on an input key.
Definition pcg.h:143
constexpr std::uint32_t init()
Default initialise the PRNG state.
Definition pcg.h:117
constexpr std::uint32_t rng(std::uint32_t &state)
Compute a random number from the PRNG sequence.
Definition pcg.h:162
EncodeKey decodeBits16(std::uint16_t value)
Decode a value back into a key.
Definition encode.h:81
void stochasticPmjInit(int nsamples, std::uint32_t table[][4])
Initialise a table with a progressive mult-jittered (0,2) sequence.
Definition stochastic.h:32