-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathsynchronization.jl
282 lines (232 loc) · 7.93 KB
/
synchronization.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# support for nonblocking synchronization
const use_nonblocking_synchronization =
Preferences.@load_preference("nonblocking_synchronization", true)
#
# bidirectional channel
#
# custom, unbuffered channel that supports returning a value to the sender
# without the need for a second channel
struct BidirectionalChannel{T} <: AbstractChannel{T}
cond_take::Threads.Condition # waiting for data to become available
cond_put::Threads.Condition # waiting for a writeable slot
cond_ret::Threads.Condition # waiting for a data to be returned
function BidirectionalChannel{T}() where T
lock = ReentrantLock()
cond_put = Threads.Condition(lock)
cond_take = Threads.Condition(lock)
cond_ret = Threads.Condition(lock)
return new(cond_take, cond_put, cond_ret)
end
end
Base.put!(c::BidirectionalChannel{T}, v) where T = put!(c, convert(T, v))
function Base.put!(c::BidirectionalChannel{T}, v::T) where T
lock(c)
try
# wait for a slot to be available
while isempty(c.cond_take)
Base.wait(c.cond_put)
end
# pass a value to the consumer
notify(c.cond_take, v, false, false)
# wait for a return value to be produced
Base.wait(c.cond_ret)
finally
unlock(c)
end
end
function Base.take!(f::Base.Callable, c::BidirectionalChannel{T}) where T
lock(c)
try
# notify the producer that we're ready to accept a value
notify(c.cond_put, nothing, false, false)
# receive a value from the producer
v = Base.wait(c.cond_take)::T
# return a value to the producer
ret = f(v)
notify(c.cond_ret, ret, false, false)
finally
unlock(c)
end
end
Base.lock(c::BidirectionalChannel) = lock(c.cond_take)
Base.unlock(c::BidirectionalChannel) = unlock(c.cond_take)
#
# fast-path synchronization
#
# before using a nonblocking mechanism, which has some overhead, use a busy-loop
# that queries the state of the object to synchronize. this reduces latency,
# especially for short operations. note that because it does not actually perform
# the synchronization, when it returns true (indicating that the object is synchronized)
# the actual synchronization API should be called again.
function spinning_synchronization(f, obj)
# fast path
f(obj) && return true
# minimize latency of short operations by busy-waiting,
# initially without even yielding to other tasks
spins = 0
while spins < 256
if spins < 32
ccall(:jl_cpu_pause, Cvoid, ())
# temporary solution before we have gc transition support in codegen.
ccall(:jl_gc_safepoint, Cvoid, ())
else
yield()
end
f(obj) && return true
spins += 1
end
return false
end
#
# nonblocking sync
#
@static if VERSION >= v"1.9.2"
# if we support foreign threads, perform the actual synchronization on a separate thread.
const MAX_SYNC_THREADS = 4
const sync_channels = Array{BidirectionalChannel{Any}}(undef, MAX_SYNC_THREADS)
const sync_channel_cursor = Threads.Atomic{UInt32}(1)
function synchronization_worker(data)
i = Int(data)
chan = sync_channels[i]
while true
# wait for work
take!(chan) do v
if v isa CuContext
context!(v)
unsafe_cuCtxSynchronize()
elseif v isa CuStream
context!(v.ctx)
unsafe_cuStreamSynchronize(v)
elseif v isa CuEvent
context!(v.ctx)
unsafe_cuEventSynchronize(v)
end
end
end
end
@noinline function create_synchronization_worker(i)
sync_channels[i] = BidirectionalChannel{Any}()
# should be safe to assign before threads are running;
# any user will just submit work that makes it block
# we don't know what the size of uv_thread_t is, so reserve enough space
tid = Ref{NTuple{32, UInt8}}(ntuple(i -> 0, 32))
cb = @cfunction(synchronization_worker, Cvoid, (Ptr{Cvoid},))
@ccall uv_thread_create(tid::Ptr{Cvoid}, cb::Ptr{Cvoid}, Ptr{Cvoid}(i)::Ptr{Cvoid})::Int32
return
end
function nonblocking_synchronize(val)
# get the channel of a synchronization worker
i = mod1(Threads.atomic_add!(sync_channel_cursor, UInt32(1)), MAX_SYNC_THREADS)
if !isassigned(sync_channels, i)
# TODO: write lock, double check, etc
create_synchronization_worker(i)
end
chan = @inbounds sync_channels[i]
# submit the object to synchronize
res = put!(chan, val)
# this `put!` blocks until the worker has finished processing and returned value
# (which is different from regular channels)
if res != SUCCESS
throw_api_error(res)
end
return
end
function device_synchronize(; blocking::Bool=false, spin::Bool=true)
if use_nonblocking_synchronization && !blocking
if spin && spinning_synchronization(isdone, legacy_stream())
cuCtxSynchronize()
else
nonblocking_synchronize(context())
end
else
cuCtxSynchronize()
end
check_exceptions()
end
function synchronize(stream::CuStream=stream(); blocking::Bool=false, spin::Bool=true)
if use_nonblocking_synchronization && !blocking
if spin && spinning_synchronization(isdone, stream)
cuStreamSynchronize(stream)
else
nonblocking_synchronize(stream)
end
else
cuStreamSynchronize(stream)
end
check_exceptions()
end
function synchronize(event::CuEvent; blocking::Bool=false, spin::Bool=true)
if use_nonblocking_synchronization && !blocking
if spin && spinning_synchronization(isdone, event)
cuEventSynchronize(event)
else
nonblocking_synchronize(event)
end
else
cuEventSynchronize(event)
end
end
else
# without thread adoption, have CUDA notify an async condition that wakes the libuv loop.
# this is not ideal: stream callbacks are deprecated, and do not fire in case of errors.
# furthermore, they do not trigger CUDA's synchronization hooks (see NVIDIA bug #3383169)
# requiring us to perform the actual API call again after nonblocking synchronization.
function nonblocking_synchronize(stream::CuStream)
# wait for an event signalled by CUDA
event = Base.Event()
launch(; stream) do
notify(event)
end
# if an error occurs, the callback may never fire, so use a timer to detect such cases
dev = device()
timer = Timer(0; interval=1)
Base.@sync begin
Threads.@spawn try
device!(dev)
while true
try
Base.wait(timer)
catch err
err isa EOFError && break
rethrow()
end
if unsafe_cuStreamQuery(stream) != ERROR_NOT_READY
break
end
end
finally
notify(event)
end
Threads.@spawn begin
Base.wait(event)
close(timer)
end
end
return
end
function device_synchronize(; blocking::Bool=false, spin::Bool=true)
if use_nonblocking_synchronization && !blocking
stream = legacy_stream()
if !spin || !spinning_synchronization(isdone, stream)
nonblocking_synchronize(stream)
end
end
cuCtxSynchronize()
check_exceptions()
end
function synchronize(stream::CuStream=stream(); blocking::Bool=false, spin::Bool=true)
if use_nonblocking_synchronization && !blocking
if !spin || !spinning_synchronization(isdone, stream)
nonblocking_synchronize(stream)
end
end
cuStreamSynchronize(stream)
check_exceptions()
end
function synchronize(event::CuEvent; blocking::Bool=false, spin::Bool=true)
if use_nonblocking_synchronization && !blocking
spin && spinning_synchronization(isdone, event)
end
cuEventSynchronize(event)
end
end