BioDynaMo  v1.03.58-27764645
timing_aggregator.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_TIMING_AGGREGATOR_H_
16 #define CORE_UTIL_TIMING_AGGREGATOR_H_
17 
18 #include <map>
19 #include <ostream>
20 #include <string>
21 #include <vector>
22 
23 #include "TSystem.h"
24 
25 #include "core/simulation.h"
26 #include "core/util/math.h"
27 
28 namespace bdm {
29 
31  public:
32  TimingAggregator() = default;
33  ~TimingAggregator() = default;
34 
35  void AddEntry(const std::string& key, int64_t value) {
36  if (!timings_.count(key)) {
37  std::vector<int64_t> data;
38  data.push_back(value);
39  timings_[key] = data;
40  } else {
41  timings_[key].push_back(value);
42  }
43  }
44 
45  void AddDescription(const std::string& text) {
46  descriptions_.push_back(text);
47  }
48 
49  int operator[](std::string idx) {
50  auto sum = std::accumulate(timings_[idx].begin(), timings_[idx].end(), 0);
51  return sum;
52  }
53 
54  private:
55  std::map<std::string, std::vector<int64_t>> timings_;
56  std::vector<std::string> descriptions_;
58 
59  friend std::ostream& operator<<(std::ostream& os, const TimingAggregator& p);
60 };
61 
62 inline std::ostream& operator<<(std::ostream& os, const TimingAggregator& ta) {
63  os << std::endl;
64 
65  if (ta.timings_.size() != 0) {
66  os << "\033[1mTotal execution time per operation\033[0m" << std::endl;
67  for (auto& timing : ta.timings_) {
68  os << timing.first << ": "
69  << std::accumulate(timing.second.begin(), timing.second.end(), 0)
70  << std::endl;
71  }
72  } else {
73  os << "No statistics were gathered!" << std::endl;
74  }
75  return os;
76 }
77 } // namespace bdm
78 
79 #endif // CORE_UTIL_TIMING_AGGREGATOR_H_
bdm::TimingAggregator::descriptions_
std::vector< std::string > descriptions_
Definition: timing_aggregator.h:56
bdm
Definition: agent.cc:39
bdm::TimingAggregator::~TimingAggregator
~TimingAggregator()=default
bdm::TimingAggregator::timings_
std::map< std::string, std::vector< int64_t > > timings_
Definition: timing_aggregator.h:55
math.h
bdm::TimingAggregator::operator[]
int operator[](std::string idx)
Definition: timing_aggregator.h:49
bdm::TimingAggregator::AddEntry
void AddEntry(const std::string &key, int64_t value)
Definition: timing_aggregator.h:35
bdm::TimingAggregator::operator<<
friend std::ostream & operator<<(std::ostream &os, const TimingAggregator &p)
Definition: timing_aggregator.h:62
bdm::operator<<
std::ostream & operator<<(std::ostream &o, const MathArray< T, N > &arr)
Definition: math_array.h:412
bdm::TimingAggregator::TimingAggregator
TimingAggregator()=default
bdm::TimingAggregator::AddDescription
void AddDescription(const std::string &text)
Definition: timing_aggregator.h:45
simulation.h
bdm::TimingAggregator::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(TimingAggregator, 1)
bdm::TimingAggregator
Definition: timing_aggregator.h:30