18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 05:57:08 +01:00
Files
maszyna/tests/unit/test_random_engine.cpp
jerrrrycho aa9fc1b6f3 perf(physics): the engine of a group of vehicles stepped in parallel is seeded at its first draw
vehicle_table::update_steps_parallel() gives each group of vehicles an engine of Random() of its own,
seeded from the run's engine and the group, so the draws are the same whichever thread takes the
group. It built a std::mt19937 from a std::seed_seq for every group in every step: seed_seq::generate
filling the 624 words of the state was 2.4% of the main thread and most of what the worker threads did
(5263 of their non-waiting samples) on Tomaszewo, where most groups (parked wagons, cars) draw nothing.

The group now gets a lazy_random_engine (utilities/lazyrandom.h) holding the two numbers, which
Random() seeds the same way (std::seed_seq of the two) at the group's first draw. A group that draws
gets the same numbers as before, one that doesn't skips the seeding. Ported from a local branch of
2026-11-28 (perf/parallel-group-random-engine-lazy-seed, on the layout before #531), with its tests:
the lazy engine draws as one seeded up front from the same numbers, is seeded at the first draw only,
and stands ahead of the thread's engine while it's set.

600 s in --test, seed 12345, one run each: Tomaszewo (raj40821) sim 151.5 s against 206.7 s the step
before (-27%); L107 (linia_107_objazdy, binary terrain, gfxrenderer null) sim 20.5 s against 31.1 s.
The physics logs (14 and 9) and the event logs are byte-identical to staging's, unit tests 1207/1207
with exact golden traces.
2026-11-30 08:15:16 +01:00

72 lines
2.2 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "utilities/utilities.h"
#include "utilities/lazyrandom.h"
#include <random>
#include <vector>
namespace
{
// what Random() draws in the calling thread with the engine set before, n times
std::vector<double> draws(int const Count)
{
std::vector<double> result;
for (int i = 0; i < Count; ++i)
result.push_back(Random(0.0, 1.0));
return result;
}
} // namespace
TEST_CASE("random engine: the lazily seeded engine draws as one seeded up front from the same numbers")
{
std::pair<std::uint32_t, std::uint32_t> const seeds[]{{0u, 0u}, {12345u, 7u}, {0xFFFFFFFFu, 4096u}};
for (auto const &seed : seeds)
{
CAPTURE(seed.first);
CAPTURE(seed.second);
std::seed_seq eagerseed{seed.first, seed.second};
std::mt19937 eager{eagerseed}; // NOSONAR: randomness of the simulation, not of anything security-relevant
set_thread_random_engine(&eager);
auto const expected{draws(100)};
set_thread_random_engine(nullptr);
lazy_random_engine lazy;
lazy.seed[0] = seed.first;
lazy.seed[1] = seed.second;
set_thread_lazy_random_engine(&lazy);
auto const actual{draws(100)};
set_thread_lazy_random_engine(nullptr);
CHECK(actual == expected);
}
}
TEST_CASE("random engine: the lazy engine is seeded at the first draw only, and stands ahead of the thread's engine")
{
std::mt19937 threadengine{1u}; // NOSONAR: randomness of the simulation, not of anything security-relevant
auto const untouched{threadengine};
set_thread_random_engine(&threadengine);
lazy_random_engine lazy;
lazy.seed[0] = 3u;
lazy.seed[1] = 5u;
set_thread_lazy_random_engine(&lazy);
CHECK(thread_lazy_random_engine() == &lazy);
// a group that draws nothing never seeds it
CHECK_FALSE(lazy.engine.has_value());
static_cast<void>(Random(0.0, 1.0));
CHECK(lazy.engine.has_value());
// the draw came from the lazy engine, the one of the thread is left as it was
CHECK(threadengine == untouched);
set_thread_lazy_random_engine(nullptr);
CHECK(thread_lazy_random_engine() == nullptr);
// without the lazy engine the draws come from the thread's engine again
static_cast<void>(Random(0.0, 1.0));
CHECK_FALSE(threadengine == untouched);
set_thread_random_engine(nullptr);
}