BioDynaMo  v1.05.119-a4ff3934
timing.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_H_
16 #define CORE_UTIL_TIMING_H_
17 
18 #include <chrono>
19 #include <iostream>
20 #include <string>
21 
22 #include "core/param/param.h"
23 #include "core/scheduler.h"
24 #include "core/simulation.h"
26 
27 namespace bdm {
28 
29 class Timing {
30  public:
31  typedef std::chrono::high_resolution_clock Clock;
32 
33  static int64_t Timestamp() {
34  using std::chrono::duration_cast;
35  using std::chrono::milliseconds;
36  auto time = Clock::now();
37  auto since_epoch = time.time_since_epoch();
38  auto millis = duration_cast<milliseconds>(since_epoch);
39  return millis.count();
40  }
41 
42  template <typename TFunctor>
43  static void Time(const std::string& description, TFunctor&& f) {
44  static bool kUseTimer = Simulation::GetActive()->GetParam()->statistics;
45  if (kUseTimer) {
46  auto* agg = Simulation::GetActive()->GetScheduler()->GetOpTimes();
47  Timing timing(description, agg);
48  f();
49  } else {
50  f();
51  }
52  }
53 
54  explicit Timing(const std::string& description = "")
55  : start_{Timestamp()}, text_{description} {}
56 
57  Timing(const std::string& description, TimingAggregator* aggregator)
58  : start_{Timestamp()}, text_{description}, aggregator_{aggregator} {}
59 
60  ~Timing() {
61  int64_t duration = (Timestamp() - start_);
62  if (aggregator_ == nullptr) {
63  std::cout << text_ << " " << duration << " ms" << std::endl;
64  } else {
65  aggregator_->AddEntry(text_, duration);
66  }
67  }
68 
69  private:
70  int64_t start_;
71  std::string text_;
73 };
74 
75 } // namespace bdm
76 
77 #endif // CORE_UTIL_TIMING_H_
timing_aggregator.h
bdm
Definition: agent.cc:39
bdm::Scheduler::GetOpTimes
TimingAggregator * GetOpTimes()
Definition: scheduler.cc:162
scheduler.h
bdm::Timing::start_
int64_t start_
Definition: timing.h:70
bdm::Timing
Definition: timing.h:29
bdm::Timing::Timing
Timing(const std::string &description="")
Definition: timing.h:54
bdm::Simulation::GetScheduler
Scheduler * GetScheduler()
Definition: simulation.cc:262
bdm::Timing::text_
std::string text_
Definition: timing.h:71
bdm::TimingAggregator::AddEntry
void AddEntry(const std::string &key, int64_t value)
Definition: timing_aggregator.h:35
simulation.h
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:254
bdm::Timing::Timing
Timing(const std::string &description, TimingAggregator *aggregator)
Definition: timing.h:57
bdm::Timing::~Timing
~Timing()
Definition: timing.h:60
bdm::Timing::Timestamp
static int64_t Timestamp()
Definition: timing.h:33
param.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::Timing::aggregator_
TimingAggregator * aggregator_
Definition: timing.h:72
bdm::Timing::Clock
std::chrono::high_resolution_clock Clock
Definition: timing.h:31
bdm::Timing::Time
static void Time(const std::string &description, TFunctor &&f)
Definition: timing.h:43
bdm::Param::statistics
bool statistics
Definition: param.h:568
bdm::TimingAggregator
Definition: timing_aggregator.h:30