Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Commit 616fb49

Browse files
authored
Merge pull request #433 from JuliaGPU/tb/time_sync
Synchronize the GPU while doing time measurements.
2 parents b3242da + 51c06f1 commit 616fb49

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/memory.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,30 @@ macro allocated(ex)
202202
end
203203
end
204204

205+
206+
"""
207+
@time ex
208+
209+
Run expression `ex` and report on execution time and GPU/CPU memory behavior. The GPU is
210+
synchronized right before and after executing `ex` to exclude any external effects.
211+
212+
"""
205213
macro time(ex)
206214
quote
215+
# @time might surround an application, so be sure to initialize CUDA before that
216+
# FIXME: this should be done in CUDAdrv (`synchronize(ctx=CuCurrentOrNewContext()`)
217+
# but the CUDA initialization mechanics are part of CUDAnative.jl
218+
CUDAnative.maybe_initialize("@time")
219+
220+
# coarse synchronization to exclude effects from previously-executed code
221+
CUDAdrv.synchronize()
222+
207223
local gpu_mem_stats0 = copy(alloc_stats)
208224
local cpu_mem_stats0 = Base.gc_num()
209225
local cpu_time0 = time_ns()
210226

211-
local val = $(esc(ex))
227+
# fine-grained synchronization of the code under analysis
228+
local val = @sync $(esc(ex))
212229

213230
local cpu_time1 = time_ns()
214231
local cpu_mem_stats1 = Base.gc_num()

0 commit comments

Comments
 (0)