n2p2 - A neural network potential package
Stopwatch.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include <cstdio>
18#include "Stopwatch.h"
19
20using namespace nnp;
21
22const double Stopwatch::NSEC = 1E-9;
23
24Stopwatch::Stopwatch()
25{
26 state = STOPPED;
27 timeTotal = 0.0;
28 timeLoop = 0.0;
29#ifdef N2P2_NO_TIME
30 //fprintf(stderr, "WARNING: Stopwatch is using dummy implementation.\n");
31#elif __linux__
32 time.tv_sec = 0;
33 time.tv_nsec = 0;
34#elif __MACH__
35 time = 0;
36#endif
37
38}
39
40void Stopwatch::start(bool newLoop)
41{
42 if (state == STOPPED)
43 {
44#ifdef N2P2_NO_TIME
45#elif __linux__
46 clock_gettime(CLOCK_MONOTONIC, &time);
47#elif __MACH__
48 time = mach_absolute_time();
49#endif
50 if (newLoop) timeLoop = 0.0;
51 state = RUNNING;
52 }
53 else
54 {
55 fprintf(stderr,
56 "WARNING: Unable to start clock, clock already running.\n");
57 }
58
59 return;
60}
61
63{
64 stopTime();
65
66 return getTotal();
67}
68
70{
71 stopTime();
72
73 return getLoop();
74}
75
77{
78 state = STOPPED;
79 timeTotal = 0.0;
80 timeLoop = 0.0;
81#ifdef N2P2_NO_TIME
82#elif __linux__
83 time.tv_sec = 0;
84 time.tv_nsec = 0;
85#elif __MACH__
86 time = 0;
87#endif
88
89 return;
90}
91
93{
94 double timeInterval = updateTime();
95 timeLoop += timeInterval;
96 timeTotal += timeInterval;
97 state = STOPPED;
98
99 return;
100}
101
103{
104 if (state == RUNNING)
105 {
106#ifdef N2P2_NO_TIME
107 return 1.0;
108#elif __linux__
109 time_t secLast = time.tv_sec;
110 long nsecLast = time.tv_nsec;
111
112 clock_gettime(CLOCK_MONOTONIC, &time);
113 return (double)(time.tv_sec - secLast)
114 + (double)(time.tv_nsec - nsecLast) * NSEC;
115#elif __MACH__
116 uint64_t lastTime = time;
117 uint64_t time = mach_absolute_time();
118 static mach_timebase_info_data_t info = {0, 0};
119
120 if (info.denom == 0) mach_timebase_info(&info);
121 return ((time - lastTime) * (info.numer / info.denom)) * NSEC;
122#endif
123 }
124 else
125 {
126 fprintf(stderr,
127 "WARNING: Unable to update time, clock not running.\n");
128 return 0.0;
129 }
130}
double getTotal() const
Return total time elapsed (of a stopped watch).
Definition: Stopwatch.h:56
double updateTime()
Definition: Stopwatch.cpp:102
double timeLoop
Definition: Stopwatch.h:73
void reset()
Reset stopwatch (total and loop time zero, clock not running).
Definition: Stopwatch.cpp:76
void stopTime()
Definition: Stopwatch.cpp:92
double stop()
Stop and return total time.
Definition: Stopwatch.cpp:62
void start(bool newLoop=true)
Start the stopwatch.
Definition: Stopwatch.cpp:40
double getLoop() const
Return time elapsed in last loop interval (of a stopped watch).
Definition: Stopwatch.h:58
double timeTotal
Definition: Stopwatch.h:72
double loop()
Stop and return loop time.
Definition: Stopwatch.cpp:69
static const double NSEC
Definition: Stopwatch.h:71
Definition: Atom.h:29