Skip to content

Commit fd33140

Browse files
authored
Merge pull request #436 from llaniewski/feature/chrono
Switching to `steady_clock` for walltime
2 parents 916dbc8 + aa903e3 commit fd33140

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

src/Global.cpp.Rt

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@
2525

2626
MPMDHelper MPMD;
2727

28-
std::clock_t global_start;
28+
time_point_t global_start;
29+
30+
void start_walltime() {
31+
#ifdef USE_STEADY_CLOCK
32+
global_start = std::chrono::steady_clock::now();
33+
output("Using std::chrono::steady_clock::now() for walltime\n");
34+
#else
35+
global_start = std::clock();
36+
output("Using std::clock() for walltime\n");
37+
#endif
38+
}
39+
40+
2941
int D_MPI_RANK;
3042
int D_TERMINAL;
3143
/*

src/Global.h.Rt

+19-3
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,29 @@ for (m in Margin) { ?>
8585
#endif
8686
*/
8787
#ifndef DEBUG_H
88-
#include <ctime>
89-
extern std::clock_t global_start;
88+
#ifdef USE_STEADY_CLOCK
89+
#include <chrono>
90+
typedef std::chrono::time_point<std::chrono::steady_clock> time_point_t;
91+
#else
92+
#include <ctime>
93+
typedef std::clock_t time_point_t;
94+
#endif
95+
96+
extern time_point_t global_start;
9097

9198
inline double get_walltime() {
92-
return (std::clock() - global_start) / (double)CLOCKS_PER_SEC;
99+
#ifdef USE_STEADY_CLOCK
100+
time_point_t now = std::chrono::steady_clock::now();
101+
std::chrono::duration<double, std::milli> ms = now - global_start;
102+
return ms.count() / 1000.0;
103+
#else
104+
time_point_t now = std::clock();
105+
return (now - global_start) / (double)CLOCKS_PER_SEC;
106+
#endif
93107
}
94108

109+
void start_walltime();
110+
95111
extern int D_MPI_RANK;
96112
extern int D_TERMINAL;
97113

src/config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@
6868
/* Build-in python support */
6969
#undef EMBEDED_PYTHON
7070

71+
/* If to use the C++11 chrono */
72+
#undef USE_STEADY_CLOCK
73+
7174
#endif

src/configure.ac

+3-1
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,14 @@ then
777777
fi
778778

779779

780-
if test "x${enable_cpp11}" == "xyes"
780+
if test "x${enable_cpp11}" != "xno"
781781
then
782782
CPPFLAGS="${CPPFLAGS} -std=c++11"
783783
NVFLAGS="${NVFLAGS} -std=c++11"
784784
fi
785785

786+
AC_CHECK_HEADERS([chrono],[AC_DEFINE([USE_STEADY_CLOCK], [1], [Using chrono's steady_clock])],[])
787+
786788

787789
if test "x${CONF_CPPFLAGS}" == "x"
788790
then

src/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ int main ( int argc, char * argv[] )
191191
InitPrint(DEBUG_LEVEL, 6, 8);
192192
MPI_Barrier(MPMD.local);
193193

194-
global_start = std::clock();
194+
start_walltime();
195195
if (solver->mpi_rank == 0) {
196196
NOTICE("-------------------------------------------------------------------------\n");
197197
NOTICE("- CLB version: %25s -\n",VERSION);
@@ -419,7 +419,7 @@ int main ( int argc, char * argv[] )
419419
CudaEventDestroy( stop );
420420

421421
if (solver->mpi_rank == 0) {
422-
double duration = (std::clock() - global_start) / (double)CLOCKS_PER_SEC;
422+
double duration = get_walltime();
423423
output("Total duration: %lf s = %lf min = %lf h\n", duration, duration / 60, duration /60/60);
424424
}
425425
delete solver;

0 commit comments

Comments
 (0)