Observe.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include "IO/Observer/ObservationId.hpp"
12 #include "IO/Observer/ObserverComponent.hpp"
13 #include "IO/Observer/ReductionActions.hpp"
14 #include "IO/Observer/TypeOfObservation.hpp"
15 #include "Parallel/GlobalCache.hpp"
16 #include "Parallel/Info.hpp"
17 #include "Parallel/Invoke.hpp"
18 #include "Parallel/Reduction.hpp"
20 #include "Utilities/Functional.hpp"
21 #include "Utilities/PrettyType.hpp"
22 
23 namespace LinearSolver {
24 namespace observe_detail {
25 
26 using reduction_data = Parallel::ReductionData<
27  // Iteration
29  // Residual
31 
32 template <typename OptionsGroup>
33 struct Registration {
34  template <typename ParallelComponent, typename DbTagsList,
35  typename ArrayIndex>
37  register_info(const db::DataBox<DbTagsList>& /*box*/,
38  const ArrayIndex& /*array_index*/) noexcept {
40  observers::ObservationKey{pretty_type::get_name<OptionsGroup>()}};
41  }
42 };
43 
44 /*!
45  * \brief Contributes data from the residual monitor to the reduction observer
46  */
47 template <typename OptionsGroup, typename ParallelComponent,
48  typename Metavariables>
49 void contribute_to_reduction_observer(
50  const size_t iteration_id, const double residual_magnitude,
52  const auto observation_id = observers::ObservationId(
53  iteration_id, pretty_type::get_name<OptionsGroup>());
54  auto& reduction_writer = Parallel::get_parallel_component<
56  auto& my_proxy = Parallel::get_parallel_component<ParallelComponent>(cache);
57  Parallel::threaded_action<observers::ThreadedActions::WriteReductionData>(
58  // Node 0 is always the writer, so directly call the component on that
59  // node
60  reduction_writer[0], observation_id,
61  static_cast<size_t>(Parallel::my_node(*my_proxy.ckLocal())),
62  // When multiple linear solves are performed, e.g. for the nonlinear
63  // solver, we'll need to write into separate subgroups, e.g.:
64  // `/linear_residuals/<nonlinear_iteration_id>`
65  std::string{"/" + Options::name<OptionsGroup>() + "Residuals"},
66  std::vector<std::string>{"Iteration", "Residual"},
67  reduction_data{iteration_id, residual_magnitude});
68 }
69 
70 } // namespace observe_detail
71 } // namespace LinearSolver
observers::ObservationId
A unique identifier for an observation representing the type of observation and the instance (e....
Definition: ObservationId.hpp:71
std::string
utility
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:15
Parallel::ReductionDatum
The data to be reduced, and invokables to be called whenever two reduction messages are combined and ...
Definition: Reduction.hpp:65
std::pair
GlobalCache.hpp
Parallel::get_parallel_component
auto get_parallel_component(GlobalCache< Metavariables > &cache) noexcept -> Parallel::proxy_from_parallel_component< GlobalCache_detail::get_component_if_mocked< typename Metavariables::component_list, ParallelComponentTag >> &
Access the Charm++ proxy associated with a ParallelComponent.
Definition: GlobalCache.hpp:535
vector
PrettyType.hpp
observers::ObservationKey
Used as a key in maps to keep track of how many elements have registered.
Definition: ObservationId.hpp:28
Info.hpp
Tags.hpp
observers::ObserverWriter
The nodegroup parallel component that is responsible for writing data to disk.
Definition: ObserverComponent.hpp:51
Parallel::my_node
int my_node(const DistribObject &distributed_object) noexcept
Index of my node.
Definition: Info.hpp:51
cstddef
ActionTesting::cache
Parallel::GlobalCache< Metavariables > & cache(MockRuntimeSystem< Metavariables > &runner, const ArrayIndex &array_index) noexcept
Returns the GlobalCache of Component with index array_index.
Definition: MockRuntimeSystemFreeFunctions.hpp:382
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
LinearSolver
Functionality for solving linear systems of equations.
Definition: ExplicitInverse.hpp:20
observers::TypeOfObservation::Reduction
@ Reduction
The sender will only perform reduction observations.
Registration
Helpers for derived class registration.
Definition: Registration.hpp:32
string