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#include "unused.h"
17
18#include <cassert>
19#include <cstdint>
20
21namespace oqmc
22{
23
33inline void stochasticPmjInit(int nsamples, std::uint32_t table[][4])
34{
35 constexpr auto maxIndexSize = 0x10000; // 2^16 index upper limit.
36 OQMC_MAYBE_UNUSED(maxIndexSize);
37
38 assert(nsamples >= 1);
39 assert(nsamples <= maxIndexSize);
40
41 // clang-format off
42 constexpr std::uint16_t pmjXors[2][16] = {
43 {
44 0b0000000000000000,
45 0b0000000000000000,
46 0b0000000000000010,
47 0b0000000000000110,
48 0b0000000000000110,
49 0b0000000000001110,
50 0b0000000000110110,
51 0b0000000001001110,
52 0b0000000000010110,
53 0b0000000000101110,
54 0b0000001001110110,
55 0b0000011011001110,
56 0b0000011100010110,
57 0b0000110000101110,
58 0b0011000001110110,
59 0b0100000011001110,
60 },
61
62 {
63 0b0000000000000000,
64 0b0000000000000001,
65 0b0000000000000011,
66 0b0000000000000011,
67 0b0000000000000111,
68 0b0000000000011011,
69 0b0000000000100111,
70 0b0000000000001011,
71 0b0000000000010111,
72 0b0000000100111011,
73 0b0000001101100111,
74 0b0000001110001011,
75 0b0000011000010111,
76 0b0001100000111011,
77 0b0010000001100111,
78 0b0000000010001011,
79 },
80 };
81 // clang-format on
82
83 const auto buffer = new std::uint32_t[nsamples][2];
84
85 auto state = pcg::init();
86
87 for(int k = 0; k < 2; ++k)
88 {
89 buffer[0][k] = pcg::rng(state);
90 }
91
92 for(int prevLen = 1, logN = 0; prevLen < nsamples; prevLen *= 2, ++logN)
93 {
94 for(int i1 = 0, i2 = prevLen; i1 < prevLen && i2 < nsamples; ++i1, ++i2)
95 {
96 for(int k = 0; k < 2; ++k)
97 {
98 const auto swapBit = 0x80000000u >> logN;
99 const auto bitMask = swapBit - 1;
100
101 const auto j = i1 ^ pmjXors[k][logN];
102
103 const auto prevStratum = buffer[j][k] & ~bitMask;
104 const auto nextStratum = prevStratum ^ swapBit;
105
107 }
108 }
109 }
110
111 for(int i = 0; i < nsamples; ++i)
112 {
115 }
116
117 delete[] buffer;
118}
119
120} // 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
Definition bntables.h:21
void stochasticPmjInit(int nsamples, std::uint32_t table[][4])
Initialise a table with a progressive mult-jittered (0,2) sequence.
Definition stochastic.h:33
#define OQMC_MAYBE_UNUSED(exp)
Macro to declare a symbol unused.
Definition unused.h:11