Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches
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
37#include <chrono>
38namespace dg{
39class Timer
40{
41public:
42 inline void tic(void) { m_start = std::chrono::system_clock::now(); }
43 inline void toc(void) { m_stop = std::chrono::system_clock::now();}
44 inline double diff(void) {
45 double difference = std::chrono::duration_cast<std::chrono::nanoseconds>(
46 m_stop - m_start).count();
47 return difference*1e-9;
48 }
49private:
50 typename std::chrono::system_clock::time_point m_start, m_stop;
51};
52}//namespace dg
53#endif//MPI_VERSION
54
55#else //THRUST_DEVICE_SYSTEM==THRUST_DEVICE_SYSTEM_CUDA
56#ifdef MPI_VERSION
57namespace dg{
58class Timer //GPU MPI
59{
60 public:
61 Timer(){ }
62 void tic( MPI_Comm comm = MPI_COMM_WORLD ){
63 cudaDeviceSynchronize();
64 MPI_Barrier(comm);
65 start = MPI_Wtime();
66 }
67 void toc( MPI_Comm comm = MPI_COMM_WORLD ){
68 cudaDeviceSynchronize();
69 MPI_Barrier(comm); //sync other cpus
70 stop = MPI_Wtime();
71 }
72 double diff()const{ return stop - start; }
73 private:
74 double start = 0., stop = 0.;
75};
76}//namespace dg
77
78#else //MPI_VERSION
79
80namespace dg{
81class Timer// GPU non-MPI
82{
83 public:
84 Timer(){
85 cudaEventCreate( &start);
86 cudaEventCreate( &stop);
87 }
88 void tic( cudaStream_t stream = 0){ cudaEventRecord( start, stream);}
89 void toc( cudaStream_t stream = 0){
90 cudaEventRecord( stop, stream);
91 cudaEventSynchronize( stop);
92 }
93 float diff()const{
94 float time;
95 cudaEventElapsedTime( &time, start, stop);
96 return time/1000.;
97 }
98 private:
99 cudaEvent_t start, stop;
100};
101} //namespace dg
102#endif //MPI_VERSION
103#endif //THRUST
104
106
107#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.