dune-pdelab  2.7-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 
13 namespace 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:
45  SolverStatistics(const Dune::CollectiveCommunication<MPI_Comm>& comm_)
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  const 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  const 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  const double stddev() const {
89  double s_local = (double) std::accumulate(data.begin(),data.end(),0);
90  double ss_local = (double) std::inner_product(data.begin(),data.end(),
91  data.begin(),0);
92  size_t local_size = data.size();
93  double global_size = (double) comm.sum(local_size);
94  double s_global = comm.sum(s_local);
95  double ss_global = comm.sum(ss_local);
96  return sqrt(1./(global_size-1.)*(ss_global-s_global*s_global/global_size));
97  }
98 
103  const T min() const {
104  T min_local = *std::min_element(data.begin(),data.end());
105  T min_global = comm.min(min_local);
106  return min_global;
107  }
108 
113  const T max() const {
114  T max_local = *std::max_element(data.begin(),data.end());
115  T max_global = comm.max(max_local);
116  return max_global;
117  }
118 
121  const StatisticsResult<T> result() const {
123  result.minimum = min();
124  result.maximum = max();
125  result.avg = avg();
126  result.stddev = stddev();
127  result.size = size();
128  return result;
129  }
130 
131  private:
132  // \brief local data
133  std::vector<T> data;
134  // \brief Collective communication object
135  const Dune::CollectiveCommunication<MPI_Comm>& comm;
136  };
137 
139  template <typename T>
140  std::ostream& operator<<(std::ostream& os, const StatisticsResult<T>& result) {
141  os << "#calls = " << result.size;
142  os << ", min = " << std::fixed << result.minimum;
143  os << ", avg = " << std::fixed << result.avg;
144  os << ", stddev = " << std::fixed << result.stddev;
145  os << ", max = " << std::fixed << result.maximum;
146  return os;
147  }
148 
149  } // namespace PDELab
150 } // namespace Dune
151 
152 #endif
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
std::ostream & operator<<(std::ostream &os, const StatisticsResult< T > &result)
Write statistics result to out stream.
Definition: solverstatistics.hh:140
Statistics result structure.
Definition: solverstatistics.hh:23
double stddev
Definition: solverstatistics.hh:27
T maximum
Definition: solverstatistics.hh:25
T minimum
Definition: solverstatistics.hh:24
size_t size
Definition: solverstatistics.hh:28
double avg
Definition: solverstatistics.hh:26
Class for collecting statistics over several invocations.
Definition: solverstatistics.hh:39
const size_t size() const
Total number of calls.
Definition: solverstatistics.hh:66
const double stddev() const
Calculate standard deviation.
Definition: solverstatistics.hh:88
SolverStatistics(const Dune::CollectiveCommunication< MPI_Comm > &comm_)
Create new instance of class.
Definition: solverstatistics.hh:45
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:113
const T min() const
Calculate global minimum.
Definition: solverstatistics.hh:103
const StatisticsResult< T > result() const
Convert to statistics result.
Definition: solverstatistics.hh:121
const double avg() const
Calculate global average.
Definition: solverstatistics.hh:76