PhaseControlReductionHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include "Parallel/Reduction.hpp"
7 #include "Parallel/Serialize.hpp"
8 #include "Utilities/Registration.hpp"
9 #include "Utilities/TMPL.hpp"
10 #include "Utilities/TaggedTuple.hpp"
11 
12 namespace PhaseControl {
13 
14 /// A type for denoting a piece of data for deciding a phase change.
15 ///
16 /// `Tag` is intended to be a tag (with a type) for indexing a
17 /// `tuples::TaggedTuple`, and `CombineMethod` is intended to be a
18 /// `Parallel::ReductionDatum`-compatible invokable for combining the `type` of
19 /// the `Tag`. The `MainCombineMethod` is provided to give flexibility for a
20 /// different method of combination at the top level of the hierarchy (so, in
21 /// the case of phase control reductions, performed by the main chare to combine
22 /// reductions from different chares)
23 template <typename Tag, typename CombineMethod,
24  typename MainCombineMethod = CombineMethod>
25 struct TagAndCombine : Tag {
26  using tag = Tag;
27  using combine_method = CombineMethod;
28  using main_combine_method = MainCombineMethod;
29 };
30 
31 /// A flexible combine invokable that combines into a `tuples::TaggedTuple` a
32 /// new `tuples::TaggedTuple`, and combines according to type aliases
33 /// `combination_method`s that are required to be defined in each tag.
35  template <typename... Tags>
36  tuples::TaggedTuple<Tags...> operator()(
37  tuples::TaggedTuple<Tags...> current_state,
38  const tuples::TaggedTuple<Tags...>& element) {
39  tmpl::for_each<tmpl::list<Tags...>>(
40  [&current_state, &element](auto tag_v) noexcept {
41  using tag = typename decltype(tag_v)::type;
42  tuples::get<tag>(current_state) = typename tag::combine_method{}(
43  tuples::get<tag>(current_state), tuples::get<tag>(element));
44  });
45  return current_state;
46  }
47 };
48 
49 /// A flexible combine invokable that combines into a `tuples::TaggedTuple` a
50 /// new `tuples::TaggedTuple` with a subset of the original tags, and combines
51 /// according to type aliases `main_combine_method`s that are required to be
52 /// defined in each tag.
53 ///
54 /// \note This is _not_ usable with charm++ reductions; it mutates the current
55 /// state in-place. This is constructed for the use-case where the main chare
56 /// stores a persistent data structure and combines reduction data as it arrives
57 /// from the other chares.
59  template <typename... CurrentTags, typename... CombineTags>
60  static void apply(
62  const tuples::TaggedTuple<CombineTags...>& element) {
63  tmpl::for_each<tmpl::list<CombineTags...>>([&current_state,
64  &element](auto tag_v) noexcept {
65  using tag = typename decltype(tag_v)::type;
66  tuples::get<tag>(*current_state) = typename tag::main_combine_method{}(
67  tuples::get<tag>(*current_state), tuples::get<tag>(element));
68  });
69  }
70 };
71 
72 /// A `Parallel::ReductionData` with a single `Parallel::ReductionDatum` for a
73 /// given tagged tuple type determined by `TagsPresent`, and performs the
74 /// combine according to `TagsAndCombines`, which must be a `tmpl::list` of
75 /// `PhaseControl::TagAndCombine`s.
76 ///
77 /// Each tag in the `TagsAndCombinesPresent` may either be a `TagsAndCombines`
78 /// or otherise define all three type traits `type`, `combine_method`, and
79 /// `main_combine_method`.
80 template <typename TagsAndCombinesPresent, typename TagsAndCombines>
81 using reduction_data = Parallel::ReductionData<Parallel::ReductionDatum<
82  tuples::tagged_tuple_from_typelist<TagsAndCombinesPresent>,
84 } // namespace PhaseControl
PhaseControl
Contains utilities for determining control-flow among phases.
Definition: ExecutePhaseChange.hpp:17
Parallel::ReductionDatum
The data to be reduced, and invokables to be called whenever two reduction messages are combined and ...
Definition: Reduction.hpp:65
PhaseControl::TaggedTupleCombine
A flexible combine invokable that combines into a tuples::TaggedTuple a new tuples::TaggedTuple,...
Definition: PhaseControlReductionHelpers.hpp:34
Serialize.hpp
PhaseControl::TaggedTupleMainCombine
A flexible combine invokable that combines into a tuples::TaggedTuple a new tuples::TaggedTuple with ...
Definition: PhaseControlReductionHelpers.hpp:58
tuples::TaggedTuple
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:271
PhaseControl::TagAndCombine
A type for denoting a piece of data for deciding a phase change.
Definition: PhaseControlReductionHelpers.hpp:25
PhaseControl::reduction_data
Parallel::ReductionData< Parallel::ReductionDatum< tuples::tagged_tuple_from_typelist< TagsAndCombinesPresent >, TaggedTupleCombine > > reduction_data
A Parallel::ReductionData with a single Parallel::ReductionDatum for a given tagged tuple type determ...
Definition: PhaseControlReductionHelpers.hpp:83
TMPL.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: ReadSpecPiecewisePolynomial.hpp:13