Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
LUPI_times.cc
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7# <<END-copyright>>
8*/
9
10#ifndef _WIN32
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <libgen.h>
15#include <iostream>
16
17#include <LUPI.hpp>
18
19namespace LUPI {
20
21#define bufferSize 1024
22
23/* *********************************************************************************************************//**
24 * Default constructor for DeltaTime which sets all values to 0.0.
25 ***********************************************************************************************************/
26
28 m_CPU_time( 0.0 ),
29 m_wallTime( 0.0 ),
30 m_CPU_timeIncremental( 0.0 ),
31 m_wallTimeIncremental( 0.0 ) {
32
33}
34
35/* *********************************************************************************************************//**
36 * Constructor for DeltaTime which contains the CPU time *a_CPU_time* and wall time *a_wallTime*.
37 ***********************************************************************************************************/
38
39DeltaTime::DeltaTime( double a_CPU_time, double a_wallTime, double a_CPU_timeIncremental, double a_wallTimeIncremental ) :
40 m_CPU_time( a_CPU_time ),
41 m_wallTime( a_wallTime ),
42 m_CPU_timeIncremental( a_CPU_timeIncremental ),
43 m_wallTimeIncremental( a_wallTimeIncremental ) {
44
45}
46
47/* *********************************************************************************************************//**
48 * Copy constructor for DeltaTime.
49 ***********************************************************************************************************/
50
51DeltaTime::DeltaTime( DeltaTime const &deltaTime ) :
52 m_CPU_time( deltaTime.CPU_time( ) ),
53 m_wallTime( deltaTime.wallTime( ) ),
54 m_CPU_timeIncremental( deltaTime.CPU_timeIncremental( ) ),
55 m_wallTimeIncremental( deltaTime.wallTimeIncremental( ) ) {
56
57}
58
59/* *********************************************************************************************************//**
60 * Returns a string representation of *this*. The arugments *a_formatIncremental* and *a_format* specify, in sprintf style, the
61 * formats for the incremental and total times. If an arugment is an empty string (e.g., "") then its time is not included
62 * in the return string. Each non-empty argument must contain two flags (e.g., "%.3f") for converting two doubles. If both formats
63 * are non-empty strings, then *a_sep* is inserted between them. An example of a format string is "total: CPU %8.3f, wall %8.3f".
64 *
65 * @param a_formatIncremental [in] Specifies the format in sprintf style for the incremental CPU and wall times.
66 * @param a_formatTotal [in] Specifies the format in sprintf style for the total CPU and wall times.
67 * @param a_sep [in] Specifies the string that separates the total and incremental time strings.
68 *
69 * @return A string representation of the delta times.
70 ***********************************************************************************************************/
71
72std::string DeltaTime::toString( std::string a_formatIncremental, std::string a_formatTotal, std::string a_sep ) {
73
74 std::string deltaTimeStr;
75 char buffer[bufferSize+1];
76
77 if( a_formatIncremental != "" ) {
78 snprintf( buffer, bufferSize, a_formatIncremental.c_str( ), m_CPU_timeIncremental, m_wallTimeIncremental );
79 deltaTimeStr += buffer;
80 }
81
82 if( a_formatTotal != "" ) {
83 if( deltaTimeStr != "" ) deltaTimeStr += a_sep;
84 snprintf( buffer, bufferSize, a_formatTotal.c_str( ), m_CPU_time, m_wallTime );
85 deltaTimeStr += buffer;
86 }
87
88 return( deltaTimeStr );
89}
90
91/* *********************************************************************************************************//**
92 * Constructor.
93 ***********************************************************************************************************/
94
96
97 reset( );
98}
99
100/* *********************************************************************************************************//**
101 * Returns a DeltaTime instance representing the time since *this* was created or *reset* was called.
102 *
103 * @return A DeltaTime representing the time since *this* was created or *reset* was called.
104 ***********************************************************************************************************/
105
107
108 struct timeval wallTime;
109 clock_t CPU_time = clock( );
110 gettimeofday( &wallTime, 0 );
111
112 double dWallTime = static_cast<double>( wallTime.tv_sec - m_wallTime.tv_sec )
113 + 1e-6 * static_cast<double>( wallTime.tv_usec - m_wallTime.tv_usec );
114 double dCPU_time = double( CPU_time - m_CPU_time ) / CLOCKS_PER_SEC;
115
116 double dWallTimeIncremental = static_cast<double>( wallTime.tv_sec - m_wallTimeIncremental.tv_sec )
117 + 1e-6 * static_cast<double>( wallTime.tv_usec - m_wallTimeIncremental.tv_usec );
118 double dCPU_timeIncremental = double( CPU_time - m_CPU_timeIncremental ) / CLOCKS_PER_SEC;
119
120 m_CPU_timeIncremental = CPU_time;
121 m_wallTimeIncremental = wallTime;
122
123 return( DeltaTime( dCPU_time, dWallTime, dCPU_timeIncremental, dWallTimeIncremental ) );
124}
125
126/* *********************************************************************************************************//**
127 * Calls deltaTime and then reset. Returns the results of the call to deltaTime.
128 *
129 * @return A DeltaTime representing the time since *this* was created or *reset* was called.
130 ***********************************************************************************************************/
131
133
134 DeltaTime deltaTime1 = deltaTime( );
135 reset( );
136
137 return( deltaTime1 );
138}
139
140/* *********************************************************************************************************//**
141 * Resets the internal times to the current time.
142 ***********************************************************************************************************/
143
145
146 m_CPU_time = clock( );
147 gettimeofday( &m_wallTime, 0 );
148 m_CPU_timeIncremental = m_CPU_time;
149 m_wallTimeIncremental = m_wallTime;
150}
151
152} // End of namespace LUPI.
153
154#endif // End of not _WIN32 defined.
#define bufferSize
Definition LUPI_times.cc:21
double CPU_timeIncremental() const
Definition LUPI.hpp:362
double CPU_time() const
Definition LUPI.hpp:360
std::string toString(std::string a_formatIncremental=LUPI_DeltaTime_toStringFormatIncremental, std::string a_format=LUPI_DeltaTime_toStringFormatTotal, std::string a_sep="; ")
Definition LUPI_times.cc:72
double wallTimeIncremental() const
Definition LUPI.hpp:363
double wallTime() const
Definition LUPI.hpp:361
DeltaTime deltaTime()
DeltaTime deltaTimeAndReset()
Definition LUPI.hpp:40