openPMD-api
 
Loading...
Searching...
No Matches
Timer.hpp
1/* Copyright 2020-2025 Junmin Gu, Axel Huebl
2 *
3 * This file is part of openPMD-api.
4 *
5 * openPMD-api is free software: you can redistribute it and/or modify
6 * it under the terms of of either the GNU General Public License or
7 * the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * openPMD-api is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License and the GNU Lesser General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * and the GNU Lesser General Public License along with openPMD-api.
19 * If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "MemoryProfiler.hpp"
25
26#include <chrono>
27#include <fstream>
28#include <iostream>
29#include <string>
30
31namespace openPMD
32{
33namespace benchmark
34{
40 class Timer
41 {
42 public:
43 using Clock = std::chrono::system_clock;
44 using TimePoint = std::chrono::time_point<Clock>;
45
52 Timer(const std::string &tag, int rank, TimePoint progStart)
53 : m_ProgStart(progStart)
54 , m_Start(std::chrono::system_clock::now())
55 , m_Tag(tag)
56 , m_Rank(rank)
57 {
58 MemoryProfiler(rank, tag);
59 }
60
61 ~Timer()
62 {
63 std::string tt = "~" + m_Tag;
64 MemoryProfiler(m_Rank, tt.c_str());
65 m_End = Clock::now();
66
67 double millis =
68 std::chrono::duration_cast<std::chrono::milliseconds>(
69 m_End - m_Start)
70 .count();
71 double secs = millis / 1000.0;
72 if (m_Rank > 0)
73 return;
74
75 std::cout << " [" << m_Tag << "] took:" << secs << " seconds\n";
76 std::cout << " " << m_Tag << " From ProgStart in seconds "
77 << std::chrono::duration_cast<std::chrono::milliseconds>(
78 m_End - m_ProgStart)
79 .count() /
80 1000.0
81 << std::endl;
82
83 std::cout << std::endl;
84 }
85
86 private:
87 TimePoint m_ProgStart;
88 TimePoint m_Start;
89 TimePoint m_End;
90
91 std::string m_Tag;
92 int m_Rank = 0;
93 };
94} // namespace benchmark
95} // namespace openPMD
The Memory profiler class for profiling purpose.
Definition MemoryProfiler.hpp:37
The Timer class for profiling purpose.
Definition Timer.hpp:41
Timer(const std::string &tag, int rank, TimePoint progStart)
Simple Timer.
Definition Timer.hpp:52
Public definitions of openPMD-api.
Definition Date.cpp:29
STL namespace.