@@ -316,12 +316,9 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
316
316
317
317
// update all components
318
318
template <cache_type_c CacheType>
319
- void
320
- update_component (ConstDataset const & update_data, Idx pos,
321
- std::array<std::reference_wrapper<std::vector<Idx2D> const >, n_types> const & sequence_idx_map) {
319
+ void update_component (ConstDataset const & update_data, Idx pos, SequenceIdxView const & sequence_idx_map) {
322
320
run_functor_with_all_types_return_void ([this , pos, &update_data, &sequence_idx_map]<typename CT>() {
323
- this ->update_component <CT, CacheType>(update_data, pos,
324
- std::get<index_of_component<CT>>(sequence_idx_map).get ());
321
+ this ->update_component <CT, CacheType>(update_data, pos, std::get<index_of_component<CT>>(sequence_idx_map));
325
322
});
326
323
}
327
324
template <cache_type_c CacheType>
@@ -432,36 +429,28 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
432
429
return get_sequence (buffer_span);
433
430
}
434
431
SequenceIdx get_sequence_idx_map (ConstDataset const & update_data, Idx scenario_idx,
435
- ComponentFlags const & to_store) const {
436
- // TODO: (jguo) this function could be encapsulated in UpdateCompIndependence in update.hpp
437
- return run_functor_with_all_types_return_array ([this , scenario_idx, &update_data, &to_store]<typename CT>() {
438
- if (!to_store[index_of_component<CT>]) {
439
- return std::vector<Idx2D>{};
440
- }
441
- auto const independence = check_components_independence<CT>(update_data);
442
- validate_update_data_independence (independence);
443
- return get_component_sequence<CT>(update_data, scenario_idx, independence);
444
- });
445
- }
446
- // get sequence idx map of an entire batch for fast caching of component sequences
447
- SequenceIdx get_sequence_idx_map (ConstDataset const & update_data, ComponentFlags const & to_store) const {
432
+ ComponentFlags const & components_to_store) const {
448
433
// TODO: (jguo) this function could be encapsulated in UpdateCompIndependence in update.hpp
449
- return run_functor_with_all_types_return_array ([this , &update_data, &to_store]<typename CT>() {
450
- if (!to_store[index_of_component<CT>]) {
451
- return std::vector<Idx2D>{};
452
- }
453
- auto const independence = check_components_independence<CT>(update_data);
454
- validate_update_data_independence (independence);
455
- return get_component_sequence<CT>(update_data, 0 , independence);
456
- });
434
+ return run_functor_with_all_types_return_array (
435
+ [this , scenario_idx, &update_data, &components_to_store]<typename CT>() {
436
+ if (!std::get<index_of_component<CT>>(components_to_store)) {
437
+ return std::vector<Idx2D>{};
438
+ }
439
+ auto const independence = check_components_independence<CT>(update_data);
440
+ validate_update_data_independence (independence);
441
+ return get_component_sequence<CT>(update_data, scenario_idx, independence);
442
+ });
457
443
}
444
+ // Get sequence idx map of an entire batch for fast caching of component sequences.
445
+ // The sequence idx map of the batch is the same as that of the first scenario in the batch (assuming homogeneity)
446
+ // This is the entry point for permanent updates.
458
447
SequenceIdx get_sequence_idx_map (ConstDataset const & update_data) const {
459
448
constexpr ComponentFlags all_true = [] {
460
449
ComponentFlags result{};
461
450
std::ranges::fill (result, true );
462
451
return result;
463
452
}();
464
- return get_sequence_idx_map (update_data, all_true);
453
+ return get_sequence_idx_map (update_data, 0 , all_true);
465
454
}
466
455
467
456
private:
@@ -621,9 +610,10 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
621
610
// const ref of current instance
622
611
MainModelImpl const & base_model = *this ;
623
612
624
- // cache component update order if possible
613
+ // cache component update order where possible.
614
+ // the order for a cacheable (independent) component by definition is the same across all scenarios
625
615
auto const is_independent = is_update_independent (update_data);
626
- all_scenarios_sequence = get_sequence_idx_map (update_data, is_independent);
616
+ all_scenarios_sequence = get_sequence_idx_map (update_data, 0 , is_independent);
627
617
628
618
return [&base_model, &exceptions, &infos, &calculation_fn, &result_data, &update_data,
629
619
&all_scenarios_sequence = std::as_const (all_scenarios_sequence),
@@ -640,15 +630,8 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
640
630
auto model = copy_model_functor (start);
641
631
642
632
SequenceIdx current_scenario_sequence_cache = SequenceIdx{};
643
- std::array<std::reference_wrapper<std::vector<Idx2D> const >, n_types> const current_scenario_sequence =
644
- run_functor_with_all_types_return_array (
645
- [&is_independent, &all_scenarios_sequence, ¤t_scenario_sequence_cache]<typename CT>() {
646
- constexpr auto comp_idx = index_of_component<CT>;
647
- return is_independent[comp_idx] ? std::cref (all_scenarios_sequence[comp_idx])
648
- : std::cref (current_scenario_sequence_cache[comp_idx]);
649
- });
650
- auto [setup, winddown] = scenario_update_restore (model, update_data, current_scenario_sequence,
651
- current_scenario_sequence_cache, is_independent, infos);
633
+ auto [setup, winddown] = scenario_update_restore (model, update_data, is_independent, all_scenarios_sequence,
634
+ current_scenario_sequence_cache, infos);
652
635
653
636
auto calculate_scenario = MainModelImpl::call_with<Idx>(
654
637
[&model, &calculation_fn, &result_data, &infos](Idx scenario_idx) {
@@ -720,28 +703,41 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
720
703
};
721
704
}
722
705
723
- static auto scenario_update_restore (
724
- MainModelImpl& model, ConstDataset const & update_data,
725
- std::array<std::reference_wrapper<std::vector<Idx2D> const >, n_types> const & scenario_sequence,
726
- SequenceIdx& current_scenario_sequence_cache, ComponentFlags const & is_independent,
727
- std::vector<CalculationInfo>& infos) noexcept {
706
+ static auto scenario_update_restore (MainModelImpl& model, ConstDataset const & update_data,
707
+ ComponentFlags const & is_independent, SequenceIdx const & all_scenario_sequence,
708
+ SequenceIdx& current_scenario_sequence_cache,
709
+ std::vector<CalculationInfo>& infos) noexcept {
728
710
auto do_update_cache = [&is_independent] {
729
711
ComponentFlags result;
730
712
std::ranges::transform (is_independent, result.begin (), std::logical_not<>{});
731
713
return result;
732
714
}();
733
715
716
+ auto const scenario_sequence = [&all_scenario_sequence, ¤t_scenario_sequence_cache,
717
+ &is_independent]() -> SequenceIdxView {
718
+ return run_functor_with_all_types_return_array (
719
+ [&all_scenario_sequence, ¤t_scenario_sequence_cache, &is_independent]<typename CT>() {
720
+ constexpr auto comp_idx = index_of_component<CT>;
721
+ if (std::get<comp_idx>(is_independent)) {
722
+ return std::span<Idx2D const >{std::get<comp_idx>(all_scenario_sequence)};
723
+ }
724
+ return std::span<Idx2D const >{std::get<comp_idx>(current_scenario_sequence_cache)};
725
+ });
726
+ };
727
+
734
728
return std::make_pair (
735
- [&model, &update_data, & scenario_sequence, ¤t_scenario_sequence_cache,
729
+ [&model, &update_data, scenario_sequence, ¤t_scenario_sequence_cache,
736
730
do_update_cache_ = std::move (do_update_cache), &infos](Idx scenario_idx) {
737
731
Timer const t_update_model (infos[scenario_idx], 1200 , " Update model" );
738
732
current_scenario_sequence_cache =
739
733
model.get_sequence_idx_map (update_data, scenario_idx, do_update_cache_);
740
- model.template update_component <cached_update_t >(update_data, scenario_idx, scenario_sequence);
734
+
735
+ model.template update_component <cached_update_t >(update_data, scenario_idx, scenario_sequence ());
741
736
},
742
- [&model, & scenario_sequence, ¤t_scenario_sequence_cache, &infos](Idx scenario_idx) {
737
+ [&model, scenario_sequence, ¤t_scenario_sequence_cache, &infos](Idx scenario_idx) {
743
738
Timer const t_update_model (infos[scenario_idx], 1201 , " Restore model" );
744
- model.restore_components (scenario_sequence);
739
+
740
+ model.restore_components (scenario_sequence ());
745
741
std::ranges::for_each (current_scenario_sequence_cache,
746
742
[](auto & comp_seq_idx) { comp_seq_idx.clear (); });
747
743
});
0 commit comments