openPMD-api
Timer.hpp
1 /* Copyright 2020 Junmin Gu
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 
31 
32 namespace openPMD
33 {
34 namespace benchmark
35 {
41  class Timer
42  {
43  public:
44  using Clock = std::chrono::system_clock;
45  using TimePoint = std::chrono::time_point< Clock >;
46 
53  Timer( const std::string& tag, int rank, TimePoint progStart )
54  : m_ProgStart( progStart ),
55  m_Start( std::chrono::system_clock::now() ),
56  m_Tag( tag ),
57  m_Rank( rank )
58  {
59  MemoryProfiler( rank, tag );
60  }
61 
62  ~Timer() {
63  std::string tt = "~" + m_Tag;
64  MemoryProfiler (m_Rank, tt.c_str());
65  m_End = Clock::now();
66 
67  double millis = std::chrono::duration_cast< std::chrono::milliseconds >( m_End - m_Start ).count();
68  double secs = millis/1000.0;
69  if( m_Rank > 0 )
70  return;
71 
72  std::cout << " [" << m_Tag << "] took:" << secs << " seconds\n";
73  std::cout<<" " << m_Tag <<" From ProgStart in seconds "<<
74  std::chrono::duration_cast<std::chrono::milliseconds>(m_End - m_ProgStart).count()/1000.0<<std::endl;
75 
76  std::cout<<std::endl;
77  }
78  private:
79  TimePoint m_ProgStart;
80  TimePoint m_Start;
81  TimePoint m_End;
82 
83  std::string m_Tag;
84  int m_Rank = 0;
85  };
86 }
87 }
The Timer class for profiling purpose.
Definition: Timer.hpp:41
STL namespace.
Timer(const std::string &tag, int rank, TimePoint progStart)
Simple Timer.
Definition: Timer.hpp:53
Public definitions of openPMD-api.
Definition: Date.cpp:29
The Memory profiler class for profiling purpose.
Definition: MemoryProfiler.hpp:37