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 00063dc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/openvic-simulation/economy/trading/MarketInstance.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "MarketInstance.hpp"

#include "openvic-simulation/utility/CompilerFeatureTesting.hpp"

using namespace OpenVic;

MarketInstance::MarketInstance(GoodInstanceManager& new_good_instance_manager)
Expand All @@ -17,8 +19,10 @@ 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) {
PARALLELISE_IF_SUPPORTED(
good_instance_manager.get_good_instances(),
,
good_instance,
good_instance.execute_orders();
}
)
}
15 changes: 11 additions & 4 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "openvic-simulation/history/ProvinceHistory.hpp"
#include "openvic-simulation/map/MapDefinition.hpp"
#include "openvic-simulation/utility/CompilerFeatureTesting.hpp"
#include "openvic-simulation/utility/Logger.hpp"

using namespace OpenVic;
Expand Down Expand Up @@ -164,18 +165,24 @@ void MapInstance::update_gamestate(const Date today, DefineManager const& define
}

void MapInstance::map_tick(const Date today) {
for (ProvinceInstance& province : province_instances.get_items()) {
PARALLELISE_IF_SUPPORTED(
province_instances.get_items(),
today,
province,
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()) {
PARALLELISE_IF_SUPPORTED(
province_instances.get_items(),
today,
province,
province.initialise_rgo();
province.province_tick(today);
}
)
}
5 changes: 2 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,7 @@ void ProvinceInstance::update_gamestate(const Date today, DefineManager const& d
}

void ProvinceInstance::province_tick(const Date today) {
for (Pop& pop : pops) {
pop.pop_tick();
}
PARALLELISE_IF_SUPPORTED(pops, , pop, pop.pop_tick();)
for (BuildingInstance& building : buildings.get_items()) {
building.tick(today);
}
Expand Down
30 changes: 30 additions & 0 deletions src/openvic-simulation/utility/CompilerFeatureTesting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//Based on https://en.cppreference.com/w/cpp/feature_test#Compiler_Features_Dump
#pragma once

#include <execution> //used in the macro

namespace OpenVic {
// Expect a string that starts with 6-decimal-digits or with '_' (if unsupported)
#define COMPILER_VALUE_INT(n) #n [0] == '_' ? 0 : \
(#n[5] - '0') + (#n[4] - '0') * 10 + (#n[3] - '0') * 100 + \
(#n[2] - '0') * 1000 + (#n[1] - '0') * 10000 + (#n[0] - '0') * 100000
#define IS_COMPILER_FEATURE_SUPPORTED(name, expected_version) COMPILER_VALUE_INT(name) >= expected_version
constexpr int EXECUTION_POLICY_MINIMUM_VERSION = 201603L;

#define PARALLELISE_IF_SUPPORTED(items_rh, captured_variables, item_variable_name, method_body) \
if constexpr (IS_COMPILER_FEATURE_SUPPORTED(__cpp_lib_execution,EXECUTION_POLICY_MINIMUM_VERSION)) { \
auto& items = items_rh; \
std::for_each( \
std::execution::par, \
items.begin(), \
items.end(), \
[captured_variables](auto& item_variable_name) -> void { \
method_body \
} \
); \
} else { \
for (auto& item_variable_name : items_rh) { \
method_body \
} \
}
}

0 comments on commit 00063dc

Please sign in to comment.