diff --git a/physics/components/hosetrains.h b/physics/components/hosetrains.h index 5a2ac7e29..359a4348b 100644 --- a/physics/components/hosetrains.h +++ b/physics/components/hosetrains.h @@ -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 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 const &Work)> run; }; // physics::Vehicle::UpdatePipeCouplers(), with the trains kept in Cache from one call to the next. for one list of diff --git a/physics/components/pneumatics.cpp b/physics/components/pneumatics.cpp index 9a57a32ec..08247789d 100644 --- a/physics/components/pneumatics.cpp +++ b/physics/components/pneumatics.cpp @@ -1465,6 +1465,34 @@ void lay_out_trains(physics::hose_trains_cache::pipe &Layout, std::vector> outside_trains(physics::hose_trains_cache::pipe const &Layout, + std::vector const &Inputs) +{ + // the vehicles of the list in a train, for telling the others + std::vector intrains; + intrains.reserve(Layout.members.size()); + for (auto const member : Layout.members) + { + intrains.push_back(Inputs[member].vehicle); + } + std::ranges::sort(intrains); + std::vector> 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 const &Inputs) @@ -1480,30 +1508,7 @@ void lay_out_clusters(physics::hose_trains_cache::pipe &Layout, std::vector 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> 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::max()}; - std::vector clusterof(traincount, none); + auto constexpr no_cluster{std::numeric_limits::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 &Inputs, std::ve } } +// what the exchange of one pipe through the hoses goes by, for the trains of it +struct hose_exchange +{ + std::vector 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 fewsubsteps{}; + std::vector 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 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 const &Vehicles, train_pipe const &trainPipe, double const dt, physics::hose_trains_cache::pipe *Cache = nullptr, - std::function 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 fewsubsteps{}; - std::vector 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 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 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) diff --git a/tests/unit/test_brake_pipe.cpp b/tests/unit/test_brake_pipe.cpp index 726b2c99d..e8efe80d3 100644 --- a/tests/unit/test_brake_pipe.cpp +++ b/tests/unit/test_brake_pipe.cpp @@ -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 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); diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 3e2d08fa9..08f8f7e3e 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -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::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 neighbours { none, none }; // the places of the vehicles at its ends + static constexpr std::uint32_t nowhere { std::numeric_limits::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 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 m_groupparent; diff --git a/vehicle/dynamic/vehicletable.cpp b/vehicle/dynamic/vehicletable.cpp index e6b79d558..ff9af245e 100644 --- a/vehicle/dynamic/vehicletable.cpp +++ b/vehicle/dynamic/vehicletable.cpp @@ -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( 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( 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( m_hosetrains.run ) ) { - m_hosetrains.run = []( std::size_t const Count, std::function 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 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( 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( group ) );