Line data Source code
1 0 : // Distributed under the MIT License. 2 : // See LICENSE.txt for details. 3 : 4 : #pragma once 5 : 6 : #include <functional> 7 : #include <optional> 8 : #include <type_traits> 9 : 10 : #include "Parallel/CharmRegistration.hpp" 11 : #include "Parallel/GlobalCache.hpp" 12 : #include "Parallel/Local.hpp" 13 : #include "Parallel/PhaseControl/PhaseControlTags.hpp" 14 : #include "Parallel/PhaseControlReductionHelpers.hpp" 15 : #include "Utilities/TaggedTuple.hpp" 16 : 17 : #include "Parallel/Main.decl.h" 18 : 19 : namespace Parallel { 20 : /// @{ 21 : /// Send data from a parallel component to the Main chare for making 22 : /// phase-change decisions. 23 : /// 24 : /// This function is distinct from `Parallel::contribute_to_reduction` because 25 : /// this function contributes reduction data to the Main chare via the entry 26 : /// method `phase_change_reduction`. This must be done because the entry method 27 : /// must alter member data specific to the Main chare, and the Main chare cannot 28 : /// execute actions like most SpECTRE parallel components. 29 : /// For all cases other than sending phase-change decision data to the Main 30 : /// chare, you should use `Parallel::contribute_to_reduction`. 31 : template <typename SenderComponent, typename ArrayIndex, typename Metavariables, 32 : class... Ts> 33 1 : void contribute_to_phase_change_reduction( 34 : tuples::TaggedTuple<Ts...> data_for_reduction, 35 : Parallel::GlobalCache<Metavariables>& cache, 36 : const ArrayIndex& array_index) { 37 : using phase_change_tags_and_combines_list = 38 : PhaseControl::get_phase_change_tags<Metavariables>; 39 : using reduction_data_type = 40 : PhaseControl::reduction_data<tmpl::list<Ts...>, 41 : phase_change_tags_and_combines_list>; 42 : (void)Parallel::charmxx::RegisterReducerFunction< 43 : reduction_data_type::combine>::registrar; 44 : CkCallback callback(CProxy_Main<Metavariables>::index_t:: 45 : template redn_wrapper_phase_change_reduction< 46 : PhaseControl::TaggedTupleCombine, Ts...>(nullptr), 47 : cache.get_main_proxy().value()); 48 : reduction_data_type reduction_data{data_for_reduction}; 49 : Parallel::local( 50 : Parallel::get_parallel_component<SenderComponent>(cache)[array_index]) 51 : ->contribute(static_cast<int>(reduction_data.size()), 52 : reduction_data.packed().get(), 53 : Parallel::charmxx::charm_reducer_functions.at( 54 : std::hash<Parallel::charmxx::ReducerFunctions>{}( 55 : &reduction_data_type::combine)), 56 : callback); 57 : } 58 : 59 : template <typename SenderComponent, typename Metavariables, class... Ts> 60 1 : void contribute_to_phase_change_reduction( 61 : tuples::TaggedTuple<Ts...> data_for_reduction, 62 : Parallel::GlobalCache<Metavariables>& cache) { 63 : using phase_change_tags_and_combines_list = 64 : PhaseControl::get_phase_change_tags<Metavariables>; 65 : using reduction_data_type = 66 : PhaseControl::reduction_data<tmpl::list<Ts...>, 67 : phase_change_tags_and_combines_list>; 68 : (void)Parallel::charmxx::RegisterReducerFunction< 69 : reduction_data_type::combine>::registrar; 70 : CkCallback callback(CProxy_Main<Metavariables>::index_t:: 71 : template redn_wrapper_phase_change_reduction< 72 : PhaseControl::TaggedTupleCombine, Ts...>(nullptr), 73 : cache.get_main_proxy().value()); 74 : reduction_data_type reduction_data{data_for_reduction}; 75 : // Note that Singletons could be supported by directly calling the main 76 : // entry function, but due to this and other peculiarities with 77 : // Singletons, it is best to discourage their use. 78 : static_assert( 79 : not std::is_same_v<typename SenderComponent::chare_type, 80 : Parallel::Algorithms::Singleton>, 81 : "Phase change reduction is not supported for singleton chares. " 82 : "Consider constructing your chare as a length-1 array chare if you " 83 : "need to contribute to phase change data"); 84 : Parallel::local_branch( 85 : Parallel::get_parallel_component<SenderComponent>(cache)) 86 : ->contribute(static_cast<int>(reduction_data.size()), 87 : reduction_data.packed().get(), 88 : Parallel::charmxx::charm_reducer_functions.at( 89 : std::hash<Parallel::charmxx::ReducerFunctions>{}( 90 : &reduction_data_type::combine)), 91 : callback); 92 : } 93 : /// @} 94 : } // namespace Parallel