Skip to content

Commit 463d1da

Browse files
committed
Add locking to stream GC
1 parent 17469f2 commit 463d1da

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/backends/cuda.jl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,28 @@ end
2020
# Possible improvements
2121
# - Add a background task that occasionally scans all streams
2222
# - Add a hysterisis by checking a "since last scanned" timestamp
23-
# - Add locking
23+
const STREAM_GC_LOCK = Threads.ReentrantLock()
2424
function next_stream()
25-
if !isempty(FREE_STREAMS)
26-
return pop!(FREE_STREAMS)
27-
end
25+
lock(STREAM_GC_LOCK) do
26+
if !isempty(FREE_STREAMS)
27+
return pop!(FREE_STREAMS)
28+
end
2829

29-
if length(STREAMS) > STREAM_GC_THRESHOLD[]
30-
for stream in STREAMS
31-
if CUDA.query(stream)
32-
push!(FREE_STREAMS, stream)
30+
if length(STREAMS) > STREAM_GC_THRESHOLD[]
31+
for stream in STREAMS
32+
if CUDA.query(stream)
33+
push!(FREE_STREAMS, stream)
34+
end
3335
end
3436
end
35-
end
3637

37-
if !isempty(FREE_STREAMS)
38-
return pop!(FREE_STREAMS)
38+
if !isempty(FREE_STREAMS)
39+
return pop!(FREE_STREAMS)
40+
end
41+
stream = CUDA.CuStream(flags = CUDA.STREAM_NON_BLOCKING)
42+
push!(STREAMS, stream)
43+
return stream
3944
end
40-
stream = CUDA.CuStream(flags = CUDA.STREAM_NON_BLOCKING)
41-
push!(STREAMS, stream)
42-
return stream
4345
end
4446

4547
struct CudaEvent <: Event
@@ -321,4 +323,4 @@ end
321323
# GPU implementation of const memory
322324
###
323325

324-
Adapt.adapt_storage(to::ConstAdaptor, a::CUDA.CuDeviceArray) = Base.Experimental.Const(a)
326+
Adapt.adapt_storage(to::ConstAdaptor, a::CUDA.CuDeviceArray) = Base.Experimental.Const(a)

0 commit comments

Comments
 (0)