forked from JuliaGPU/CUDA.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.jl
93 lines (74 loc) · 2.03 KB
/
events.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Events for timing
export CuEvent, record, synchronize, elapsed, @elapsed
"""
CuEvent()
Create a new CUDA event.
"""
mutable struct CuEvent
handle::CUevent
ctx::CuContext
function CuEvent(flags=EVENT_DEFAULT)
handle_ref = Ref{CUevent}()
cuEventCreate(handle_ref, flags)
ctx = CuCurrentContext()
obj = new(handle_ref[], ctx)
finalizer(unsafe_destroy!, obj)
return obj
end
end
function unsafe_destroy!(e::CuEvent)
if isvalid(e.ctx)
cuEventDestroy(e)
end
end
Base.unsafe_convert(::Type{CUevent}, e::CuEvent) = e.handle
Base.:(==)(a::CuEvent, b::CuEvent) = a.handle == b.handle
Base.hash(e::CuEvent, h::UInt) = hash(e.handle, h)
"""
record(e::CuEvent, stream=CuDefaultStream())
Record an event on a stream.
"""
record(e::CuEvent, stream::CuStream=CuDefaultStream()) =
cuEventRecord(e, stream)
"""
synchronize(e::CuEvent)
Waits for an event to complete.
"""
synchronize(e::CuEvent) = cuEventSynchronize(e)
"""
wait(e::CuEvent, stream=CuDefaultStream())
Make a stream wait on a event. This only makes the stream wait, and not the host; use
[`synchronize(::CuEvent)`](@ref) for that.
"""
wait(e::CuEvent, stream::CuStream=CuDefaultStream()) =
cuStreamWaitEvent(stream, e, 0)
"""
elapsed(start::CuEvent, stop::CuEvent)
Computes the elapsed time between two events (in seconds).
"""
function elapsed(start::CuEvent, stop::CuEvent)
time_ref = Ref{Cfloat}()
cuEventElapsedTime(time_ref, start, stop)
return time_ref[]/1000
end
"""
@elapsed stream ex
@elapsed ex
A macro to evaluate an expression, discarding the resulting value, instead returning the
number of seconds it took to execute on the GPU, as a floating-point number.
"""
macro elapsed(stream, ex)
quote
t0, t1 = CuEvent(), CuEvent()
record(t0, $(esc(stream)))
$(esc(ex))
record(t1, $(esc(stream)))
synchronize(t1)
elapsed(t0, t1)
end
end
macro elapsed(ex)
quote
@elapsed(CuDefaultStream(), $(esc(ex)))
end
end