Skip to content

Commit 7ac9aa6

Browse files
committed
Parallellise update gamestate and modifiers
1 parent 40a8de8 commit 7ac9aa6

13 files changed

Lines changed: 233 additions & 230 deletions

src/openvic-simulation/InstanceManager.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "openvic-simulation/core/stl/containers/TypedSpan.hpp"
66
#include "openvic-simulation/misc/GameAction.hpp"
77
#include "openvic-simulation/utility/Logger.hpp"
8+
#include "openvic-simulation/utility/ThreadDeps.hpp"
89

910
using namespace OpenVic;
1011

@@ -168,8 +169,8 @@ void InstanceManager::update_gamestate() {
168169
update_modifier_sums();
169170

170171
// Update gamestate...
171-
map_instance.update_gamestate(*this);
172-
country_instance_manager.update_gamestate(today, map_instance);
172+
map_instance.update_gamestate();
173+
country_instance_manager.update_gamestate_after_map(today);
173174
unit_instance_manager.update_gamestate();
174175

175176
gamestate_updated();
@@ -234,12 +235,19 @@ bool InstanceManager::setup() {
234235
}
235236

236237
thread_pool.initialise_threadpool(
237-
game_rules_manager,
238-
good_instance_manager,
239-
definition_manager.get_modifier_manager().get_modifier_effect_cache(),
240-
definition_manager.get_define_manager().get_pops_defines(),
241-
definition_manager.get_economy_manager().get_production_type_manager(),
242-
strata_index_t(definition_manager.get_pop_manager().get_strata_count()),
238+
ThreadDeps{
239+
game_rules_manager,
240+
good_instance_manager,
241+
map_instance,
242+
definition_manager.get_define_manager().get_military_defines(),
243+
definition_manager.get_modifier_manager().get_modifier_effect_cache(),
244+
definition_manager.get_define_manager().get_pops_defines(),
245+
definition_manager.get_economy_manager().get_production_type_manager(),
246+
definition_manager.get_modifier_manager().get_static_modifier_cache(),
247+
country_index_t(definition_manager.get_country_definition_manager().get_country_definition_count()),
248+
good_index_t(definition_manager.get_economy_manager().get_good_definition_manager().get_good_definition_count()),
249+
strata_index_t(definition_manager.get_pop_manager().get_strata_count())
250+
},
243251
good_instance_manager.get_good_instances(),
244252
country_instance_manager.get_country_instances(),
245253
map_instance.get_province_instances()
@@ -305,8 +313,8 @@ bool InstanceManager::load_bookmark(Bookmark const& new_bookmark) {
305313
OV_ERR_FAIL_COND_V_MSG(!all_has_state, false, "At least one land province has no state");
306314

307315
update_modifier_sums();
308-
map_instance.initialise_for_new_game(*this);
309-
country_instance_manager.update_gamestate(today, map_instance);
316+
map_instance.initialise_for_new_game();
317+
country_instance_manager.update_gamestate_after_map(today);
310318
market_instance.execute_orders();
311319

312320
return ret;
@@ -359,12 +367,9 @@ void InstanceManager::update_modifier_sums() {
359367
// full copy of all the modifiers affecting them in their modifier sum, but provinces only having their directly/locally
360368
// applied modifiers in their modifier sum, hence requiring owner country modifier effect values to be looked up when
361369
// determining the value of a global effect on the province.
362-
country_instance_manager.update_modifier_sums(
363-
today, definition_manager.get_modifier_manager().get_static_modifier_cache()
364-
);
365-
map_instance.update_modifier_sums(
366-
today, definition_manager.get_modifier_manager().get_static_modifier_cache()
367-
);
370+
country_instance_manager.update_modifier_sums_before_map();
371+
map_instance.update_modifier_sums(today);
372+
country_instance_manager.update_modifier_sums_after_map();
368373
}
369374

370375
bool InstanceManager::queue_game_action(game_action_t&& game_action) {

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ static constexpr Modifier const& get_country_status_static_effect(
17821782
}
17831783
}
17841784

1785-
void CountryInstance::update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache) {
1785+
void CountryInstance::update_modifier_sum_before_map(Date today, StaticModifierCache const& static_modifier_cache) {
17861786
// Update sum of national modifiers
17871787
modifier_sum.clear();
17881788

@@ -1856,19 +1856,23 @@ void CountryInstance::update_modifier_sum(Date today, StaticModifierCache const&
18561856
// TODO - calculate stats for each unit type (locked and unlocked)
18571857
}
18581858

1859-
void CountryInstance::make_room_for_province_modifier_sum(ModifierSum const& province_modifier_sum) {
1860-
modifier_sum.make_room_for(province_modifier_sum);
1859+
void CountryInstance::update_modifier_sum_after_map(Date today) {
1860+
for (ProvinceInstance const* const province_ptr : controlled_provinces) {
1861+
if (OV_likely(province_ptr != nullptr)) {
1862+
modifier_sum.add_modifier_sum(province_ptr->get_modifier_sum());
1863+
}
1864+
}
18611865
}
18621866

1863-
void CountryInstance::contribute_province_modifier_sum(ModifierSum const& province_modifier_sum) {
1864-
modifier_sum.add_modifier_sum(province_modifier_sum);
1867+
void CountryInstance::make_room_for_province_modifier_sum(ModifierSum const& province_modifier_sum) {
1868+
modifier_sum.make_room_for(province_modifier_sum);
18651869
}
18661870

18671871
fixed_point_t CountryInstance::get_modifier_effect_value(ModifierEffect const& effect) const {
18681872
return modifier_sum.get_modifier_effect_value(effect);
18691873
}
18701874

1871-
void CountryInstance::update_gamestate(const Date today, MapInstance& map_instance) {
1875+
void CountryInstance::update_gamestate_after_map(const Date today) {
18721876
if (is_civilised()) {
18731877
civilisation_progress = 0;
18741878
} else {
@@ -1932,14 +1936,14 @@ void CountryInstance::update_gamestate(const Date today, MapInstance& map_instan
19321936
province->set_connected_to_capital(false);
19331937
province->set_is_overseas(province_definition.get_continent() != capital_continent);
19341938

1935-
for (ProvinceDefinition::adjacency_t const& adjacency : province_definition.get_adjacencies()) {
1936-
// TODO - should we limit based on adjacency type? Straits and impassable still work in game,
1937-
// and water provinces don't have an owner so they'll get caught by the later checks anyway.
1938-
CountryInstance* neighbour = map_instance.get_province_instance_by_definition(adjacency.get_to()).get_owner();
1939-
if (neighbour != nullptr && neighbour != this) {
1940-
neighbouring_countries.insert(neighbour);
1941-
}
1942-
}
1939+
// for (ProvinceDefinition::adjacency_t const& adjacency : province_definition.get_adjacencies()) {
1940+
// // TODO - should we limit based on adjacency type? Straits and impassable still work in game,
1941+
// // and water provinces don't have an owner so they'll get caught by the later checks anyway.
1942+
// CountryInstance* neighbour = map_instance.get_province_instance_by_definition(adjacency.get_to()).get_owner();
1943+
// if (neighbour != nullptr && neighbour != this) {
1944+
// neighbouring_countries.insert(neighbour);
1945+
// }
1946+
// }
19431947
}
19441948

19451949
if (occupied_provinces_proportion != 0) {
@@ -1949,21 +1953,21 @@ void CountryInstance::update_gamestate(const Date today, MapInstance& map_instan
19491953

19501954
if (capital != nullptr) {
19511955
capital->set_connected_to_capital(true);
1952-
memory::vector<std::reference_wrapper<const ProvinceInstance>> province_checklist { *capital };
1956+
// memory::vector<std::reference_wrapper<const ProvinceInstance>> province_checklist { *capital };
19531957

1954-
for (size_t index = 0; index < province_checklist.size(); ++index) {
1955-
ProvinceInstance const& province = province_checklist[index];
1958+
// for (size_t index = 0; index < province_checklist.size(); ++index) {
1959+
// ProvinceInstance const& province = province_checklist[index];
19561960

1957-
for (ProvinceDefinition::adjacency_t const& adjacency : province.province_definition.get_adjacencies()) {
1958-
ProvinceInstance& adjacent_province = map_instance.get_province_instance_by_definition(adjacency.get_to());
1961+
// for (ProvinceDefinition::adjacency_t const& adjacency : province.province_definition.get_adjacencies()) {
1962+
// ProvinceInstance& adjacent_province = map_instance.get_province_instance_by_definition(adjacency.get_to());
19591963

1960-
if (adjacent_province.get_owner() == this && !adjacent_province.get_connected_to_capital()) {
1961-
adjacent_province.set_connected_to_capital(true);
1962-
adjacent_province.set_is_overseas(false);
1963-
province_checklist.emplace_back(adjacent_province);
1964-
}
1965-
}
1966-
}
1964+
// if (adjacent_province.get_owner() == this && !adjacent_province.get_connected_to_capital()) {
1965+
// adjacent_province.set_connected_to_capital(true);
1966+
// adjacent_province.set_is_overseas(false);
1967+
// province_checklist.emplace_back(adjacent_province);
1968+
// }
1969+
// }
1970+
// }
19671971
}
19681972

19691973
// Order of updates might need to be changed/functions split up to account for dependencies

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,17 +685,17 @@ namespace OpenVic {
685685
bool update_rule_set();
686686

687687
public:
688-
void update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache);
688+
void update_modifier_sum_before_map(Date today, StaticModifierCache const& static_modifier_cache);
689+
void update_modifier_sum_after_map(Date today);
689690
void make_room_for_province_modifier_sum(ModifierSum const& province_modifier_sum);
690-
void contribute_province_modifier_sum(ModifierSum const& province_modifier_sum);
691691
fixed_point_t get_modifier_effect_value(ModifierEffect const& effect) const;
692692
constexpr void for_each_contributing_modifier(
693693
ModifierEffect const& effect, ContributingModifierCallback auto callback
694694
) const {
695695
return modifier_sum.for_each_contributing_modifier(effect, std::move(callback));
696696
}
697697

698-
void update_gamestate(const Date today, MapInstance& map_instance);
698+
void update_gamestate_after_map(const Date today);
699699
void country_tick_before_map(
700700
TypedSpan<good_index_t, char> reusable_goods_mask,
701701
forwardable_span<

src/openvic-simulation/country/CountryInstanceManager.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,16 @@ bool CountryInstanceManager::apply_history_to_countries(InstanceManager& instanc
292292
return ret;
293293
}
294294

295-
void CountryInstanceManager::update_modifier_sums(const Date today, StaticModifierCache const& static_modifier_cache) {
296-
for (CountryInstance& country : country_instances) {
297-
country.update_modifier_sum(today, static_modifier_cache);
298-
}
295+
void CountryInstanceManager::update_modifier_sums_before_map() {
296+
thread_pool.process(work_t::COUNTRY_UPDATE_MODIFIER_SUMS_BEFORE_MAP);
299297
}
300298

301-
void CountryInstanceManager::update_gamestate(const Date today, MapInstance& map_instance) {
302-
for (CountryInstance& country : country_instances) {
303-
country.update_gamestate(today, map_instance);
304-
}
299+
void CountryInstanceManager::update_modifier_sums_after_map() {
300+
thread_pool.process(work_t::COUNTRY_UPDATE_MODIFIER_SUMS_AFTER_MAP);
301+
}
302+
303+
void CountryInstanceManager::update_gamestate_after_map(const Date today) {
304+
thread_pool.process(work_t::COUNTRY_UPDATE_GAMESTATE_AFTER_MAP);
305305

306306
// TODO - work out how to have ranking effects applied (e.g. static modifiers) applied at game start
307307
// we can't just move update_rankings to the top of this function as it will choose initial GPs based on
@@ -311,10 +311,10 @@ void CountryInstanceManager::update_gamestate(const Date today, MapInstance& map
311311
}
312312

313313
void CountryInstanceManager::country_manager_tick_before_map() {
314-
thread_pool.process_country_ticks_before_map();
314+
thread_pool.process(work_t::COUNTRY_TICK_BEFORE_MAP);
315315
}
316316

317317
void CountryInstanceManager::country_manager_tick_after_map() {
318-
thread_pool.process_country_ticks_after_map();
318+
thread_pool.process(work_t::COUNTRY_TICK_AFTER_MAP);
319319
shared_country_values.update_costs();
320320
}

src/openvic-simulation/country/CountryInstanceManager.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ namespace OpenVic {
1818
struct CountryHistoryManager;
1919
struct GoodInstanceManager;
2020
struct InstanceManager;
21-
struct MapInstance;
2221
struct PopsDefines;
2322
struct PopType;
24-
struct StaticModifierCache;
2523
struct ThreadPool;
2624

2725
struct CountryInstanceManager {
@@ -71,8 +69,9 @@ namespace OpenVic {
7169

7270
bool apply_history_to_countries(InstanceManager& instance_manager);
7371

74-
void update_modifier_sums(const Date today, StaticModifierCache const& static_modifier_cache);
75-
void update_gamestate(const Date today, MapInstance& map_instance);
72+
void update_modifier_sums_before_map();
73+
void update_modifier_sums_after_map();
74+
void update_gamestate_after_map(const Date today);
7675
void country_manager_tick_before_map();
7776
void country_manager_tick_after_map();
7877
};

src/openvic-simulation/economy/trading/MarketInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void MarketInstance::place_market_sell_order(MarketSellOrder&& market_sell_order
7878
}
7979

8080
void MarketInstance::execute_orders() {
81-
thread_pool.process_good_execute_orders();
81+
thread_pool.process(work_t::GOOD_EXECUTE_ORDERS);
8282
}
8383

8484
void MarketInstance::record_price_history() {

src/openvic-simulation/map/MapInstance.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,17 @@ bool MapInstance::apply_history_to_provinces(
145145
return ret;
146146
}
147147

148-
void MapInstance::update_modifier_sums(const Date today, StaticModifierCache const& static_modifier_cache) {
149-
for (ProvinceInstance& province : get_province_instances()) {
150-
province.update_modifier_sum(today, static_modifier_cache);
151-
}
152-
153-
for (ProvinceInstance& province : get_province_instances()) {
154-
province.update_country_modifier_sum();
155-
}
148+
void MapInstance::update_modifier_sums(const Date today) {
149+
thread_pool.process(work_t::PROVINCE_UPDATE_MODIFIER_SUMS);
156150
}
157151

158-
void MapInstance::update_gamestate(InstanceManager const& instance_manager) {
152+
void MapInstance::update_gamestate() {
159153
highest_province_population = 0;
160154
total_map_population = 0;
161155

162-
for (ProvinceInstance& province : get_province_instances()) {
163-
province.update_gamestate(instance_manager);
156+
thread_pool.process(work_t::PROVINCE_UPDATE_GAMESTATE);
164157

158+
for (ProvinceInstance& province : get_province_instances()) {
165159
// Update population stats
166160
const pop_sum_t province_population = province.get_total_population();
167161
if (highest_province_population < province_population) {
@@ -174,13 +168,13 @@ void MapInstance::update_gamestate(InstanceManager const& instance_manager) {
174168
}
175169

176170
void MapInstance::map_tick() {
177-
thread_pool.process_province_ticks();
171+
thread_pool.process(work_t::PROVINCE_TICK);
178172
//state tick
179173
//after province tick as province tick sets pop employment to 0
180174
//state tick will update pop employment via factories
181175
}
182176

183-
void MapInstance::initialise_for_new_game(InstanceManager const& instance_manager) {
184-
update_gamestate(instance_manager);
185-
thread_pool.process_province_initialise_for_new_game();
177+
void MapInstance::initialise_for_new_game() {
178+
update_gamestate();
179+
thread_pool.process(work_t::PROVINCE_INITIALISE_FOR_NEW_GAME);
186180
}

src/openvic-simulation/map/MapInstance.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ namespace OpenVic {
8585
TypedSpan<reform_index_t, const Reform> reforms
8686
);
8787

88-
void update_modifier_sums(const Date today, StaticModifierCache const& static_modifier_cache);
89-
void update_gamestate(InstanceManager const& instance_manager);
88+
void update_modifier_sums(const Date today);
89+
void update_gamestate();
9090
void map_tick();
91-
void initialise_for_new_game(InstanceManager const& instance_manager);
91+
void initialise_for_new_game();
9292
};
9393
}

0 commit comments

Comments
 (0)