18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 09:36:48 +01:00

perf(physics): the new worker jobs only with enough work, the hose exchange of a train in shared helpers

Review of #574:
- The sub-steps of one train and its exchange in them are helpers (train_substeps(), exchange_train()) taken by both the
  serial loop and the cluster job (exchange_cluster()), which gets one context struct (hose_exchange) by reference, so
  std::function keeps it in its small buffer. Picking the trains (hose_layout()) is its own function too.
- The runner of the clusters gets the number of vehicles exchanged and wakes the pool only for at least 64 of them a
  thread; below that the clusters go one after another. The neighbours and facts take a thread for each 64 vehicles
  and stay in the main thread below 128. With performance.multithreading no both still go one after another.
- The neighbours and facts moved out of update() into update_neighbours_and_facts() and take_facts(); the inner loop
  variable is side, no longer shadowing end. The vehicles outside the trains of lay_out_clusters() are gathered by
  outside_trains(). vehicle_facts::none is nowhere, the none of lay_out_clusters() no_cluster.
- The test lambdas capture explicitly and the runner of the test takes its job as auto.

Results unchanged: the clusters still work out the same sub-steps and take the trains in the same order.
This commit is contained in:
jerrrrycho
2026-12-02 01:39:48 +01:00
parent f058df2c64
commit 75efb467db
5 changed files with 223 additions and 188 deletions
+131 -117
View File
@@ -1465,6 +1465,34 @@ void lay_out_trains(physics::hose_trains_cache::pipe &Layout, std::vector<std::s
}
}
// each vehicle outside the trains of Layout (lay_out_trains()) with Inputs which a train sends air into, with that train,
// sorted
std::vector<std::pair<physics::Vehicle const *, std::size_t>> outside_trains(physics::hose_trains_cache::pipe const &Layout,
std::vector<physics::hose_trains_cache::input> const &Inputs)
{
// the vehicles of the list in a train, for telling the others
std::vector<physics::Vehicle const *> intrains;
intrains.reserve(Layout.members.size());
for (auto const member : Layout.members)
{
intrains.push_back(Inputs[member].vehicle);
}
std::ranges::sort(intrains);
std::vector<std::pair<physics::Vehicle const *, std::size_t>> outside;
for (auto const member : Layout.members)
{
for (auto const *joined : Inputs[member].joined)
{
if (joined != nullptr && false == std::ranges::binary_search(intrains, joined))
{
outside.emplace_back(joined, Layout.trains[member]);
}
}
}
std::ranges::sort(outside);
return outside;
}
// the clusters of the trains of Layout (lay_out_trains()) with Inputs: the trains joined by a hose to the same vehicle which
// isn't in one (it didn't step the pipe) go together, in the order of the trains
void lay_out_clusters(physics::hose_trains_cache::pipe &Layout, std::vector<physics::hose_trains_cache::input> const &Inputs)
@@ -1480,30 +1508,7 @@ void lay_out_clusters(physics::hose_trains_cache::pipe &Layout, std::vector<phys
}
return Train;
};
// the vehicles of the list in a train, for telling the others
std::vector<physics::Vehicle const *> intrains;
intrains.reserve(Layout.members.size());
for (auto const member : Layout.members)
{
intrains.push_back(Inputs[member].vehicle);
}
std::ranges::sort(intrains);
// each vehicle outside the trains with a train sending air into it
std::vector<std::pair<physics::Vehicle const *, std::size_t>> outside;
for (std::size_t train = 0; train < traincount; ++train)
{
for (auto member = Layout.firsts[train]; member < Layout.firsts[train + 1]; ++member)
{
for (auto const *joined : Inputs[Layout.members[member]].joined)
{
if (joined != nullptr && false == std::ranges::binary_search(intrains, joined))
{
outside.emplace_back(joined, train);
}
}
}
}
std::ranges::sort(outside);
auto const outside{outside_trains(Layout, Inputs)};
for (std::size_t index = 1; index < outside.size(); ++index)
{
if (outside[index].first == outside[index - 1].first)
@@ -1517,13 +1522,13 @@ void lay_out_clusters(physics::hose_trains_cache::pipe &Layout, std::vector<phys
}
}
// numbered by their first train; the trains of each in their order
auto constexpr none{std::numeric_limits<std::size_t>::max()};
std::vector<std::size_t> clusterof(traincount, none);
auto constexpr no_cluster{std::numeric_limits<std::size_t>::max()};
std::vector clusterof(traincount, no_cluster);
std::size_t clustercount{0};
for (std::size_t train = 0; train < traincount; ++train)
{
auto &cluster{clusterof[root(train)]};
if (cluster == none)
if (cluster == no_cluster)
{
cluster = clustercount++;
}
@@ -1563,111 +1568,120 @@ void hose_inputs(std::vector<physics::hose_trains_cache::input> &Inputs, std::ve
}
}
// what the exchange of one pipe through the hoses goes by, for the trains of it
struct hose_exchange
{
std::vector<physics::Vehicle *> const &vehicles;
physics::hose_trains_cache::pipe const &layout;
train_pipe const &trainPipe;
double dt;
};
// the sub-steps of dt the train of the exchange takes: as many as its vehicle needing the most of them
int train_substeps(hose_exchange const &Exchange, std::size_t const Train)
{
auto const &layout{Exchange.layout};
auto substeps{1};
for (auto member = layout.firsts[Train]; member < layout.firsts[Train + 1]; ++member)
{
substeps = std::max(substeps, hose_substeps(*Exchange.vehicles[layout.members[member]], Exchange.trainPipe, Exchange.dt));
}
return substeps;
}
// the exchange of the train of the exchange in Substeps sub-steps of dt, each going through its vehicles one after another
void exchange_train(hose_exchange const &Exchange, std::size_t const Train, int const Substeps)
{
auto const &layout{Exchange.layout};
auto const &trainPipe{Exchange.trainPipe};
auto const substep{Exchange.dt / Substeps};
for (auto i = 0; i < Substeps; ++i)
{
for (auto member = layout.firsts[Train]; member < layout.firsts[Train + 1]; ++member)
{
auto *vehicle{Exchange.vehicles[layout.members[member]]};
auto &pipe{*(vehicle->*trainPipe.pipe).reservoir};
pipe.Act(); // what the neighbours have sent in the meantime
(vehicle->*trainPipe.pipe).SetPressure(pipe.P());
pipe.Flow((vehicle->*trainPipe.exchange)(substep));
pipe.Act();
(vehicle->*trainPipe.pipe).SetPressure(pipe.P());
}
}
}
// the exchange of the trains of the cluster of the exchange: the sub-steps of all of them first, then the exchange of each
// in their order, as exchange_through_hoses() does for all the trains. a cluster reads and changes its own vehicles only
void exchange_cluster(hose_exchange const &Exchange, std::size_t const Cluster)
{
auto const &layout{Exchange.layout};
auto const first{layout.clusterfirsts[Cluster]};
auto const count{layout.clusterfirsts[Cluster + 1] - first};
std::array<int, 16> fewsubsteps{};
std::vector<int> manysubsteps;
int *substeps{fewsubsteps.data()};
if (count > fewsubsteps.size())
{
manysubsteps.resize(count);
substeps = manysubsteps.data();
}
for (std::size_t index = 0; index < count; ++index)
{
substeps[index] = train_substeps(Exchange, layout.clustertrains[first + index]);
}
for (std::size_t index = 0; index < count; ++index)
{
exchange_train(Exchange, layout.clustertrains[first + index], substeps[index]);
}
}
// the trains of the pipe for the exchange of Vehicles: those of Cache, worked out again when what they were worked out from
// changed, or without a Cache those of Fresh, worked out now
physics::hose_trains_cache::pipe const &hose_layout(std::vector<physics::Vehicle *> const &Vehicles, train_pipe const &trainPipe, physics::hose_trains_cache::pipe *Cache,
physics::hose_trains_cache::pipe &Fresh)
{
if (Cache == nullptr)
{
lay_out_trains(Fresh, hose_trains(Vehicles, trainPipe));
return Fresh;
}
hose_inputs(Cache->scratch, Vehicles, trainPipe);
if (false == Cache->valid || Cache->scratch != Cache->inputs)
{
lay_out_trains(*Cache, hose_trains(Vehicles, trainPipe));
std::swap(Cache->inputs, Cache->scratch);
lay_out_clusters(*Cache, Cache->inputs);
Cache->valid = true;
}
return *Cache;
}
// the air exchange through the hoses of the pipe, for the vehicles which stepped it since the last call, in the order
// they stepped: each train in as many sub-steps of dt as its vehicle needing the most of them takes, each going through
// its vehicles one after another, as the step of the vehicles does. the air of a train stays in it, so a train comes out
// the same whichever other vehicles are exchanged in the same call: all of them, or the ones of a group of
// vehicle_table::update_steps_parallel(). with a Cache, the trains of the call before are taken again when what they
// were worked out from is the same
// were worked out from is the same, and with a Run its clusters go through it, on many threads
void exchange_through_hoses(std::vector<physics::Vehicle *> const &Vehicles, train_pipe const &trainPipe, double const dt, physics::hose_trains_cache::pipe *Cache = nullptr,
std::function<void(std::size_t, std::function<void(std::size_t)> const &)> const *Run = nullptr)
decltype(physics::hose_trains_cache::run) const *Run = nullptr)
{
physics::hose_trains_cache::pipe fresh;
auto *layout{&fresh};
if (Cache != nullptr)
hose_exchange const exchange{Vehicles, hose_layout(Vehicles, trainPipe, Cache, fresh), trainPipe, dt};
auto const &layout{exchange.layout};
if (Run != nullptr && *Run && layout.clusterfirsts.size() > 2)
{
hose_inputs(Cache->scratch, Vehicles, trainPipe);
if (false == Cache->valid || Cache->scratch != Cache->inputs)
{
lay_out_trains(*Cache, hose_trains(Vehicles, trainPipe));
std::swap(Cache->inputs, Cache->scratch);
lay_out_clusters(*Cache, Cache->inputs);
Cache->valid = true;
}
layout = Cache;
(*Run)(layout.clusterfirsts.size() - 1, layout.members.size(), [&exchange](std::size_t const Cluster) { exchange_cluster(exchange, Cluster); });
}
else
{
lay_out_trains(fresh, hose_trains(Vehicles, trainPipe));
}
auto const &trains{layout->trains};
auto const &firsts{layout->firsts};
auto const &members{layout->members};
// the trains a cluster at a time, on many threads: a cluster reads and changes its own vehicles only, the sub-steps of
// all its trains first, then the exchange of each in their order, as below for all the trains
if (Run != nullptr && *Run && layout->clusterfirsts.size() > 2)
{
auto const &clusterfirsts{layout->clusterfirsts};
auto const &clustertrains{layout->clustertrains};
(*Run)(clusterfirsts.size() - 1, [&](std::size_t const Cluster) {
auto const first{clusterfirsts[Cluster]};
auto const count{clusterfirsts[Cluster + 1] - first};
std::array<int, 16> fewsubsteps{};
std::vector<int> manysubsteps;
int *substeps{fewsubsteps.data()};
if (count > fewsubsteps.size())
{
manysubsteps.resize(count);
substeps = manysubsteps.data();
}
for (std::size_t index = 0; index < count; ++index)
{
auto const train{clustertrains[first + index]};
substeps[index] = 1;
for (auto member = firsts[train]; member < firsts[train + 1]; ++member)
{
substeps[index] = std::max(substeps[index], hose_substeps(*Vehicles[members[member]], trainPipe, dt));
}
}
for (std::size_t index = 0; index < count; ++index)
{
auto const train{clustertrains[first + index]};
auto const substep{dt / substeps[index]};
for (auto i = 0; i < substeps[index]; ++i)
{
for (auto member = firsts[train]; member < firsts[train + 1]; ++member)
{
auto *vehicle{Vehicles[members[member]]};
auto &pipe{*(vehicle->*trainPipe.pipe).reservoir};
pipe.Act(); // what the neighbours have sent in the meantime
(vehicle->*trainPipe.pipe).SetPressure(pipe.P());
pipe.Flow((vehicle->*trainPipe.exchange)(substep));
pipe.Act();
(vehicle->*trainPipe.pipe).SetPressure(pipe.P());
}
}
}
});
for (auto *vehicle : Vehicles)
std::vector<int> substeps(layout.firsts.size() - 1);
for (std::size_t train = 0; train < substeps.size(); ++train)
{
(vehicle->*trainPipe.pipe).SetCouplersPending(false);
substeps[train] = train_substeps(exchange, train);
}
return;
}
std::vector<int> substeps(firsts.size() - 1, 1);
for (std::size_t index = 0; index < Vehicles.size(); ++index)
{
if (false == (Vehicles[index]->*trainPipe.pipe).CouplersPending())
for (std::size_t train = 0; train < substeps.size(); ++train)
{
continue;
}
substeps[trains[index]] = std::max(substeps[trains[index]], hose_substeps(*Vehicles[index], trainPipe, dt));
}
for (std::size_t train = 0; train < substeps.size(); ++train)
{
auto const substep{dt / substeps[train]};
for (auto i = 0; i < substeps[train]; ++i)
{
for (auto member = firsts[train]; member < firsts[train + 1]; ++member)
{
auto *vehicle{Vehicles[members[member]]};
auto &pipe{*(vehicle->*trainPipe.pipe).reservoir};
pipe.Act(); // what the neighbours have sent in the meantime
(vehicle->*trainPipe.pipe).SetPressure(pipe.P());
pipe.Flow((vehicle->*trainPipe.exchange)(substep));
pipe.Act();
(vehicle->*trainPipe.pipe).SetPressure(pipe.P());
}
exchange_train(exchange, train, substeps[train]);
}
}
for (auto *vehicle : Vehicles)