diff --git a/tests/unit/test_random_engine.cpp b/tests/unit/test_random_engine.cpp new file mode 100644 index 000000000..da673ef4d --- /dev/null +++ b/tests/unit/test_random_engine.cpp @@ -0,0 +1,71 @@ +#include "stdafx.h" + +#include "doctest.h" + +#include "utilities/utilities.h" +#include "utilities/lazyrandom.h" + +#include +#include + +namespace +{ +// what Random() draws in the calling thread with the engine set before, n times +std::vector draws(int const Count) +{ + std::vector 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 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(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(Random(0.0, 1.0)); + CHECK_FALSE(threadengine == untouched); + set_thread_random_engine(nullptr); +} diff --git a/utilities/lazyrandom.h b/utilities/lazyrandom.h new file mode 100644 index 000000000..aed55184b --- /dev/null +++ b/utilities/lazyrandom.h @@ -0,0 +1,30 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +// an engine of Random() seeded from two numbers (std::seed_seq) only when the first draw needs it: seeding a std::mt19937 +// fills its whole state (624 numbers), which took longer than the steps of most of the groups of vehicles it was made for +// in every frame (vehicle_table::update_steps_parallel()), and most of them (parked wagons, cars) draw nothing + +#pragma once + +#include +#include +#include + +struct lazy_random_engine +{ + std::uint32_t seed[2]{}; + std::optional engine; // NOSONAR: randomness of the simulation, not of anything security-relevant +}; + +// the engine Random() draws from in the calling thread ahead of the one of set_thread_random_engine(), or none for that one +// again. its draws are the ones of a std::mt19937 built from std::seed_seq{ seed[0], seed[1] } up front +void set_thread_lazy_random_engine(lazy_random_engine *Engine); +// the engine set for the calling thread by set_thread_lazy_random_engine(), or none +lazy_random_engine *thread_lazy_random_engine(); diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 1adb80a5b..8b7c3868b 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -28,6 +28,7 @@ Copyright (C) 2007-2014 Maciej Cierniak //#endif #include "utilities/utilities.h" +#include "utilities/lazyrandom.h" #include "utilities/Globals.h" #include "utilities/parser.h" #include "utilities/U8.h" @@ -181,13 +182,33 @@ bool ClearFlag(int &Flag, int const Value) namespace { thread_local std::mt19937 *t_random_engine{nullptr}; // NOSONAR: randomness of the simulation, not of anything security-relevant +thread_local lazy_random_engine *t_lazy_random_engine{nullptr}; std::mt19937 &random_engine() // NOSONAR: randomness of the simulation, not of anything security-relevant { + if (t_lazy_random_engine != nullptr) + { + if (false == t_lazy_random_engine->engine.has_value()) + { + std::seed_seq seed{t_lazy_random_engine->seed[0], t_lazy_random_engine->seed[1]}; + t_lazy_random_engine->engine.emplace(seed); + } + return *t_lazy_random_engine->engine; + } return t_random_engine != nullptr ? *t_random_engine : Global.random_engine; } } // namespace +void set_thread_lazy_random_engine(lazy_random_engine *Engine) +{ + t_lazy_random_engine = Engine; +} + +lazy_random_engine *thread_lazy_random_engine() +{ + return t_lazy_random_engine; +} + void set_thread_random_engine(std::mt19937 *Engine) // NOSONAR: randomness of the simulation, not of anything security-relevant { t_random_engine = Engine; diff --git a/vehicle/dynamic/vehicletable.cpp b/vehicle/dynamic/vehicletable.cpp index 9d0e42a22..cc22de88d 100644 --- a/vehicle/dynamic/vehicletable.cpp +++ b/vehicle/dynamic/vehicletable.cpp @@ -41,6 +41,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Globals.h" #include "utilities/Timer.h" #include "utilities/workerpool.h" +#include "utilities/lazyrandom.h" #include "utilities/Logs.h" #include "utilities/glmHelpers.h" #include "Console.h" @@ -671,10 +672,12 @@ vehicle_table::update_steps_parallel( double const Deltatime, int const Iteratio auto const seed { static_cast( Global.random_engine() ) }; auto const steps = [ & ]( std::uint32_t const Group ) { auto const &members { m_groups[ Group ] }; - std::seed_seq groupseed { seed, members.front() }; - std::mt19937 engine { groupseed }; // NOSONAR: randomness of the simulation, not of anything security-relevant - auto *const threadengine { thread_random_engine() }; - set_thread_random_engine( &engine ); + // seeded at the first draw, if the group makes one: the seeding took longer than the steps of most groups + lazy_random_engine engine; + engine.seed[ 0 ] = seed; + engine.seed[ 1 ] = members.front(); + auto *const threadengine { thread_lazy_random_engine() }; + set_thread_lazy_random_engine( &engine ); // the hoses join only vehicles coupled with each other, which are in one group: the air they carry is exchanged // within the group after each step, as update_brake_pipes() does for all the vehicles in one thread std::vector pipes; @@ -701,7 +704,7 @@ vehicle_table::update_steps_parallel( double const Deltatime, int const Iteratio // the vehicles going by one step take it with the time of all of them m_items[ index ]->UpdateForce( coarse( index ) ? totaltime : Deltatime ); } - set_thread_random_engine( threadengine ); }; + set_thread_lazy_random_engine( threadengine ); }; Workers().run( m_threadgroups.size(), Threads,