Fermat
timer.h
1 /*
2  * CUGAR : Cuda Graphics Accelerator
3  *
4  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <cugar/basic/types.h>
32 #include <cuda_runtime.h>
33 
34 namespace cugar {
35 namespace cuda {
36 
39 
42 
46 struct Timer
47 {
50  inline Timer();
51 
54  inline ~Timer();
55 
58  inline void start();
59 
62  inline void stop();
63 
66  inline float seconds() const;
67 
68  cudaEvent_t m_start, m_stop;
69 };
70 
75 template <typename T>
77 {
78  ScopedTimer(T* time) : m_time( time ), m_timer() { m_timer.start(); }
79  ~ScopedTimer() { m_timer.stop(); *m_time += m_timer.seconds(); }
80 
81  T* m_time;
82  Timer m_timer;
83 };
84 
85 // constructor
86 //
88 {
89  cudaEventCreate(&m_start);
90  cudaEventCreate(&m_stop);
91 }
92 
93 // destructor
94 //
96 {
97  cudaEventDestroy(m_start);
98  cudaEventDestroy(m_stop);
99 }
100 
101 // start timing
102 //
104 {
105  cudaEventRecord(m_start, 0);
106 }
107 
108 // stop timing
109 //
111 {
112  cudaEventRecord(m_stop, 0);
113  //cudaEventSynchronize(m_stop);
114 }
115 
116 // elapsed seconds
117 //
118 float Timer::seconds() const
119 {
120  float elapsedTime;
121  cudaEventSynchronize(m_stop);
122  cudaEventElapsedTime(&elapsedTime, m_start, m_stop);
123  return elapsedTime * 1.0e-3f;
124 }
125 
128 
129 } // namespace cuda
130 } // namespace cugar
Definition: timer.h:46
~Timer()
Definition: timer.h:95
Definition: timer.h:76
void start()
Definition: timer.h:103
Timer()
Definition: timer.h:87
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
float seconds() const
Definition: timer.h:118
void stop()
Definition: timer.h:110