diff --git a/src/openvic-simulation/economy/trading/MarketInstance.cpp b/src/openvic-simulation/economy/trading/MarketInstance.cpp index 21eb384c..f9dd2b1f 100644 --- a/src/openvic-simulation/economy/trading/MarketInstance.cpp +++ b/src/openvic-simulation/economy/trading/MarketInstance.cpp @@ -1,5 +1,8 @@ #include "MarketInstance.hpp" +#include "openvic-simulation/utility/CompilerFeatureTesting.hpp" +#include "economy/GoodInstance.hpp" + using namespace OpenVic; MarketInstance::MarketInstance(GoodInstanceManager& new_good_instance_manager) @@ -17,8 +20,12 @@ void MarketInstance::place_market_sell_order(MarketSellOrder&& market_sell_order } void MarketInstance::execute_orders() { - std::vector& good_instances = good_instance_manager.get_good_instances(); - for (GoodInstance& good_instance : good_instances) { - good_instance.execute_orders(); - } + auto& good_instances = good_instance_manager.get_good_instances(); + try_parallel_for_each( + good_instances.begin(), + good_instances.end(), + [](GoodInstance& good_instance) -> void { + good_instance.execute_orders(); + } + ); } diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp index a433bb65..0155df2f 100644 --- a/src/openvic-simulation/map/MapInstance.cpp +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -2,7 +2,9 @@ #include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/map/MapDefinition.hpp" +#include "openvic-simulation/utility/CompilerFeatureTesting.hpp" #include "openvic-simulation/utility/Logger.hpp" +#include "map/ProvinceInstance.hpp" using namespace OpenVic; @@ -164,9 +166,14 @@ void MapInstance::update_gamestate(const Date today, DefineManager const& define } void MapInstance::map_tick(const Date today) { - for (ProvinceInstance& province : province_instances.get_items()) { - province.province_tick(today); - } + auto& provinces = province_instances.get_items(); + try_parallel_for_each( + provinces.begin(), + provinces.end(), + [today](ProvinceInstance& province) -> void { + province.province_tick(today); + } + ); } void MapInstance::initialise_for_new_game( @@ -174,8 +181,13 @@ void MapInstance::initialise_for_new_game( DefineManager const& define_manager ) { update_gamestate(today, define_manager); - for (ProvinceInstance& province : province_instances.get_items()) { - province.initialise_rgo(); - province.province_tick(today); - } + auto& provinces = province_instances.get_items(); + try_parallel_for_each( + provinces.begin(), + provinces.end(), + [today](ProvinceInstance& province) -> void { + province.initialise_rgo(); + province.province_tick(today); + } + ); } \ No newline at end of file diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index 0e57a2d3..9768a95b 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -12,6 +12,7 @@ #include "openvic-simulation/military/UnitInstanceGroup.hpp" #include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/politics/Ideology.hpp" +#include "openvic-simulation/utility/CompilerFeatureTesting.hpp" #include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -375,9 +376,13 @@ void ProvinceInstance::update_gamestate(const Date today, DefineManager const& d } void ProvinceInstance::province_tick(const Date today) { - for (Pop& pop : pops) { - pop.pop_tick(); - } + try_parallel_for_each( + pops.begin(), + pops.end(), + [](Pop& pop) -> void { + pop.pop_tick(); + } + ); for (BuildingInstance& building : buildings.get_items()) { building.tick(today); } diff --git a/src/openvic-simulation/utility/CompilerFeatureTesting.hpp b/src/openvic-simulation/utility/CompilerFeatureTesting.hpp new file mode 100644 index 00000000..dcf2bc5d --- /dev/null +++ b/src/openvic-simulation/utility/CompilerFeatureTesting.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace OpenVic { + template + inline constexpr void try_parallel_for_each( + InputIt first, + InputIt last, + UnaryFunc f + ) { + #ifdef __cpp_lib_execution + if constexpr (__cpp_lib_execution >= 201603L) { + std::for_each(std::execution::par, first, last, f); + } else { + std::for_each(first, last, f); + } + #else + std::for_each(first, last, f); + #endif + } +} \ No newline at end of file