Skip to content

Commit 8287cb8

Browse files
committed
re-introduce model_reference_
Signed-off-by: Santiago Figueroa Manrique <[email protected]>
1 parent 57f924a commit 8287cb8

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/job_adapter.hpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ template <class MainModel, class... ComponentType>
2121
class JobDispatchAdapter<MainModel, ComponentList<ComponentType...>>
2222
: public JobDispatchInterface<JobDispatchAdapter<MainModel, ComponentList<ComponentType...>>> {
2323
public:
24-
JobDispatchAdapter(MainModel& model, std::reference_wrapper<MainModelOptions const> options)
25-
: model_{std::make_unique<MainModel>(model)}, options_{options} {}
24+
JobDispatchAdapter(std::reference_wrapper<MainModel> model_reference,
25+
std::reference_wrapper<MainModelOptions const> options)
26+
: model_reference_{model_reference}, options_{options} {}
2627
JobDispatchAdapter(JobDispatchAdapter const& other)
27-
: model_{std::make_unique<MainModel>(*other.model_)},
28+
: model_copy_{std::make_unique<MainModel>(other.model_reference_.get())},
29+
model_reference_{std::ref(*model_copy_)},
2830
options_{std::ref(other.options_)},
2931
components_to_update_{other.components_to_update_},
3032
update_independence_{other.update_independence_},
3133
independence_flags_{other.independence_flags_},
3234
all_scenarios_sequence_{other.all_scenarios_sequence_} {}
3335
JobDispatchAdapter& operator=(JobDispatchAdapter const& other) {
3436
if (this != &other) {
35-
model_ = std::make_unique<MainModel>(*other.model_);
37+
model_copy_ = std::make_unique<MainModel>(other.model_reference_.get());
38+
model_reference_ = std::ref(*model_copy_);
3639
options_ = std::ref(other.options_);
3740
components_to_update_ = other.components_to_update_;
3841
update_independence_ = other.update_independence_;
@@ -42,15 +45,17 @@ class JobDispatchAdapter<MainModel, ComponentList<ComponentType...>>
4245
return *this;
4346
}
4447
JobDispatchAdapter(JobDispatchAdapter&& other) noexcept
45-
: model_{std::move(other.model_)},
48+
: model_copy_{std::move(other.model_copy_)},
49+
model_reference_{model_copy_ ? std::ref(*model_copy_) : std::move(other.model_reference_)},
4650
options_{other.options_},
4751
components_to_update_{std::move(other.components_to_update_)},
4852
update_independence_{std::move(other.update_independence_)},
4953
independence_flags_{std::move(other.independence_flags_)},
5054
all_scenarios_sequence_{std::move(other.all_scenarios_sequence_)} {}
5155
JobDispatchAdapter& operator=(JobDispatchAdapter&& other) noexcept {
5256
if (this != &other) {
53-
model_ = std::move(other.model_);
57+
model_copy_ = std::move(other.model_copy_);
58+
model_reference_ = model_copy_ ? std::ref(*model_copy_) : std::move(other.model_reference_);
5459
options_ = other.options_;
5560
components_to_update_ = std::move(other.components_to_update_);
5661
update_independence_ = std::move(other.update_independence_);
@@ -59,15 +64,16 @@ class JobDispatchAdapter<MainModel, ComponentList<ComponentType...>>
5964
}
6065
return *this;
6166
}
62-
~JobDispatchAdapter() { model_.reset(); }
67+
~JobDispatchAdapter() { model_copy_.reset(); }
6368

6469
private:
6570
// Grant the CRTP base (JobDispatchInterface<JobDispatchAdapter>) access to
6671
// JobDispatchAdapter's private members. This allows the base class template
6772
// to call derived-class implementation details as part of the CRTP pattern.
6873
friend class JobDispatchInterface<JobDispatchAdapter>;
6974

70-
std::unique_ptr<MainModel> model_;
75+
std::unique_ptr<MainModel> model_copy_;
76+
std::reference_wrapper<MainModel> model_reference_;
7177
std::reference_wrapper<MainModelOptions const> options_;
7278

7379
main_core::utils::ComponentFlags<ComponentType...> components_to_update_{};
@@ -80,18 +86,19 @@ class JobDispatchAdapter<MainModel, ComponentList<ComponentType...>>
8086
std::mutex calculation_info_mutex_;
8187

8288
void calculate_impl(MutableDataset const& result_data, Idx scenario_idx) const {
83-
MainModel::calculator(options_.get(), *model_, result_data.get_individual_scenario(scenario_idx), false);
89+
MainModel::calculator(options_.get(), model_reference_.get(), result_data.get_individual_scenario(scenario_idx),
90+
false);
8491
}
8592

8693
void cache_calculate_impl() const {
8794
// calculate once to cache topology, ignore results, all math solvers are initialized
8895
try {
89-
MainModel::calculator(options_.get(), *model_,
96+
MainModel::calculator(options_.get(), model_reference_.get(),
9097
{
9198
false,
9299
1,
93100
"sym_output",
94-
model_->meta_data(),
101+
model_reference_.get().meta_data(),
95102
},
96103
true);
97104
} catch (SparseMatrixError const&) { // NOLINT(bugprone-empty-catch) // NOSONAR
@@ -104,33 +111,35 @@ class JobDispatchAdapter<MainModel, ComponentList<ComponentType...>>
104111
void prepare_job_dispatch_impl(ConstDataset const& update_data) {
105112
// cache component update order where possible.
106113
// the order for a cacheable (independent) component by definition is the same across all scenarios
107-
components_to_update_ = model_->get_components_to_update(update_data);
108-
update_independence_ =
109-
main_core::update::independence::check_update_independence<ComponentType...>(model_->state(), update_data);
114+
components_to_update_ = model_reference_.get().get_components_to_update(update_data);
115+
update_independence_ = main_core::update::independence::check_update_independence<ComponentType...>(
116+
model_reference_.get().state(), update_data);
110117
std::ranges::transform(update_independence_, independence_flags_.begin(),
111118
[](auto const& comp) { return comp.is_independent(); });
112119
all_scenarios_sequence_ = std::make_shared<main_core::utils::SequenceIdx<ComponentType...>>(
113120
main_core::update::get_all_sequence_idx_map<ComponentType...>(
114-
model_->state(), update_data, 0, components_to_update_, update_independence_, false));
121+
model_reference_.get().state(), update_data, 0, components_to_update_, update_independence_, false));
115122
}
116123

117124
void setup_impl(ConstDataset const& update_data, Idx scenario_idx) {
118125
current_scenario_sequence_cache_ = main_core::update::get_all_sequence_idx_map<ComponentType...>(
119-
model_->state(), update_data, scenario_idx, components_to_update_, update_independence_, true);
126+
model_reference_.get().state(), update_data, scenario_idx, components_to_update_, update_independence_,
127+
true);
120128
auto const current_scenario_sequence = get_current_scenario_sequence_view_();
121-
model_->template update_components<cached_update_t>(update_data, scenario_idx, current_scenario_sequence);
129+
model_reference_.get().template update_components<cached_update_t>(update_data, scenario_idx,
130+
current_scenario_sequence);
122131
}
123132

124133
void winddown_impl() {
125-
model_->restore_components(get_current_scenario_sequence_view_());
134+
model_reference_.get().restore_components(get_current_scenario_sequence_view_());
126135
std::ranges::for_each(current_scenario_sequence_cache_, [](auto& comp_seq_idx) { comp_seq_idx.clear(); });
127136
}
128137

129-
CalculationInfo get_calculation_info_impl() const { return model_->calculation_info(); }
138+
CalculationInfo get_calculation_info_impl() const { return model_reference_.get().calculation_info(); }
130139

131140
void thread_safe_add_calculation_info_impl(CalculationInfo const& info) {
132141
std::lock_guard const lock{calculation_info_mutex_};
133-
model_->merge_calculation_info(info);
142+
model_reference_.get().merge_calculation_info(info);
134143
}
135144

136145
auto get_current_scenario_sequence_view_() const {

power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MainModel {
7878
*/
7979
BatchParameter calculate(Options const& options, MutableDataset const& result_data,
8080
ConstDataset const& update_data) {
81-
JobDispatchAdapter<Impl, AllComponents> adapter{impl(), std::ref(options)};
81+
JobDispatchAdapter<Impl, AllComponents> adapter{std::ref(impl()), std::ref(options)};
8282
return JobDispatch::batch_calculation(adapter, result_data, update_data, options.threading);
8383
}
8484

0 commit comments

Comments
 (0)