KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
aggregated_tree_node.hpp
Go to the documentation of this file.
1// This file is part of KaMPIng.
2//
3// Copyright 2023 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
7// version. KaMPIng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9// for more details.
10//
11// You should have received a copy of the GNU Lesser General Public License along with KaMPIng. If not, see
12// <https://www.gnu.org/licenses/>.
13
14/// @file
15/// This file contains a tree node class which can be used to represent an evaluated measurement tree.
16
17#pragma once
20
21namespace kamping::measurements {
22
23/// @brief Class representing a node in an (globally) aggregated tree, i.e., a node of a timer (or counter) tree
24/// where the global aggregation operations has been performed and which can be printed.
25///
26/// @tparam DataType Underlying data type.
27template <typename DataType>
28class AggregatedTreeNode : public internal::TreeNode<AggregatedTreeNode<DataType>> {
29public:
31
32 ///@brief Type into which the aggregated data is stored together with the applied aggregation operation.
33 using StorageType = std::unordered_map<GlobalAggregationMode, std::vector<ScalarOrContainer<DataType>>>;
34
35 /// @brief Access to stored aggregated data.
36 /// @return Reference to aggregated data.
37 auto const& aggregated_data() const {
38 return _aggregated_data;
39 }
40
41 /// @brief Add scalar of type T to aggregated data storage together with the name of the applied aggregation
42 /// operation.
43 /// @param aggregation_mode Aggregation mode that has been applied to the data.
44 /// @param data Scalar resulted from applying the given aggregation operation.
45 void add(GlobalAggregationMode aggregation_mode, std::optional<DataType> data) {
46 if (data) {
47 _aggregated_data[aggregation_mode].emplace_back(data.value());
48 }
49 }
50
51 /// @brief Add scalar of type T to aggregated data storage together with the name of the applied aggregation
52 /// operation.
53 /// @param aggregation_mode Aggregation mode that has been applied to the duration data.
54 /// @param data Vector of Scalars resulted from applying the given aggregation operation.
55 void add(GlobalAggregationMode aggregation_mode, std::vector<DataType> const& data) {
56 _aggregated_data[aggregation_mode].emplace_back(data);
57 }
58
59public:
60 StorageType _aggregated_data; ///< Storage of the aggregated data.
61};
62
63/// @brief Class representing an aggregated measurement tree, i.e., a measurement tree for which the global aggregation
64/// has been performed.
65///
66/// @tparam DataType Type of interanlly stored data.
67template <typename DataType>
69public:
70 /// @brief Globally aggregates the measurement tree provided with \param measurement_root_node across all ranks in
71 /// \param comm .
72 ///
73 /// @tparam MeasurementNode Type of the measurement tree to aggregate.
74 /// @tparam Communicator Communicator defining the scope for the global aggregation.
75 template <typename MeasurementNode, typename Communicator>
77 aggregate(_root, measurement_root_node, comm);
78 }
79
80 /// @brief Access to the root of the aggregated tree.
81 /// @return Reference to root node of aggregated tree.
82 auto& root() {
83 return _root;
84 }
85
86 /// @brief Access to the root of the aggregated tree.
87 /// @return Reference to root node of aggregated tree.
88 auto const& root() const {
89 return _root;
90 }
91
92private:
93 AggregatedTreeNode<DataType> _root; ///< Root node of aggregated tree.
94 /// @brief Traverses and evaluates the given (Measurement)TreeNode and stores the result in the corresponding
95 /// AggregatedTreeNode
96 ///
97 /// param aggregation_tree_node Node where the aggregated data points are stored.
98 /// param measurement_tree_node Node where the raw (not aggregated) data points are stored.
99 template <typename MeasurementNode, typename Communciator>
100 void aggregate(
103 Communciator const& comm
104 ) {
105 KASSERT(
106 internal::is_string_same_on_all_ranks(measurement_tree_node.name(), comm),
107 "Currently processed MeasurementTreeNode has not the same name on all ranks -> measurement trees have "
108 "diverged",
110 );
111 KASSERT(
112 comm.is_same_on_all_ranks(measurement_tree_node.measurements().size()),
113 "Currently processed MeasurementTreeNode has not the same number of measurements on all ranks -> "
114 "measurement trees have "
115 "diverged",
117 );
118
119 // gather all durations at once as gathering all durations individually may deteriorate
120 // the performance of the evaluation operation significantly.
121 auto recv_buf = comm.gatherv(send_buf(measurement_tree_node.measurements()));
122 auto const num_durations = measurement_tree_node.measurements().size();
123 for (size_t duration_idx = 0; duration_idx < num_durations; ++duration_idx) {
124 if (!comm.is_root()) {
125 continue;
126 }
127 std::vector<DataType> cur_durations;
128 cur_durations.reserve(comm.size());
129 // gather the durations belonging to the same measurement
130 for (size_t rank = 0; rank < comm.size(); ++rank) {
132 }
133
134 for (auto const& aggregation_mode: measurement_tree_node.measurements_aggregation_operations()) {
135 aggregate_measurements_globally(aggregation_mode, cur_durations, aggregation_tree_node);
136 }
137 }
138 for (auto& measurement_tree_child: measurement_tree_node.children()) {
139 auto& aggregation_tree_child = aggregation_tree_node.find_or_insert(measurement_tree_child->name());
140 aggregate(aggregation_tree_child, *measurement_tree_child.get(), comm);
141 }
142 }
143
144 /// @brief Computes the specified aggregation operation on an already gathered range of values.
145 ///
146 /// @param mode Aggregation operation to perform.
147 /// @param gathered_data Durations gathered from all participating ranks.
148 /// @param evaluation_node Object where the aggregated and evaluated measurements are stored.
149 void aggregate_measurements_globally(
150 GlobalAggregationMode mode,
151 std::vector<DataType> const& gathered_data,
153 ) {
154 switch (mode) {
155 case GlobalAggregationMode::max: {
156 using Operation = internal::Max;
157 evaluation_node.add(mode, Operation::compute(gathered_data));
158 break;
159 }
160 case GlobalAggregationMode::min: {
161 using Operation = internal::Min;
162 evaluation_node.add(mode, Operation::compute(gathered_data));
163 break;
164 }
165 case GlobalAggregationMode::sum: {
166 using Operation = internal::Sum;
167 evaluation_node.add(mode, Operation::compute(gathered_data));
168 break;
169 }
170 case GlobalAggregationMode::gather: {
171 using Operation = internal::Gather;
172 evaluation_node.add(mode, Operation::compute(gathered_data));
173 break;
174 }
175 }
176 }
177};
178
179} // namespace kamping::measurements
Wrapper for MPI communicator providing access to rank() and size() of the communicator....
Definition communicator.hpp:49
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
Class representing a node in an (globally) aggregated tree, i.e., a node of a timer (or counter) tree...
Definition aggregated_tree_node.hpp:28
auto const & aggregated_data() const
Access to stored aggregated data.
Definition aggregated_tree_node.hpp:37
void add(GlobalAggregationMode aggregation_mode, std::optional< DataType > data)
Add scalar of type T to aggregated data storage together with the name of the applied aggregation ope...
Definition aggregated_tree_node.hpp:45
void add(GlobalAggregationMode aggregation_mode, std::vector< DataType > const &data)
Add scalar of type T to aggregated data storage together with the name of the applied aggregation ope...
Definition aggregated_tree_node.hpp:55
std::unordered_map< GlobalAggregationMode, std::vector< ScalarOrContainer< DataType > > > StorageType
Type into which the aggregated data is stored together with the applied aggregation operation.
Definition aggregated_tree_node.hpp:33
StorageType _aggregated_data
Storage of the aggregated data.
Definition aggregated_tree_node.hpp:60
Class representing an aggregated measurement tree, i.e., a measurement tree for which the global aggr...
Definition aggregated_tree_node.hpp:68
auto const & root() const
Access to the root of the aggregated tree.
Definition aggregated_tree_node.hpp:88
AggregatedTree(MeasurementNode const &measurement_root_node, Communicator const &comm)
Globally aggregates the measurement tree provided with.
Definition aggregated_tree_node.hpp:76
auto & root()
Access to the root of the aggregated tree.
Definition aggregated_tree_node.hpp:82
Object representing a node in a tree. The class is not meant to be used on its own but to encapsulate...
Definition measurement_utils.hpp:108
TreeNode()
Constructs node without pointer to parent and empty name.
Definition measurement_utils.hpp:111
constexpr int heavy_communication
Assertions that perform heavyweight communication.
Definition assertion_levels.hpp:31
constexpr int light_communication
Assertions that perform lightweight communication.
Definition assertion_levels.hpp:25
auto recv_buf(Container &&container)
Passes a container, into which the received elements will be written, to the underlying call....
Definition named_parameters.hpp:859
auto send_buf(internal::ignore_t< Data > ignore)
Generates a dummy send buf that wraps a nullptr.
Definition named_parameters.hpp:51
GlobalAggregationMode
Enum to specify how time durations with same key shall be aggregated across the participating ranks.
Definition measurement_aggregation_definitions.hpp:37