Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca-rng.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
59#ifndef CA_RNG_H
60#define CA_RNG_H
61
62#include <cstddef>
63#include <cstdint>
64#include <random>
65#include <type_traits>
66
67#include <ca-traits.H>
68
69namespace Aleph {
70namespace CA {
71
82[[nodiscard]] inline constexpr std::uint64_t splitmix64(std::uint64_t x) noexcept
83{
84 x += 0x9e3779b97f4a7c15ull;
85 x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ull;
86 x = (x ^ (x >> 27)) * 0x94d049bb133111ebull;
87 return x ^ (x >> 31);
88}
89
97[[nodiscard]] inline constexpr std::uint64_t mix_seed(const std::uint64_t a,
98 const std::uint64_t b) noexcept
99{
100 return splitmix64(splitmix64(a + 0x9e3779b97f4a7c15ull) ^ (b + 0xbf58476d1ce4e5b9ull));
101}
102
114template <std::size_t Rank>
115[[nodiscard]] inline constexpr std::uint64_t cell_key_from_coord(const Coord_Vec<Rank> &c) noexcept
116{
117 std::uint64_t h = 0xcbf29ce484222325ull; // FNV offset basis.
118 for (std::size_t d = 0; d < Rank; ++d)
119 {
120 const std::uint64_t component = static_cast<std::uint64_t>(static_cast<std::int64_t>(c[d]));
121 h = splitmix64(h ^ component);
122 }
123 return h;
124}
125
140template <std::size_t Rank>
141[[nodiscard]] inline constexpr std::uint64_t cell_seed(const std::uint64_t master_seed,
142 const std::size_t step,
143 const Coord_Vec<Rank> &coord) noexcept
144{
145 const std::uint64_t step_hash
146 = splitmix64(static_cast<std::uint64_t>(step) + 0xa5a5a5a5a5a5a5a5ull);
147 const std::uint64_t coord_hash = cell_key_from_coord<Rank>(coord);
148 return splitmix64(master_seed ^ step_hash ^ coord_hash);
149}
150
159template <std::size_t Rank>
160[[nodiscard]] inline constexpr std::uint64_t cell_seed(const std::uint64_t master_seed,
161 const Cell_Context<Rank> &ctx) noexcept
162{
163 return cell_seed<Rank>(master_seed, ctx.step, ctx.coord);
164}
165
180[[nodiscard]] inline constexpr std::uint64_t thread_step_seed(const std::uint64_t master_seed,
181 const std::size_t thread_id,
182 const std::size_t step) noexcept
183{
184 const std::uint64_t tid_hash
185 = splitmix64(static_cast<std::uint64_t>(thread_id) + 0xfeedfacecafebee0ull);
186 const std::uint64_t step_hash
187 = splitmix64((static_cast<std::uint64_t>(step) << 32) ^ 0x0123456789abcdefull);
188 return splitmix64(master_seed ^ tid_hash ^ step_hash);
189}
190
205template <typename Engine>
206[[nodiscard]] inline double uniform_unit(Engine &eng)
207{
208 static_assert(std::is_unsigned_v<typename Engine::result_type>,
209 "uniform_unit requires an unsigned result_type");
210 const std::uint64_t v = static_cast<std::uint64_t>(eng());
211 // 2^53 = 9007199254740992. The shift drops the noisy low 11 bits
212 // and leaves a value in [0, 2^53) that maps exactly to [0, 1).
213 return static_cast<double>(v >> 11) * (1.0 / 9007199254740992.0);
214}
215
230template <typename Engine>
231[[nodiscard]] inline std::size_t uniform_int(Engine &eng, const std::size_t lo, const std::size_t hi)
232{
233 if (hi <= lo)
234 return lo;
235 const double u = uniform_unit(eng);
236 const std::size_t span = hi - lo + 1;
237 std::size_t pick = lo + static_cast<std::size_t>(u * static_cast<double>(span));
238 if (pick > hi)
239 pick = hi;
240 return pick;
241}
242
272template <typename Engine = std::mt19937_64>
274{
275 std::uint64_t master_;
276
277public:
281 using result_type = typename Engine::result_type;
282
288 explicit constexpr Per_Thread_RNG(const std::uint64_t seed = 0) noexcept : master_(seed) {}
289
294 [[nodiscard]] constexpr std::uint64_t master_seed() const noexcept
295 {
296 return master_;
297 }
298
311 template <std::size_t Rank>
312 [[nodiscard]] Engine for_cell(const std::size_t step, const Coord_Vec<Rank> &coord) const
313 {
314 return Engine{static_cast<result_type>(cell_seed<Rank>(master_, step, coord))};
315 }
316
324 template <std::size_t Rank>
326 {
327 return for_cell<Rank>(ctx.step, ctx.coord);
328 }
329
342 [[nodiscard]] Engine for_thread_step(const std::size_t thread_id, const std::size_t step) const
343 {
344 return Engine{static_cast<result_type>(thread_step_seed(master_, thread_id, step))};
345 }
346};
347
348} // namespace CA
349} // namespace Aleph
350
351#endif // CA_RNG_H
long double h
Definition btreepic.C:154
Common typedefs and tag types for the Cellular Automata module.
Master seed dispenser for stochastic CA rules.
Definition ca-rng.H:274
typename Engine::result_type result_type
Result type of engine_type.
Definition ca-rng.H:281
std::uint64_t master_
Definition ca-rng.H:275
Engine for_cell(const Cell_Context< Rank > &ctx) const
Build a fresh deterministic engine from a Cell_Context.
Definition ca-rng.H:325
constexpr std::uint64_t master_seed() const noexcept
Return the master seed.
Definition ca-rng.H:294
Engine engine_type
Underlying engine type.
Definition ca-rng.H:279
Engine for_cell(const std::size_t step, const Coord_Vec< Rank > &coord) const
Build a fresh deterministic engine for cell (step, coord).
Definition ca-rng.H:312
constexpr Per_Thread_RNG(const std::uint64_t seed=0) noexcept
Build a dispenser around seed.
Definition ca-rng.H:288
Engine for_thread_step(const std::size_t thread_id, const std::size_t step) const
Build an engine for a (thread, step) sub-stream.
Definition ca-rng.H:342
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
constexpr std::uint64_t mix_seed(const std::uint64_t a, const std::uint64_t b) noexcept
Combine two 64-bit values into a single deterministic hash.
Definition ca-rng.H:97
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
constexpr std::uint64_t thread_step_seed(const std::uint64_t master_seed, const std::size_t thread_id, const std::size_t step) noexcept
Build a deterministic seed from (master, thread_id, step).
Definition ca-rng.H:180
double uniform_unit(Engine &eng)
Map a 64-bit RNG output to a uniform value in [0, 1).
Definition ca-rng.H:206
constexpr std::uint64_t cell_key_from_coord(const Coord_Vec< Rank > &c) noexcept
Hash a cell coordinate into a stable 64-bit key.
Definition ca-rng.H:115
std::size_t uniform_int(Engine &eng, const std::size_t lo, const std::size_t hi)
Sample a uniform integer in [lo, hi] (inclusive).
Definition ca-rng.H:231
constexpr std::uint64_t splitmix64(std::uint64_t x) noexcept
64-bit SplitMix hash.
Definition ca-rng.H:82
constexpr std::uint64_t cell_seed(const std::uint64_t master_seed, const std::size_t step, const Coord_Vec< Rank > &coord) noexcept
Build a deterministic per-cell seed.
Definition ca-rng.H:141
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Definition ca-traits.H:106
std::size_t step
Index of the step that is currently being computed (0-based).
Definition ca-traits.H:108
Coord_Vec< Rank > coord
Cell coordinate inside the current frame.
Definition ca-traits.H:110
ValueArg< size_t > seed
Definition testHash.C:53