From a0e4d685e934bbce67d11f0fd6cc117b66c15d3f Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 8 Nov 2024 12:54:59 +0100 Subject: [PATCH 1/3] use span instead of reference wrapper for cache sequence Signed-off-by: Martijn Govers --- .../power_grid_model/main_model_impl.hpp | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp index 8d35a585a..1a2928f21 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp @@ -316,12 +316,9 @@ class MainModelImpl, ComponentLis // update all components template - void - update_component(ConstDataset const& update_data, Idx pos, - std::array const>, n_types> const& sequence_idx_map) { + void update_component(ConstDataset const& update_data, Idx pos, SequenceIdxView const& sequence_idx_map) { run_functor_with_all_types_return_void([this, pos, &update_data, &sequence_idx_map]() { - this->update_component(update_data, pos, - std::get>(sequence_idx_map).get()); + this->update_component(update_data, pos, std::get>(sequence_idx_map)); }); } template @@ -435,7 +432,7 @@ class MainModelImpl, ComponentLis ComponentFlags const& to_store) const { // TODO: (jguo) this function could be encapsulated in UpdateCompIndependence in update.hpp return run_functor_with_all_types_return_array([this, scenario_idx, &update_data, &to_store]() { - if (!to_store[index_of_component]) { + if (!std::get>(to_store)) { return std::vector{}; } auto const independence = check_components_independence(update_data); @@ -445,15 +442,7 @@ class MainModelImpl, ComponentLis } // get sequence idx map of an entire batch for fast caching of component sequences SequenceIdx get_sequence_idx_map(ConstDataset const& update_data, ComponentFlags const& to_store) const { - // TODO: (jguo) this function could be encapsulated in UpdateCompIndependence in update.hpp - return run_functor_with_all_types_return_array([this, &update_data, &to_store]() { - if (!to_store[index_of_component]) { - return std::vector{}; - } - auto const independence = check_components_independence(update_data); - validate_update_data_independence(independence); - return get_component_sequence(update_data, 0, independence); - }); + return get_sequence_idx_map(update_data, 0, to_store); } SequenceIdx get_sequence_idx_map(ConstDataset const& update_data) const { constexpr ComponentFlags all_true = [] { @@ -640,15 +629,8 @@ class MainModelImpl, ComponentLis auto model = copy_model_functor(start); SequenceIdx current_scenario_sequence_cache = SequenceIdx{}; - std::array const>, n_types> const current_scenario_sequence = - run_functor_with_all_types_return_array( - [&is_independent, &all_scenarios_sequence, ¤t_scenario_sequence_cache]() { - constexpr auto comp_idx = index_of_component; - return is_independent[comp_idx] ? std::cref(all_scenarios_sequence[comp_idx]) - : std::cref(current_scenario_sequence_cache[comp_idx]); - }); - auto [setup, winddown] = scenario_update_restore(model, update_data, current_scenario_sequence, - current_scenario_sequence_cache, is_independent, infos); + auto [setup, winddown] = scenario_update_restore(model, update_data, is_independent, all_scenarios_sequence, + current_scenario_sequence_cache, infos); auto calculate_scenario = MainModelImpl::call_with( [&model, &calculation_fn, &result_data, &infos](Idx scenario_idx) { @@ -720,28 +702,40 @@ class MainModelImpl, ComponentLis }; } - static auto scenario_update_restore( - MainModelImpl& model, ConstDataset const& update_data, - std::array const>, n_types> const& scenario_sequence, - SequenceIdx& current_scenario_sequence_cache, ComponentFlags const& is_independent, - std::vector& infos) noexcept { + static auto scenario_update_restore(MainModelImpl& model, ConstDataset const& update_data, + ComponentFlags const& is_independent, SequenceIdx const& all_scenario_sequence, + SequenceIdx& current_scenario_sequence_cache, + std::vector& infos) noexcept { auto do_update_cache = [&is_independent] { ComponentFlags result; std::ranges::transform(is_independent, result.begin(), std::logical_not<>{}); return result; }(); + auto const scenario_sequence = [&all_scenario_sequence, ¤t_scenario_sequence_cache, + &is_independent]() -> SequenceIdxView { + return run_functor_with_all_types_return_array([&]() { + constexpr auto comp_idx = index_of_component; + if (std::get(is_independent)) { + return std::span{std::get(all_scenario_sequence)}; + } + return std::span{std::get(current_scenario_sequence_cache)}; + }); + }; + return std::make_pair( - [&model, &update_data, &scenario_sequence, ¤t_scenario_sequence_cache, + [&model, &update_data, scenario_sequence, ¤t_scenario_sequence_cache, do_update_cache_ = std::move(do_update_cache), &infos](Idx scenario_idx) { Timer const t_update_model(infos[scenario_idx], 1200, "Update model"); current_scenario_sequence_cache = model.get_sequence_idx_map(update_data, scenario_idx, do_update_cache_); - model.template update_component(update_data, scenario_idx, scenario_sequence); + + model.template update_component(update_data, scenario_idx, scenario_sequence()); }, - [&model, &scenario_sequence, ¤t_scenario_sequence_cache, &infos](Idx scenario_idx) { + [&model, scenario_sequence, ¤t_scenario_sequence_cache, &infos](Idx scenario_idx) { Timer const t_update_model(infos[scenario_idx], 1201, "Restore model"); - model.restore_components(scenario_sequence); + + model.restore_components(scenario_sequence()); std::ranges::for_each(current_scenario_sequence_cache, [](auto& comp_seq_idx) { comp_seq_idx.clear(); }); }); From 68078f5b3f7dd1a9c54b43c9d3b170e844cf082c Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 8 Nov 2024 13:34:52 +0100 Subject: [PATCH 2/3] sonar cloud Signed-off-by: Martijn Govers --- .../include/power_grid_model/main_model_impl.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp index 1a2928f21..8f0e00780 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp @@ -714,13 +714,14 @@ class MainModelImpl, ComponentLis auto const scenario_sequence = [&all_scenario_sequence, ¤t_scenario_sequence_cache, &is_independent]() -> SequenceIdxView { - return run_functor_with_all_types_return_array([&]() { - constexpr auto comp_idx = index_of_component; - if (std::get(is_independent)) { - return std::span{std::get(all_scenario_sequence)}; - } - return std::span{std::get(current_scenario_sequence_cache)}; - }); + return run_functor_with_all_types_return_array( + [&all_scenario_sequence, ¤t_scenario_sequence_cache, &is_independent]() { + constexpr auto comp_idx = index_of_component; + if (std::get(is_independent)) { + return std::span{std::get(all_scenario_sequence)}; + } + return std::span{std::get(current_scenario_sequence_cache)}; + }); }; return std::make_pair( From 8b39eda650ebe3745e94a6f8d46659f51ca6c7c6 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Wed, 13 Nov 2024 11:35:55 +0100 Subject: [PATCH 3/3] resolve comments Signed-off-by: Martijn Govers --- .../power_grid_model/main_model_impl.hpp | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp index 8f0e00780..b62b30cdc 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp @@ -429,28 +429,28 @@ class MainModelImpl, ComponentLis return get_sequence(buffer_span); } SequenceIdx get_sequence_idx_map(ConstDataset const& update_data, Idx scenario_idx, - ComponentFlags const& to_store) const { + ComponentFlags const& components_to_store) const { // TODO: (jguo) this function could be encapsulated in UpdateCompIndependence in update.hpp - return run_functor_with_all_types_return_array([this, scenario_idx, &update_data, &to_store]() { - if (!std::get>(to_store)) { - return std::vector{}; - } - auto const independence = check_components_independence(update_data); - validate_update_data_independence(independence); - return get_component_sequence(update_data, scenario_idx, independence); - }); - } - // get sequence idx map of an entire batch for fast caching of component sequences - SequenceIdx get_sequence_idx_map(ConstDataset const& update_data, ComponentFlags const& to_store) const { - return get_sequence_idx_map(update_data, 0, to_store); + return run_functor_with_all_types_return_array( + [this, scenario_idx, &update_data, &components_to_store]() { + if (!std::get>(components_to_store)) { + return std::vector{}; + } + auto const independence = check_components_independence(update_data); + validate_update_data_independence(independence); + return get_component_sequence(update_data, scenario_idx, independence); + }); } + // Get sequence idx map of an entire batch for fast caching of component sequences. + // The sequence idx map of the batch is the same as that of the first scenario in the batch (assuming homogeneity) + // This is the entry point for permanent updates. SequenceIdx get_sequence_idx_map(ConstDataset const& update_data) const { constexpr ComponentFlags all_true = [] { ComponentFlags result{}; std::ranges::fill(result, true); return result; }(); - return get_sequence_idx_map(update_data, all_true); + return get_sequence_idx_map(update_data, 0, all_true); } private: @@ -610,9 +610,10 @@ class MainModelImpl, ComponentLis // const ref of current instance MainModelImpl const& base_model = *this; - // cache component update order if possible + // cache component update order where possible. + // the order for a cacheable (independent) component by definition is the same across all scenarios auto const is_independent = is_update_independent(update_data); - all_scenarios_sequence = get_sequence_idx_map(update_data, is_independent); + all_scenarios_sequence = get_sequence_idx_map(update_data, 0, is_independent); return [&base_model, &exceptions, &infos, &calculation_fn, &result_data, &update_data, &all_scenarios_sequence = std::as_const(all_scenarios_sequence),