NimbleSM
NimbleSM is a solid mechanics simulation code for dynamic systems
Loading...
Searching...
No Matches
nimble.quanta.stopwatch.h
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// NimbleSM
6// Copyright 2018
7// National Technology & Engineering Solutions of Sandia, LLC (NTESS)
8//
9// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10// retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY EXPRESS OR IMPLIED
28// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
30// NO EVENT SHALL NTESS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact David Littlewood (djlittl@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef NIMBLE_LIGHTWEIGHT_STOPWATCH_H
45#define NIMBLE_LIGHTWEIGHT_STOPWATCH_H
46
47#include <chrono>
48
49namespace nimble {
50namespace quanta {
52{
53 typedef decltype(std::chrono::high_resolution_clock::now()) time;
54 typedef std::chrono::duration<double, std::ratio<1, 1>> seconds;
55 typedef std::chrono::duration<double, std::milli> milliseconds;
56 typedef std::chrono::duration<double, std::micro> microseconds;
57 typedef decltype((time{} - time{}).count()) nanoseconds;
58 typedef decltype((time{} - time{})) duration;
59 time _start = std::chrono::high_resolution_clock::now();
62 {
63 return (std::chrono::high_resolution_clock::now() - _start).count();
64 }
65 double
67 {
68 return microseconds(std::chrono::high_resolution_clock::now() - _start).count();
69 }
70 double
72 {
73 return milliseconds(std::chrono::high_resolution_clock::now() - _start).count();
74 }
75 double
77 {
78 return seconds(std::chrono::high_resolution_clock::now() - _start).count();
79 }
80 void
82 {
83 _start = std::chrono::high_resolution_clock::now();
84 }
85 template <class Function, class... Args>
86 static double
87 timefunction(Function&& f, Args&&... inputs)
88 {
89 stopwatch s;
90 s.reset();
91 f(std::forward<Args>(inputs)...);
92 return s.age();
93 }
94 static std::string
96 {
97 auto epoch_duration = std::chrono::high_resolution_clock::now().time_since_epoch();
98 auto duration_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch_duration);
99 auto nanoseconds =
100 (std::chrono::duration_cast<std::chrono::nanoseconds>(epoch_duration - duration_in_seconds).count());
101 std::time_t t = duration_in_seconds.count();
102 char buffer[100]{};
103 size_t end;
104 if ((end = std::strftime(buffer, 100, "%Y.%m.%e.%H.%M.%S.", std::localtime(&t)))) {
105 buffer[end] = '0' + (nanoseconds / 100000000 % 10);
106 buffer[end + 1] = '0' + (nanoseconds / 10000000 % 10);
107 buffer[end + 2] = '0' + (nanoseconds / 1000000 % 10);
108 buffer[end + 3] = '0' + (nanoseconds / 100000 % 10);
109 buffer[end + 4] = '0' + (nanoseconds / 10000 % 10);
110 buffer[end + 5] = '0' + (nanoseconds / 1000 % 10);
111 buffer[end + 6] = '0' + (nanoseconds / 100 % 10);
112 buffer[end + 7] = '0' + (nanoseconds / 10 % 10);
113 buffer[end + 8] = '0' + (nanoseconds % 10);
114 buffer[end + 9] = '\0';
115 }
116 return buffer;
117 }
118 static std::string
120 {
121 auto epoch_duration = std::chrono::high_resolution_clock::now().time_since_epoch();
122 auto duration_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch_duration);
123 auto microseconds =
124 (std::chrono::duration_cast<std::chrono::microseconds>(epoch_duration - duration_in_seconds).count());
125 std::time_t t = duration_in_seconds.count();
126 char buffer[100]{};
127 size_t end;
128 if ((end = std::strftime(buffer, 100, "%Y.%m.%d.%H.%M.%S.", std::localtime(&t)))) {
129 buffer[end] = '0' + (microseconds / 100000 % 10);
130 buffer[end + 1] = '0' + (microseconds / 10000 % 10);
131 buffer[end + 2] = '0' + (microseconds / 1000 % 10);
132 buffer[end + 3] = '0' + (microseconds / 100 % 10);
133 buffer[end + 4] = '0' + (microseconds / 10 % 10);
134 buffer[end + 5] = '0' + (microseconds % 10);
135 buffer[end + 6] = '\0';
136 }
137 return buffer;
138 }
139 static std::string
141 {
142 auto epoch_duration = std::chrono::high_resolution_clock::now().time_since_epoch();
143 auto duration_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch_duration);
144 auto milliseconds =
145 (std::chrono::duration_cast<std::chrono::milliseconds>(epoch_duration - duration_in_seconds).count());
146 std::time_t t = duration_in_seconds.count();
147 char buffer[100]{};
148 size_t end;
149 if ((end = std::strftime(buffer, 100, "%Y.%m.%e.%H.%M.%S.", std::localtime(&t)))) {
150 size_t milliseconds = (std::chrono::duration_cast<std::chrono::milliseconds>(
151 std::chrono::high_resolution_clock::now().time_since_epoch())
152 .count());
153 buffer[end] = '0' + (milliseconds / 100 % 10);
154 buffer[end + 1] = '0' + (milliseconds / 10 % 10);
155 buffer[end + 2] = '0' + (milliseconds % 10);
156 buffer[end + 3] = '\0';
157 }
158 return buffer;
159 }
160};
161} // namespace quanta
162} // namespace nimble
163
164#endif // NIMBLE_LIGHTWEIGHT_STOPWATCH_H
Definition nimble.quanta.arrayview.h:53
Definition kokkos_contact_manager.h:49
Definition nimble.quanta.stopwatch.h:52
static double timefunction(Function &&f, Args &&... inputs)
Definition nimble.quanta.stopwatch.h:87
decltype((time{} - time{}).count()) nanoseconds
Definition nimble.quanta.stopwatch.h:57
static std::string get_nanosecond_timestamp()
Definition nimble.quanta.stopwatch.h:95
static std::string get_microsecond_timestamp()
Definition nimble.quanta.stopwatch.h:119
std::chrono::duration< double, std::micro > microseconds
Definition nimble.quanta.stopwatch.h:56
std::chrono::duration< double, std::milli > milliseconds
Definition nimble.quanta.stopwatch.h:55
time _start
Definition nimble.quanta.stopwatch.h:59
nanoseconds age_nano()
Definition nimble.quanta.stopwatch.h:61
static std::string get_millisecond_timestamp()
Definition nimble.quanta.stopwatch.h:140
decltype((time{} - time{})) duration
Definition nimble.quanta.stopwatch.h:58
double age()
Definition nimble.quanta.stopwatch.h:76
void reset()
Definition nimble.quanta.stopwatch.h:81
double age_micro()
Definition nimble.quanta.stopwatch.h:66
double age_milli()
Definition nimble.quanta.stopwatch.h:71
std::chrono::duration< double, std::ratio< 1, 1 > > seconds
Definition nimble.quanta.stopwatch.h:54
decltype(std::chrono::high_resolution_clock::now()) time
Definition nimble.quanta.stopwatch.h:53