DUNE PDELab (git)

solverstatistics.hh
Go to the documentation of this file.
1// -*- tab-width: 2; indent-tabs-mode: nil -*-
2// vi: set et ts=2 sw=2 sts=2:
3
4#ifndef DUNE_PDELAB_BACKEND_ISTL_MATRIXFREE_SOLVERSTATISTICS_HH
5#define DUNE_PDELAB_BACKEND_ISTL_MATRIXFREE_SOLVERSTATISTICS_HH
6
7#include <vector>
8#include <algorithm>
9#include <numeric>
10#include <iostream>
11
12
13namespace Dune{
14 namespace PDELab{
15
22 template <typename T>
24 T minimum; // Global minimum
25 T maximum; // Global maximum
26 double avg; // Global average
27 double stddev; // Standard deviation
28 size_t size; // Total number of invocations
29 };
30
38 template <typename T>
40 public:
46 : data(), comm(comm_) {}
47
52 void append(const T x) {
53 data.push_back(x);
54 }
55
58 void clear() {
59 data.clear();
60 }
61
66 size_t size() const {
67 size_t local_size = data.size();
68 size_t global_size = comm.sum(local_size);
69 return global_size;
70 }
71
76 double avg() const {
77 double s_local = (double) std::accumulate(data.begin(),data.end(),0);
78 size_t local_size = data.size();
79 double global_size = (double) comm.sum(local_size);
80 double s_global = comm.sum(s_local);
81 return s_global / global_size;
82 }
83
88 double stddev() const {
89 using std::sqrt;
90 double s_local = (double) std::accumulate(data.begin(),data.end(),0);
91 double ss_local = (double) std::inner_product(data.begin(),data.end(),
92 data.begin(),0);
93 size_t local_size = data.size();
94 double global_size = (double) comm.sum(local_size);
95 double s_global = comm.sum(s_local);
96 double ss_global = comm.sum(ss_local);
97 return sqrt(1./(global_size-1.)*(ss_global-s_global*s_global/global_size));
98 }
99
104 const T min() const {
105 T min_local = *std::min_element(data.begin(),data.end());
106 T min_global = comm.min(min_local);
107 return min_global;
108 }
109
114 const T max() const {
115 T max_local = *std::max_element(data.begin(),data.end());
116 T max_global = comm.max(max_local);
117 return max_global;
118 }
119
124 result.minimum = min();
125 result.maximum = max();
126 result.avg = avg();
127 result.stddev = stddev();
128 result.size = size();
129 return result;
130 }
131
132 private:
133 // \brief local data
134 std::vector<T> data;
135 // \brief Collective communication object
137 };
138
140 template <typename T>
141 std::ostream& operator<<(std::ostream& os, const StatisticsResult<T>& result) {
142 os << "#calls = " << result.size;
143 os << ", min = " << std::fixed << result.minimum;
144 os << ", avg = " << std::fixed << result.avg;
145 os << ", stddev = " << std::fixed << result.stddev;
146 os << ", max = " << std::fixed << result.maximum;
147 return os;
148 }
149
150 } // namespace PDELab
151} // namespace Dune
152
153#endif
Specialization of Communication for MPI.
Definition: mpicommunication.hh:108
T max(const T &in) const
Compute the maximum of the argument over all processes and return the result in every process....
Definition: mpicommunication.hh:257
T sum(const T &in) const
Compute the sum of the argument over all processes and return the result in every process....
Definition: mpicommunication.hh:208
T min(const T &in) const
Compute the minimum of the argument over all processes and return the result in every process....
Definition: mpicommunication.hh:240
Class for collecting statistics over several invocations.
Definition: solverstatistics.hh:39
double stddev() const
Calculate standard deviation.
Definition: solverstatistics.hh:88
const StatisticsResult< T > result() const
Convert to statistics result.
Definition: solverstatistics.hh:122
void append(const T x)
Add new data point.
Definition: solverstatistics.hh:52
void clear()
clear out data
Definition: solverstatistics.hh:58
const T max() const
Calculate global maximum.
Definition: solverstatistics.hh:114
SolverStatistics(const Dune::Communication< MPI_Comm > &comm_)
Create new instance of class.
Definition: solverstatistics.hh:45
const T min() const
Calculate global minimum.
Definition: solverstatistics.hh:104
double avg() const
Calculate global average.
Definition: solverstatistics.hh:76
size_t size() const
Total number of calls.
Definition: solverstatistics.hh:66
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:279
Dune namespace.
Definition: alignedallocator.hh:13
Statistics result structure.
Definition: solverstatistics.hh:23
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)