Skip to content

Commit 520c174

Browse files
committed
Revert "final refactor attempt"
This reverts commit c56adb2. Signed-off-by: Santiago Figueroa Manrique <santiago.figueroa.manrique@alliander.com>
1 parent c56adb2 commit 520c174

File tree

3 files changed

+95
-88
lines changed

3 files changed

+95
-88
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/core_utils.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
namespace power_grid_model::main_core::utils {
1414

15-
constexpr Idx invalid_index{-1};
16-
1715
template <class... ComponentTypes> constexpr size_t n_types = sizeof...(ComponentTypes);
1816
template <class CompType, class... ComponentTypes>
1917
constexpr size_t index_of_component = container_impl::get_cls_pos_v<CompType, ComponentTypes...>;

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/update.hpp

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace power_grid_model::main_core::update {
1717

18+
constexpr Idx invalid_index{-1};
19+
1820
namespace detail {
1921
template <component_c Component, forward_iterator_like<typename Component::UpdateType> ForwardIterator, typename Func>
2022
requires std::invocable<std::remove_cvref_t<Func>, typename Component::UpdateType, Idx2D const&>
@@ -45,16 +47,16 @@ template <typename T> bool check_id_na(T const& obj) {
4547

4648
namespace independence {
4749
struct UpdateCompProperties {
48-
bool has_any_elements{false}; // whether the component has any elements in the update data
49-
bool ids_all_na{false}; // whether all ids are all NA
50-
bool ids_part_na{false}; // whether some ids are NA but some are not
51-
bool dense{false}; // whether the component is dense
52-
bool uniform{false}; // whether the component is uniform
53-
bool is_columnar{false}; // whether the component is columnar
54-
bool update_ids_match{false}; // whether the ids match
55-
Idx elements_ps_in_update{
56-
main_core::utils::invalid_index}; // count of elements for this component per scenario in update
57-
Idx elements_in_base{main_core::utils::invalid_index}; // count of elements for this component per scenario in input
50+
std::string name{};
51+
bool has_any_elements{false}; // whether the component has any elements in the update data
52+
bool ids_all_na{false}; // whether all ids are all NA
53+
bool ids_part_na{false}; // whether some ids are NA but some are not
54+
bool dense{false}; // whether the component is dense
55+
bool uniform{false}; // whether the component is uniform
56+
bool is_columnar{false}; // whether the component is columnar
57+
bool update_ids_match{false}; // whether the ids match
58+
Idx elements_ps_in_update{invalid_index}; // count of elements for this component per scenario in update
59+
Idx elements_in_base{invalid_index}; // count of elements for this component per scenario in input
5860

5961
constexpr bool no_id() const { return !has_any_elements || ids_all_na; }
6062
constexpr bool qualify_for_optional_id() const {
@@ -66,7 +68,7 @@ struct UpdateCompProperties {
6668
constexpr bool is_empty_component() const { return !has_any_elements; }
6769
constexpr bool is_independent() const { return qualify_for_optional_id() || provided_ids_valid(); }
6870
constexpr Idx get_n_elements() const {
69-
assert(uniform || elements_ps_in_update == main_core::utils::invalid_index);
71+
assert(uniform || elements_ps_in_update == invalid_index);
7072

7173
return qualify_for_optional_id() ? elements_ps_in_update : na_Idx;
7274
}
@@ -105,14 +107,15 @@ template <typename CompType> void process_buffer_span(auto const& all_spans, Upd
105107
template <class CompType>
106108
UpdateCompProperties check_component_independence(ConstDataset const& update_data, Idx n_component) {
107109
UpdateCompProperties properties;
108-
auto const component_idx = update_data.find_component(CompType::name, false);
109-
properties.is_columnar = update_data.is_columnar(CompType::name);
110-
properties.dense = update_data.is_dense(CompType::name);
111-
properties.uniform = update_data.is_uniform(CompType::name);
112-
properties.has_any_elements = component_idx != main_core::utils::invalid_index &&
113-
update_data.get_component_info(component_idx).total_elements > 0;
114-
properties.elements_ps_in_update = properties.uniform ? update_data.uniform_elements_per_scenario(CompType::name)
115-
: main_core::utils::invalid_index;
110+
properties.name = CompType::name;
111+
auto const component_idx = update_data.find_component(properties.name, false);
112+
properties.is_columnar = update_data.is_columnar(properties.name);
113+
properties.dense = update_data.is_dense(properties.name);
114+
properties.uniform = update_data.is_uniform(properties.name);
115+
properties.has_any_elements =
116+
component_idx != invalid_index && update_data.get_component_info(component_idx).total_elements > 0;
117+
properties.elements_ps_in_update =
118+
properties.uniform ? update_data.uniform_elements_per_scenario(properties.name) : invalid_index;
116119
properties.elements_in_base = n_component;
117120

118121
if (properties.is_columnar) {
@@ -127,37 +130,38 @@ UpdateCompProperties check_component_independence(ConstDataset const& update_dat
127130
return properties;
128131
}
129132

130-
template <class... ComponentTypes>
131-
using UpdateIndependence = std::array<UpdateCompProperties, main_core::utils::n_types<ComponentTypes...>>;
132-
133-
inline void validate_update_data_independence(UpdateCompProperties const& comp, std::string const& comp_name) {
133+
inline void validate_update_data_independence(UpdateCompProperties const& comp) {
134134
if (comp.is_empty_component()) {
135135
return; // empty dataset is still supported
136136
}
137137
auto const elements_ps = comp.get_n_elements();
138138
assert(comp.uniform || elements_ps < 0);
139139

140140
if (elements_ps >= 0 && comp.elements_in_base < elements_ps) {
141-
throw DatasetError("Update data has more elements per scenario than input data for component " + comp_name +
141+
throw DatasetError("Update data has more elements per scenario than input data for component " + comp.name +
142142
"!");
143143
}
144144
if (comp.ids_part_na) {
145-
throw DatasetError("IDs contain both numbers and NANs for component " + comp_name + " in update data!");
145+
throw DatasetError("IDs contain both numbers and NANs for component " + comp.name + " in update data!");
146146
}
147147
if (comp.ids_all_na && comp.elements_in_base != elements_ps) {
148-
throw DatasetError("Update data without IDs for component " + comp_name +
148+
throw DatasetError("Update data without IDs for component " + comp.name +
149149
" has a different number of elements per scenario then input data!");
150150
}
151151
}
152152

153-
template <class... ComponentTypes, class ComponentContainer>
154-
UpdateIndependence<ComponentTypes...> check_update_independence(MainModelState<ComponentContainer> const& state,
155-
ConstDataset const& update_data) {
156-
return utils::run_functor_with_all_types_return_array<ComponentTypes...>(
157-
[&state, &update_data]<typename CompType>() {
158-
auto const n_component = state.components.template size<CompType>();
159-
return check_component_independence<CompType>(update_data, n_component);
153+
template <class... ComponentTypes>
154+
main_core::utils::ComponentFlags<ComponentTypes...>
155+
is_update_independent(ConstDataset const& update_data, std::span<Idx const> relevant_component_count) {
156+
main_core::utils::ComponentFlags<ComponentTypes...> result{};
157+
size_t comp_idx{};
158+
utils::run_functor_with_all_types_return_void<ComponentTypes...>(
159+
[&result, &relevant_component_count, &update_data, &comp_idx]<typename CompType>() {
160+
Idx const n_component = relevant_component_count[comp_idx];
161+
result[comp_idx] = check_component_independence<CompType>(update_data, n_component).is_independent();
162+
++comp_idx;
160163
});
164+
return result;
161165
}
162166

163167
} // namespace independence
@@ -220,31 +224,32 @@ template <class... ComponentTypes, class ComponentContainer>
220224
main_core::utils::SequenceIdx<ComponentTypes...>
221225
get_all_sequence_idx_map(MainModelState<ComponentContainer> const& state, ConstDataset const& update_data,
222226
Idx scenario_idx,
223-
main_core::utils::ComponentFlags<ComponentTypes...> const& components_to_store,
224-
independence::UpdateIndependence<ComponentTypes...> const& independence, bool cached = true) {
227+
main_core::utils::ComponentFlags<ComponentTypes...> const& components_to_store) {
225228
return utils::run_functor_with_all_types_return_array<ComponentTypes...>(
226-
[&state, &update_data, scenario_idx, &components_to_store, &independence, cached]<typename CompType>() {
227-
auto const comp_properties =
228-
std::get<main_core::utils::index_of_component<CompType, ComponentTypes...>>(independence);
229-
bool const is_comp_independent =
230-
cached ? comp_properties.is_independent() : !comp_properties.is_independent();
231-
if (!std::get<main_core::utils::index_of_component<CompType, ComponentTypes...>>(components_to_store) ||
232-
!is_comp_independent) {
229+
[&update_data, &components_to_store, &state, scenario_idx]<typename CompType>() {
230+
if (!std::get<main_core::utils::index_of_component<CompType, ComponentTypes...>>(components_to_store)) {
233231
return std::vector<Idx2D>{};
234232
}
235-
independence::validate_update_data_independence(comp_properties, CompType::name);
236-
return detail::get_component_sequence<CompType>(state, update_data, scenario_idx, comp_properties);
233+
auto const n_components = state.components.template size<CompType>();
234+
auto const independence = independence::check_component_independence<CompType>(update_data, n_components);
235+
independence::validate_update_data_independence(independence);
236+
return detail::get_component_sequence<CompType>(state, update_data, scenario_idx, independence);
237237
});
238238
}
239239
// Get sequence idx map of an entire batch for fast caching of component sequences.
240240
// The sequence idx map of the batch is the same as that of the first scenario in the batch (assuming homogeneity)
241241
// This is the entry point for permanent updates.
242242
template <class... ComponentTypes, class ComponentContainer>
243243
main_core::utils::SequenceIdx<ComponentTypes...>
244-
get_all_sequence_idx_map(MainModelState<ComponentContainer> const& state, ConstDataset const& update_data,
245-
main_core::utils::ComponentFlags<ComponentTypes...> const& components_to_store,
246-
independence::UpdateIndependence<ComponentTypes...> const& independence) {
247-
return get_all_sequence_idx_map<ComponentTypes...>(state, update_data, 0, components_to_store, independence);
244+
get_all_sequence_idx_map(MainModelState<ComponentContainer> const& state, ConstDataset const& update_data) {
245+
main_core::utils::ComponentFlags<ComponentTypes...> components_to_store{};
246+
Idx comp_idx{};
247+
utils::run_functor_with_all_types_return_void<ComponentTypes...>(
248+
[&update_data, &components_to_store, &comp_idx]<typename CompType>() {
249+
components_to_store[comp_idx] = (update_data.find_component(CompType::name, false) != invalid_index);
250+
++comp_idx;
251+
});
252+
return get_all_sequence_idx_map<ComponentTypes...>(state, update_data, 0, components_to_store);
248253
}
249254

250255
// template to update components

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,22 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
172172
explicit MainModelImpl(double system_frequency, meta_data::MetaData const& meta_data)
173173
: system_frequency_{system_frequency}, meta_data_{&meta_data} {}
174174

175-
// helper function to get what components are present in the update data
176-
std::array<bool, main_core::utils::n_types<ComponentType...>>
177-
get_components_to_update(ConstDataset const& update_data) const {
178-
return main_core::utils::run_functor_with_all_types_return_array<ComponentType...>(
179-
[&update_data]<typename CompType>() {
180-
return (update_data.find_component(CompType::name, false) != main_core::utils::invalid_index);
175+
// get number
176+
template <class CompType> Idx component_count() const {
177+
assert(construction_complete_);
178+
return state_.components.template size<CompType>();
179+
}
180+
181+
// helper function to get the number of components per type
182+
std::array<Idx, main_core::utils::n_types<ComponentType...>> get_n_components_per_type() const {
183+
std::array<Idx, main_core::utils::n_types<ComponentType...>> result{};
184+
size_t idx{};
185+
main_core::utils::run_functor_with_all_types_return_void<ComponentType...>(
186+
[&result, this, &idx]<typename CompType>() {
187+
result[idx] = this->component_count<CompType>();
188+
++idx;
181189
});
190+
return result;
182191
}
183192

184193
// helper function to add vectors of components
@@ -292,11 +301,8 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
292301
}
293302
// overload to update all components in the first scenario (e.g. permanent update)
294303
template <cache_type_c CacheType> void update_components(ConstDataset const& update_data) {
295-
auto const components_to_update = get_components_to_update(update_data);
296-
auto const update_independence =
297-
main_core::update::independence::check_update_independence<ComponentType...>(state_, update_data);
298-
auto const sequence_idx_map = main_core::update::get_all_sequence_idx_map<ComponentType...>(
299-
state_, update_data, 0, components_to_update, update_independence);
304+
auto const sequence_idx_map =
305+
main_core::update::get_all_sequence_idx_map<ComponentType...>(state_, update_data);
300306
update_components<CacheType>(update_data, 0, sequence_idx_map);
301307
}
302308

@@ -382,12 +388,9 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
382388
}
383389

384390
// Entry point for main_model.hpp
391+
template <class... ComponentTypes>
385392
main_core::utils::SequenceIdx<ComponentType...> get_all_sequence_idx_map(ConstDataset const& update_data) {
386-
auto const components_to_update = get_components_to_update(update_data);
387-
auto const update_independence =
388-
main_core::update::independence::check_update_independence<ComponentType...>(state_, update_data);
389-
return main_core::update::get_all_sequence_idx_map<ComponentType...>(state_, update_data, 0,
390-
components_to_update, update_independence);
393+
return main_core::update::get_all_sequence_idx_map<ComponentType...>(state_, update_data);
391394
}
392395

393396
private:
@@ -550,15 +553,15 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
550553

551554
// cache component update order where possible.
552555
// the order for a cacheable (independent) component by definition is the same across all scenarios
553-
auto const components_to_update = get_components_to_update(update_data);
554-
auto const update_independence =
555-
main_core::update::independence::check_update_independence<ComponentType...>(state_, update_data);
556-
all_scenarios_sequence = main_core::update::get_all_sequence_idx_map<ComponentType...>(
557-
state_, update_data, 0, components_to_update, update_independence);
556+
auto const relevant_component_count = get_n_components_per_type();
557+
auto const is_independent = main_core::update::independence::is_update_independent<ComponentType...>(
558+
update_data, relevant_component_count);
559+
all_scenarios_sequence =
560+
main_core::update::get_all_sequence_idx_map<ComponentType...>(state_, update_data, 0, is_independent);
558561

559562
return [&base_model, &exceptions, &infos, &calculation_fn, &result_data, &update_data,
560-
&all_scenarios_sequence = std::as_const(all_scenarios_sequence), components_to_update,
561-
update_independence](Idx start, Idx stride, Idx n_scenarios) {
563+
&all_scenarios_sequence = std::as_const(all_scenarios_sequence),
564+
is_independent](Idx start, Idx stride, Idx n_scenarios) {
562565
assert(n_scenarios <= narrow_cast<Idx>(exceptions.size()));
563566
assert(n_scenarios <= narrow_cast<Idx>(infos.size()));
564567

@@ -571,9 +574,8 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
571574
auto model = copy_model_functor(start);
572575

573576
auto current_scenario_sequence_cache = main_core::utils::SequenceIdx<ComponentType...>{};
574-
auto [setup, winddown] =
575-
scenario_update_restore(model, update_data, components_to_update, update_independence,
576-
all_scenarios_sequence, current_scenario_sequence_cache, infos);
577+
auto [setup, winddown] = scenario_update_restore(model, update_data, is_independent, all_scenarios_sequence,
578+
current_scenario_sequence_cache, infos);
577579

578580
auto calculate_scenario = MainModelImpl::call_with<Idx>(
579581
[&model, &calculation_fn, &result_data, &infos](Idx scenario_idx) {
@@ -645,16 +647,18 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
645647
};
646648
}
647649

648-
static auto scenario_update_restore(
649-
MainModelImpl& model, ConstDataset const& update_data,
650-
main_core::utils::ComponentFlags<ComponentType...> const& components_to_store,
651-
main_core::update::independence::UpdateIndependence<ComponentType...> const& do_update_cache,
652-
main_core::utils::SequenceIdx<ComponentType...> const& all_scenario_sequence,
653-
main_core::utils::SequenceIdx<ComponentType...>& current_scenario_sequence_cache,
654-
std::vector<CalculationInfo>& infos) noexcept {
655-
main_core::utils::ComponentFlags<ComponentType...> independence_flags{};
656-
std::ranges::transform(do_update_cache, independence_flags.begin(),
657-
[](auto const& comp) { return comp.is_independent(); });
650+
static auto
651+
scenario_update_restore(MainModelImpl& model, ConstDataset const& update_data,
652+
main_core::utils::ComponentFlags<ComponentType...> const& independence_flags,
653+
main_core::utils::SequenceIdx<ComponentType...> const& all_scenario_sequence,
654+
main_core::utils::SequenceIdx<ComponentType...>& current_scenario_sequence_cache,
655+
std::vector<CalculationInfo>& infos) noexcept {
656+
auto do_update_cache = [&independence_flags] {
657+
main_core::utils::ComponentFlags<ComponentType...> result;
658+
std::ranges::transform(independence_flags, result.begin(), std::logical_not<>{});
659+
return result;
660+
}();
661+
658662
auto const scenario_sequence = [&all_scenario_sequence, &current_scenario_sequence_cache,
659663
&independence_flags]() -> SequenceIdxView {
660664
return main_core::utils::run_functor_with_all_types_return_array<ComponentType...>(
@@ -668,11 +672,11 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
668672
};
669673

670674
return std::make_pair(
671-
[&model, &update_data, scenario_sequence, &current_scenario_sequence_cache, &components_to_store,
675+
[&model, &update_data, scenario_sequence, &current_scenario_sequence_cache,
672676
do_update_cache_ = std::move(do_update_cache), &infos](Idx scenario_idx) {
673677
Timer const t_update_model(infos[scenario_idx], 1200, "Update model");
674678
current_scenario_sequence_cache = main_core::update::get_all_sequence_idx_map<ComponentType...>(
675-
model.state_, update_data, scenario_idx, components_to_store, do_update_cache_, false);
679+
model.state_, update_data, scenario_idx, do_update_cache_);
676680

677681
model.template update_components<cached_update_t>(update_data, scenario_idx, scenario_sequence());
678682
},

0 commit comments

Comments
 (0)