votca 2026-dev
Loading...
Searching...
No Matches
dfttimings.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#pragma once
21#ifndef VOTCA_XTP_DFTTIMINGS_H
22#define VOTCA_XTP_DFTTIMINGS_H
23
24// Standard includes
25#include <chrono>
26#include <fstream>
27#include <string>
28#include <vector>
29
30// Local VOTCA includes
31#include "logger.h"
32
33namespace votca {
34namespace xtp {
35
46 public:
47 using Clock = std::chrono::steady_clock;
48
49 class Scope {
50 public:
51 Scope(DFTTimings& timings, std::string name)
52 : timings_(timings), name_(std::move(name)), start_(Clock::now()) {
53 timings_.nested_.push_back(0.0);
54 }
56 double elapsed =
57 std::chrono::duration<double>(Clock::now() - start_).count();
58 double nested = timings_.nested_.back();
59 timings_.nested_.pop_back();
60 timings_.AddEntry(name_, elapsed - nested);
61 if (!timings_.nested_.empty()) {
62 timings_.nested_.back() += elapsed;
63 }
64 }
65 Scope(const Scope&) = delete;
66 Scope& operator=(const Scope&) = delete;
67
68 private:
70 std::string name_;
71 Clock::time_point start_;
72 };
73
74 Scope Measure(const std::string& name) { return Scope(*this, name); }
75
78 void Add(const std::string& name, double seconds) {
79 AddEntry(name, seconds);
80 if (!nested_.empty()) {
81 nested_.back() += seconds;
82 }
83 }
84
85 void Reset() {
86 entries_.clear();
87 nested_.clear();
88 start_ = Clock::now();
89 }
90
91 double Elapsed() const {
92 return std::chrono::duration<double>(Clock::now() - start_).count();
93 }
94
96 void Report(Logger& log, Log::Level level) const;
97
101 static double ResidentMemoryGB(bool peak);
102
103 private:
104 void AddEntry(const std::string& name, double seconds) {
105 for (Entry& e : entries_) {
106 if (e.name == name) {
107 e.seconds += seconds;
108 ++e.calls;
109 return;
110 }
111 }
112 entries_.push_back({name, seconds, 1});
113 }
114
115 struct Entry {
116 std::string name;
117 double seconds;
118 long calls;
119 };
120 std::vector<Entry> entries_;
121 std::vector<double> nested_; // time of finished inner scopes, per level
122 Clock::time_point start_ = Clock::now();
123};
124
125} // namespace xtp
126} // namespace votca
127
128#endif // VOTCA_XTP_DFTTIMINGS_H
Scope(const Scope &)=delete
Scope(DFTTimings &timings, std::string name)
Definition dfttimings.h:51
Scope & operator=(const Scope &)=delete
Clock::time_point start_
Definition dfttimings.h:71
Wall-clock accounting for the parts of a DFT run.
Definition dfttimings.h:45
void AddEntry(const std::string &name, double seconds)
Definition dfttimings.h:104
std::vector< Entry > entries_
Definition dfttimings.h:120
std::vector< double > nested_
Definition dfttimings.h:121
void Add(const std::string &name, double seconds)
Definition dfttimings.h:78
void Report(Logger &log, Log::Level level) const
Table of all entries with their share of the time since Reset().
Definition dfttimings.cc:29
Scope Measure(const std::string &name)
Definition dfttimings.h:74
double Elapsed() const
Definition dfttimings.h:91
std::chrono::steady_clock Clock
Definition dfttimings.h:47
Clock::time_point start_
Definition dfttimings.h:122
static double ResidentMemoryGB(bool peak)
Definition dfttimings.cc:57
Logger is used for thread-safe output of messages.
Definition logger.h:164
STL namespace.
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Level
be loud and noisy
Definition globals.h:28