Skip to content

Commit

Permalink
PARALLELISE_IF_SUPPORTED
Browse files Browse the repository at this point in the history
  • Loading branch information
wvpm committed Dec 1, 2024
1 parent 626674b commit b9882a8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
15 changes: 11 additions & 4 deletions src/openvic-simulation/economy/trading/MarketInstance.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -17,8 +20,12 @@ void MarketInstance::place_market_sell_order(MarketSellOrder&& market_sell_order
}

void MarketInstance::execute_orders() {
std::vector<GoodInstance>& 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();
}
);
}
26 changes: 19 additions & 7 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -164,18 +166,28 @@ 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(
const Date today,
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);
}
);
}
11 changes: 8 additions & 3 deletions src/openvic-simulation/map/ProvinceInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions src/openvic-simulation/utility/CompilerFeatureTesting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <execution>

namespace OpenVic {
template <class InputIt, class UnaryFunc>
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
}
}

0 comments on commit b9882a8

Please sign in to comment.