Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
stopwatch.h
1/******************************************************************************
2 * Copyright (C) 2006-2024 by the GIMLi development team *
3 * Carsten Rücker carsten@resistivity.net *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************/
18
19#ifndef _GIMLI_STOPWATCH__H
20#define _GIMLI_STOPWATCH__H
21
22#include "gimli.h"
23
24#include <sys/timeb.h>
25#include <chrono>
26
27#if defined(__i386__)
28static __inline__ size_t rdtsc__(void){
29 size_t x;
30 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
31 return x;
32}
33#elif defined(__x86_64__)
34static __inline__ size_t rdtsc__(void){
35 unsigned hi, lo;
36 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
37 return ((size_t)lo)|(((size_t)hi)<<32);
38}
39#else
40static inline size_t rdtsc__(void){
41 return 0;
42}
43#endif
44
45namespace GIMLI{
46
47class DLLEXPORT CycleCounter{
48public:
49 CycleCounter() : var_(0) {}
50
51 ~CycleCounter(){}
52
53 inline void tic(){ var_ = rdtsc__(); }
54
55 inline size_t toc() const { return (rdtsc__() - var_); }
56
57protected:
58
59 size_t var_;
60};
61
62class DLLEXPORT Stopwatch {
63public:
64 Stopwatch(bool start=true);
65
66 ~Stopwatch();
67
68 void start();
69
70 void stop(bool verbose=false);
71
73 void restart();
74
76 void reset();
77
79 double duration(bool restart=false);
80
82 size_t cycles(bool restart=false);
83
84 const CycleCounter & cycleCounter() const { return _cCounter; }
85
87 void store(bool restart=false);
88
90 const RVector & stored() const { return *this->_store;}
91
92
93protected:
94 enum watchstate {undefined,halted,running} _state;
95
96 std::chrono::time_point<std::chrono::high_resolution_clock> _start, _stop;
97 RVector *_store;
98
99 CycleCounter _cCounter;
100};
101
102#define TIC__ std::cout.precision(12); GIMLI::Stopwatch __swatch__(true);
103#define TOC__ std::cout << __swatch__.duration(true) << std::endl;
104#define toc__ __swatch__.duration()
105
106} // namespace GIMLI
107
108#endif // _GIMLI_STOPWATCH__H
Definition stopwatch.h:47
void store(bool restart=false)
Definition stopwatch.cpp:83
void restart()
Definition stopwatch.cpp:50
size_t cycles(bool restart=false)
Definition stopwatch.cpp:87
void reset()
Definition stopwatch.cpp:55
double duration(bool restart=false)
Definition stopwatch.cpp:60
const RVector & stored() const
Definition stopwatch.h:90
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
RVector x(const R3Vector &rv)
Definition pos.cpp:107