mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 05:02:45 +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:
@@ -54,8 +54,9 @@ class hose_trains_cache
|
||||
pipe brake;
|
||||
pipe main;
|
||||
// calls Work(index) for each index below Count, in any order and on any threads, and returns when all are done
|
||||
// (worker_pool::run()). none: one after another
|
||||
std::function<void(std::size_t Count, std::function<void(std::size_t)> const &Work)> run;
|
||||
// (worker_pool::run()). Members is how many vehicles all of them exchange, for telling whether the work is worth more
|
||||
// than one thread. none: one after another
|
||||
std::function<void(std::size_t Count, std::size_t Members, std::function<void(std::size_t)> const &Work)> run;
|
||||
};
|
||||
|
||||
// physics::Vehicle::UpdatePipeCouplers(), with the trains kept in Cache from one call to the next. for one list of
|
||||
|
||||
+131
-117
@@ -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)
|
||||
|
||||
@@ -224,13 +224,14 @@ TEST_CASE("brake pipe: the trains of the hose exchange taken a cluster at a time
|
||||
};
|
||||
test::consist serial;
|
||||
test::consist clusters;
|
||||
with(serialdraws, [&] { build(serial); serial.run(10.0); });
|
||||
with(clusterdraws, [&] { build(clusters); clusters.run(10.0); });
|
||||
with(serialdraws, [&build, &serial] { build(serial); serial.run(10.0); });
|
||||
with(clusterdraws, [&build, &clusters] { build(clusters); clusters.run(10.0); });
|
||||
// the exchange alone, as vehicle_table::update_brake_pipes() makes it: the clusters backwards, the last one first
|
||||
physics::hose_trains_cache serialcache;
|
||||
physics::hose_trains_cache clustercache;
|
||||
std::size_t most{0};
|
||||
clustercache.run = [&most](std::size_t const Count, std::function<void(std::size_t)> const &Work) {
|
||||
// all the clusters whatever the amount of work, which vehicle_table's runner would leave to one thread here
|
||||
clustercache.run = [&most](std::size_t const Count, std::size_t, auto const &Work) {
|
||||
most = std::max(most, Count);
|
||||
for (auto index = Count; index > 0; --index) {
|
||||
Work(index - 1);
|
||||
|
||||
+9
-3
@@ -1125,9 +1125,9 @@ private:
|
||||
// the table: taken on the threads of worker_pool along with the neighbours, so that those loops read a short array in
|
||||
// place of the objects of a thousand vehicles. read where they were read before, with nothing changing them between
|
||||
struct vehicle_facts {
|
||||
static constexpr std::uint32_t none { std::numeric_limits<std::uint32_t>::max() };
|
||||
static constexpr std::uint32_t unseen { none - 1 }; // coupled with one it doesn't see next to it, or one not in the table
|
||||
std::array<std::uint32_t, 2> neighbours { none, none }; // the places of the vehicles at its ends
|
||||
static constexpr std::uint32_t nowhere { std::numeric_limits<std::uint32_t>::max() }; // no vehicle at that end
|
||||
static constexpr std::uint32_t unseen { nowhere - 1 }; // coupled with one it doesn't see next to it, or one not in the table
|
||||
std::array<std::uint32_t, 2> neighbours { nowhere, nowhere }; // the places of the vehicles at its ends
|
||||
bool active { false }; // its physics on, or the user in it
|
||||
bool loading { false }; // exchanging its load
|
||||
};
|
||||
@@ -1135,6 +1135,12 @@ private:
|
||||
// maintenance; removes from tracks consists with vehicles marked as disabled
|
||||
bool
|
||||
erase_disabled();
|
||||
// the constants of the physics of the vehicles, the vehicles next to them and then their facts, on the threads of worker_pool
|
||||
void
|
||||
update_neighbours_and_facts();
|
||||
// the facts of the vehicle at Index in the table, once the places of all of them are set
|
||||
void
|
||||
take_facts( std::size_t const Index );
|
||||
// working space of update_steps_parallel(), kept between the updates: the groups of the vehicles (joined by union find), the
|
||||
// group of each root, the groups done on the threads and in the main thread
|
||||
std::vector<std::uint32_t> m_groupparent;
|
||||
|
||||
@@ -169,6 +169,73 @@ vehicle_table::update_coupler_physics() const {
|
||||
}
|
||||
}
|
||||
|
||||
// the constants of the physics of each vehicle and the vehicles next to it, on the threads of worker_pool: a vehicle
|
||||
// changes only its own, and the others are looked at where they are, which nothing changes here (the wires of
|
||||
// update_traction() don't count). so the results are those of doing the vehicles one after another. then the facts of
|
||||
// each vehicle the loops of update() go by (vehicle_facts), once the places of all of them are known. a thread for each
|
||||
// VEHICLES_PER_THREAD vehicles, up to all the cores: with fewer, waking the threads takes longer than the work
|
||||
void
|
||||
vehicle_table::update_neighbours_and_facts() {
|
||||
|
||||
std::size_t constexpr VEHICLES_PER_PART { 16 };
|
||||
std::size_t constexpr VEHICLES_PER_THREAD { 64 };
|
||||
auto const itemcount { m_items.size() };
|
||||
auto const parts { ( itemcount + VEHICLES_PER_PART - 1 ) / VEHICLES_PER_PART };
|
||||
m_facts.resize( itemcount );
|
||||
auto const part = [ this, itemcount ]( std::size_t const Part ) {
|
||||
auto const last { std::min( itemcount, ( Part + 1 ) * VEHICLES_PER_PART ) };
|
||||
for( auto index { Part * VEHICLES_PER_PART }; index < last; ++index ) {
|
||||
auto *vehicle { m_items[ index ] };
|
||||
vehicle->m_tableindex = static_cast<std::uint32_t>( index );
|
||||
if( false == vehicle->bEnabled ) { continue; }
|
||||
vehicle->physics->ComputeConstans();
|
||||
vehicle->update_neighbours();
|
||||
}
|
||||
};
|
||||
auto const factpart = [ this, itemcount ]( std::size_t const Part ) {
|
||||
auto const last { std::min( itemcount, ( Part + 1 ) * VEHICLES_PER_PART ) };
|
||||
for( auto index { Part * VEHICLES_PER_PART }; index < last; ++index ) {
|
||||
take_facts( index );
|
||||
}
|
||||
};
|
||||
if( auto const threads { std::min( { worker_pool::capacity(), parts, itemcount / VEHICLES_PER_THREAD } ) }; threads > 1 ) {
|
||||
Workers().run( parts, threads, part );
|
||||
Workers().run( parts, threads, factpart );
|
||||
return;
|
||||
}
|
||||
for( std::size_t index = 0; index < parts; ++index ) {
|
||||
part( index );
|
||||
}
|
||||
for( std::size_t index = 0; index < parts; ++index ) {
|
||||
factpart( index );
|
||||
}
|
||||
}
|
||||
|
||||
// the facts of the vehicle at Index in the table (vehicle_facts), once the places of all of them (m_tableindex) are set
|
||||
void
|
||||
vehicle_table::take_facts( std::size_t const Index ) {
|
||||
|
||||
auto const itemcount { m_items.size() };
|
||||
auto const *vehicle { m_items[ Index ] };
|
||||
auto const *physics { vehicle->physics };
|
||||
auto &fact { m_facts[ Index ] };
|
||||
fact.active = physics->PhysicActivation() || vehicle->MechInside;
|
||||
fact.loading = ( physics->LoadStatus() != 0 ) || ( vehicle->LoadExchangeTime() > 0.f );
|
||||
for( auto const side : VehicleEnds ) {
|
||||
auto const *neighbour { physics->Neighbours[ side ].vehicle };
|
||||
auto &place { fact.neighbours[ side == VehicleEnd::front ? 0 : 1 ] };
|
||||
place = vehicle_facts::nowhere;
|
||||
if( ( physics->Couplers[ side ].Connected != nullptr )
|
||||
&& ( ( neighbour == nullptr ) || ( neighbour->physics != physics->Couplers[ side ].Connected ) ) ) {
|
||||
place = vehicle_facts::unseen;
|
||||
continue;
|
||||
}
|
||||
if( neighbour == nullptr ) { continue; }
|
||||
auto const other { neighbour->m_tableindex };
|
||||
place = ( ( other >= itemcount ) || ( m_items[ other ] != neighbour ) ) ? vehicle_facts::unseen : other;
|
||||
}
|
||||
}
|
||||
|
||||
// legacy method, calculates changes in simulation state over specified time
|
||||
void
|
||||
vehicle_table::update( double Deltatime, int Iterationcount ) {
|
||||
@@ -257,62 +324,7 @@ vehicle_table::update( double Deltatime, int Iterationcount ) {
|
||||
update_traction( vehicle );
|
||||
}
|
||||
}
|
||||
// the constants of the physics of each vehicle and the vehicles next to it, on the threads of worker_pool: a vehicle
|
||||
// changes only its own, and the others are looked at where they are, which nothing changes here (the wires above
|
||||
// don't count). so the results are those of doing the vehicles one after another
|
||||
// then the facts of each vehicle the loops below go by (vehicle_facts), once the places of all of them are known
|
||||
{
|
||||
auto constexpr VEHICLES_PER_PART { 16 };
|
||||
auto const itemcount { m_items.size() };
|
||||
auto const parts { ( itemcount + VEHICLES_PER_PART - 1 ) / VEHICLES_PER_PART };
|
||||
m_facts.resize( itemcount );
|
||||
auto const part = [ this, itemcount ]( std::size_t const Part ) {
|
||||
auto const end { std::min( itemcount, ( Part + 1 ) * VEHICLES_PER_PART ) };
|
||||
for( auto index { Part * VEHICLES_PER_PART }; index < end; ++index ) {
|
||||
auto *vehicle { m_items[ index ] };
|
||||
vehicle->m_tableindex = static_cast<std::uint32_t>( index );
|
||||
if( false == vehicle->bEnabled ) { continue; }
|
||||
vehicle->physics->ComputeConstans();
|
||||
vehicle->update_neighbours();
|
||||
}
|
||||
};
|
||||
auto const factpart = [ this, itemcount ]( std::size_t const Part ) {
|
||||
auto const end { std::min( itemcount, ( Part + 1 ) * VEHICLES_PER_PART ) };
|
||||
for( auto index { Part * VEHICLES_PER_PART }; index < end; ++index ) {
|
||||
auto const *vehicle { m_items[ index ] };
|
||||
auto const *physics { vehicle->physics };
|
||||
auto &fact { m_facts[ index ] };
|
||||
fact.active = physics->PhysicActivation() || vehicle->MechInside;
|
||||
fact.loading = ( physics->LoadStatus() != 0 ) || ( vehicle->LoadExchangeTime() > 0.f );
|
||||
for (auto const end : VehicleEnds) {
|
||||
auto const *neighbour { physics->Neighbours[ end ].vehicle };
|
||||
auto &place { fact.neighbours[ end == VehicleEnd::front ? 0 : 1 ] };
|
||||
place = vehicle_facts::none;
|
||||
if( ( physics->Couplers[ end ].Connected != nullptr )
|
||||
&& ( ( neighbour == nullptr ) || ( neighbour->physics != physics->Couplers[ end ].Connected ) ) ) {
|
||||
place = vehicle_facts::unseen;
|
||||
continue;
|
||||
}
|
||||
if( neighbour == nullptr ) { continue; }
|
||||
auto const other { neighbour->m_tableindex };
|
||||
place = ( ( other >= itemcount ) || ( m_items[ other ] != neighbour ) ) ? vehicle_facts::unseen : other;
|
||||
}
|
||||
}
|
||||
};
|
||||
auto const partthreads { std::min( worker_pool::capacity(), parts ) };
|
||||
if( partthreads > 1 ) {
|
||||
Workers().run( parts, partthreads, part );
|
||||
Workers().run( parts, partthreads, factpart );
|
||||
}
|
||||
else {
|
||||
for( std::size_t index = 0; index < parts; ++index ) {
|
||||
part( index );
|
||||
}
|
||||
for( std::size_t index = 0; index < parts; ++index ) {
|
||||
factpart( index );
|
||||
}
|
||||
}
|
||||
}
|
||||
update_neighbours_and_facts();
|
||||
update_coupler_physics();
|
||||
Timer::subsystem.vehicles_prepare.stop();
|
||||
Timer::subsystem.vehicles_forces.start();
|
||||
@@ -630,11 +642,12 @@ vehicle_table::update_brake_pipes( double const Deltatime ) {
|
||||
m_brakepipes.push_back( vehicle->physics );
|
||||
}
|
||||
}
|
||||
// the clusters of the trains at once, on the threads of worker_pool
|
||||
// the clusters of the trains at once, on the threads of worker_pool: a thread for each MEMBERS_PER_THREAD vehicles
|
||||
// exchanged, up to all the cores. with fewer, waking the threads takes longer than the work, so they go one after another
|
||||
if( false == static_cast<bool>( m_hosetrains.run ) ) {
|
||||
m_hosetrains.run = []( std::size_t const Count, std::function<void( std::size_t )> const &Work ) {
|
||||
auto const threads { std::min( worker_pool::capacity(), Count ) };
|
||||
if( threads > 1 ) {
|
||||
m_hosetrains.run = []( std::size_t const Count, std::size_t const Members, std::function<void( std::size_t )> const &Work ) {
|
||||
std::size_t constexpr MEMBERS_PER_THREAD { 64 };
|
||||
if( auto const threads { std::min( { worker_pool::capacity(), Count, Members / MEMBERS_PER_THREAD } ) }; threads > 1 ) {
|
||||
Workers().run( Count, threads, Work );
|
||||
return;
|
||||
}
|
||||
@@ -687,7 +700,7 @@ vehicle_table::update_steps_parallel( double const Deltatime, int const Iteratio
|
||||
if( other == vehicle_facts::unseen ) {
|
||||
return false;
|
||||
}
|
||||
if( other == vehicle_facts::none ) { continue; }
|
||||
if( other == vehicle_facts::nowhere ) { continue; }
|
||||
auto const left { root( static_cast<std::uint32_t>( index ) ) };
|
||||
auto const right { root( other ) };
|
||||
if( left != right ) {
|
||||
@@ -713,8 +726,8 @@ vehicle_table::update_steps_parallel( double const Deltatime, int const Iteratio
|
||||
std::size_t largest { 0 };
|
||||
for( std::size_t group = 0; group < groupcount; ++group ) {
|
||||
largest = std::max( largest, m_groups[ group ].size() );
|
||||
auto const loading { std::any_of(
|
||||
std::begin( m_groups[ group ] ), std::end( m_groups[ group ] ),
|
||||
auto const loading { std::ranges::any_of(
|
||||
m_groups[ group ],
|
||||
[ this ]( std::uint32_t const Index ) {
|
||||
return m_facts[ Index ].loading; } ) };
|
||||
( loading ? m_maingroups : m_threadgroups ).push_back( static_cast<std::uint32_t>( group ) );
|
||||
|
||||
Reference in New Issue
Block a user