Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
event.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014-2016, Scott Zuyderduyn
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // event.hpp
32 //
33 // Encapsulates a CUDA API event.
34 //
35 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
36 //----------------------------------------------------------------------------
37 
38 #pragma once
39 #ifndef ECUDA_EVENT_HPP
40 #define ECUDA_EVENT_HPP
41 
42 #include "global.hpp"
43 #include "impl/host_emulation.hpp" // gets data structure definitions when compiling host-only without nvcc
44 
45 namespace ecuda {
46 
74 class event {
75 
76 private:
77  cudaEvent_t _event;
78 
79 public:
85  event() { cudaEventCreate(&_event); }
86 
100  event( unsigned flags ) { cudaEventCreateWithFlags(&_event,flags); }
101 
107  ~event() { cudaEventDestroy(_event); }
108 
123  inline void record( cudaStream_t stream = 0 ) { cudaEventRecord( _event, stream ); }
124 
139  inline cudaError_t synchronize() { return cudaEventSynchronize( _event ); }
140 
153  inline cudaError_t query() { return cudaEventQuery( _event ); }
154 
173  static float elapsed_time( event& start, event& end ) {
174  float t;
175  cudaEventElapsedTime( &t, start._event, end._event );
176  return t;
177  }
178 
187  inline float operator-( event& other ) { return event::elapsed_time( other, *this ); }
188 
189 };
190 
191 
192 } // namespace ecuda
193 
194 #endif
~event()
Destructor.
Definition: event.hpp:107
Encapsulates CUDA API event objects and functions.
Definition: event.hpp:74
event()
Default constructor.
Definition: event.hpp:85
static float elapsed_time(event &start, event &end)
Computes the elapsed time between two events (in milliseconds with a resolution of around 0...
Definition: event.hpp:173
float operator-(event &other)
Computes the elapsed time between another event and this event.
Definition: event.hpp:187
void record(cudaStream_t stream=0)
Records an event.
Definition: event.hpp:123
cudaError_t synchronize()
Wait until the completion of all device work preceding the most recent call to record().
Definition: event.hpp:139
event(unsigned flags)
Constructs an event with the given flags.
Definition: event.hpp:100
cudaError_t query()
Query the status of all device work preceding the most recent call to record().
Definition: event.hpp:153