Discontinuous Galerkin Library
#include "dg/algorithm.h"
timer.h
Go to the documentation of this file.
1#ifndef _DG_TIMER_
2#define _DG_TIMER_
3
5#include "thrust/device_vector.h"
6//the <thrust/device_vector.h> header must be included for the THRUST_DEVICE_SYSTEM macros to work
7#if (THRUST_DEVICE_SYSTEM!=THRUST_DEVICE_SYSTEM_CUDA) //if we don't use a GPU
8#ifdef MPI_VERSION //(mpi.h is included)
9namespace dg
10{
11class Timer //CPU/ OMP + MPI
12{
13 public:
14 void tic( MPI_Comm comm = MPI_COMM_WORLD ){ MPI_Barrier(comm); start = MPI_Wtime();}
15 void toc( MPI_Comm comm = MPI_COMM_WORLD ){ MPI_Barrier(comm); stop = MPI_Wtime(); }
16 double diff()const{ return stop - start; }
17 private:
18 double start = 0., stop = 0.;
19};
20}//namespace dg
21#elif THRUST_DEVICE_SYSTEM==THRUST_DEVICE_SYSTEM_OMP //MPI_VERSION not defined and THRUST == OMP
22#include "omp.h"
23namespace dg
24{
25class Timer //OMP non-MPI
26{
27 public:
28 void tic( ){ start = omp_get_wtime();}
29 void toc( ){ stop = omp_get_wtime(); }
30 double diff()const{ return stop - start; }
31 private:
32 double start = 0., stop = 0.;
33};
34}//namespace dg
35#else // MPI_VERSION not defined and THRUST == CPU
36
38#include <chrono>
39namespace dg{
40class Timer
41{
42public:
43 inline void tic(void) { m_start = std::chrono::system_clock::now(); }
44 inline void toc(void) { m_stop = std::chrono::system_clock::now();}
45 inline double diff(void) {
46 double difference = std::chrono::duration_cast<std::chrono::nanoseconds>(
47 m_stop - m_start).count();
48 return difference*1e-9;
49 }
50private:
51 typename std::chrono::system_clock::time_point m_start, m_stop;
52};
53}//namespace dg
54#endif//MPI_VERSION
55
56#else //THRUST_DEVICE_SYSTEM==THRUST_DEVICE_SYSTEM_CUDA
57#ifdef MPI_VERSION
58namespace dg{
59class Timer //GPU MPI
60{
61 public:
62 Timer(){ }
63 void tic( MPI_Comm comm = MPI_COMM_WORLD ){
64 cudaDeviceSynchronize();
65 MPI_Barrier(comm);
66 start = MPI_Wtime();
67 }
68 void toc( MPI_Comm comm = MPI_COMM_WORLD ){
69 cudaDeviceSynchronize();
70 MPI_Barrier(comm); //sync other cpus
71 stop = MPI_Wtime();
72 }
73 double diff()const{ return stop - start; }
74 private:
75 double start = 0., stop = 0.;
76};
77}//namespace dg
78
79#else //MPI_VERSION
80
81namespace dg{
82class Timer// GPU non-MPI
83{
84 public:
85 Timer(){
86 cudaEventCreate( &start);
87 cudaEventCreate( &stop);
88 }
89 void tic( cudaStream_t stream = 0){ cudaEventRecord( start, stream);}
90 void toc( cudaStream_t stream = 0){
91 cudaEventRecord( stop, stream);
92 cudaEventSynchronize( stop);
93 }
94 float diff()const{
95 float time;
96 cudaEventElapsedTime( &time, start, stop);
97 return time/1000.;
98 }
99 private:
100 cudaEvent_t start, stop;
101};
102} //namespace dg
103#endif //MPI_VERSION
104#endif //THRUST
105
107
108#endif //_DG_TIMER_
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
double diff() const
Return time in seconds elapsed between tic and toc.
void toc()
Stop timer.
void tic()
Start timer.