Skip to content

Commit fc1d509

Browse files
vchuravymaleadt
andauthored
Mark all ccalls as GC safe (#2262)
This allows the GC to run while potentially blocking in a CUDA library. To make this safe, callbacks into Julia should again transition to GC-unsafe mode. It should be reimplemented when JuliaLang/julia#49933 lands. Co-authored-by: Tim Besard <tim.besard@gmail.com>
1 parent 297c628 commit fc1d509

28 files changed

+16107
-14142
lines changed

Diff for: .buildkite/pipeline.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ steps:
132132
println("--- :julia: Instantiating project")
133133
withenv("JULIA_PKG_PRECOMPILE_AUTO" => 0) do
134134
Pkg.activate(joinpath(pwd(), "lib", lowercase("{{matrix.package}}")))
135-
Pkg.instantiate()
135+
try
136+
Pkg.instantiate()
137+
catch
138+
# if we fail to instantiate, assume that we need a newer CUDA.jl
139+
Pkg.develop(path=".")
140+
end
136141
137142
Pkg.add("CUDA_Runtime_jll")
138143
write("LocalPreferences.toml", "[CUDA_Runtime_jll]\nversion = \"{{matrix.cuda}}\"")

Diff for: Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "CUDA"
22
uuid = "052768ef-5323-5732-b1bb-66c8b64840ba"
3-
version = "5.2.0"
3+
version = "5.3.0"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

Diff for: lib/cublas/CUBLAS.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function log_message(ptr)
210210
return
211211
end
212212

213-
function _log_message(blob)
213+
@gcunsafe_callback function _log_message(blob)
214214
# the message format isn't documented, but it looks like a message starts with a capital
215215
# and the severity (e.g. `I!`), and subsequent lines start with a lowercase mark (`!i`)
216216
#

Diff for: lib/cublas/libcublas.jl

+3,214-2,702
Large diffs are not rendered by default.

Diff for: lib/cudadrv/libcuda.jl

+1,016-923
Large diffs are not rendered by default.

Diff for: lib/cudadrv/occupancy.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ end
3636
# HACK: callback function for `launch_configuration` on platforms without support for
3737
# trampolines as used by `@cfunction` (JuliaLang/julia#27174, JuliaLang/julia#32154)
3838
_shmem_cb = nothing
39-
_shmem_cint_cb(x::Cint) = Cint(something(_shmem_cb)(x))
39+
@gcunsafe_callback function _shmem_cint_cb(x::Cint)
40+
Cint(something(_shmem_cb)(x))
41+
end
4042
_shmem_cb_lock = Threads.ReentrantLock()
4143

4244
"""
@@ -58,7 +60,9 @@ function launch_configuration(fun::CuFunction; shmem::Union{Integer,Base.Callabl
5860
if isa(shmem, Integer)
5961
cuOccupancyMaxPotentialBlockSize(blocks_ref, threads_ref, fun, C_NULL, shmem, max_threads)
6062
elseif Sys.ARCH == :x86 || Sys.ARCH == :x86_64
61-
shmem_cint = threads -> Cint(shmem(threads))
63+
@gcunsafe_callback function shmem_cint(threads)
64+
Cint(shmem(threads))
65+
end
6266
cb = @cfunction($shmem_cint, Cint, (Cint,))
6367
cuOccupancyMaxPotentialBlockSize(blocks_ref, threads_ref, fun, cb, 0, max_threads)
6468
else

Diff for: lib/cudnn/Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "cuDNN"
22
uuid = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
33
authors = ["Tim Besard <tim.besard@gmail.com>"]
4-
version = "1.3.0"
4+
version = "1.3.1"
55

66
[deps]
77
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
@@ -11,7 +11,7 @@ CUDNN_jll = "62b44479-cb7b-5706-934f-f13b2eb2e645"
1111

1212
[compat]
1313
CEnum = "0.2, 0.3, 0.4, 0.5"
14-
CUDA = "~5.1, ~5.2"
14+
CUDA = "~5.3"
1515
CUDA_Runtime_Discovery = "0.2"
1616
CUDNN_jll = "~8.9"
1717
julia = "1.8"

Diff for: lib/cudnn/src/cuDNN.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function log_message(sev, udata, dbg_ptr, ptr)
137137
return
138138
end
139139

140-
function _log_message(sev, dbg, str)
140+
@gcunsafe_callback function _log_message(sev, dbg, str)
141141
lines = split(str, '\0')
142142
msg = join(lines, '\n')
143143
if sev == CUDNN_SEV_INFO

Diff for: lib/cudnn/src/libcudnn.jl

+1,694-1,587
Large diffs are not rendered by default.

Diff for: lib/cufft/libcufft.jl

+88-77
Large diffs are not rendered by default.

Diff for: lib/cupti/libcupti.jl

+235-226
Large diffs are not rendered by default.

Diff for: lib/cupti/wrappers.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ end
1212
# multiple subscribers aren't supported, so make sure we only call CUPTI once
1313
const callback_lock = ReentrantLock()
1414

15-
function callback(userdata::Ptr{Cvoid}, domain::CUpti_CallbackDomain,
16-
id::CUpti_CallbackId, data_ptr::Ptr{Cvoid})
15+
@gcunsafe_callback function callback(userdata::Ptr{Cvoid}, domain::CUpti_CallbackDomain,
16+
id::CUpti_CallbackId, data_ptr::Ptr{Cvoid})
1717
cfg = Base.unsafe_pointer_to_objref(userdata)::CallbackConfig
1818

1919
# decode the callback data
@@ -126,7 +126,7 @@ end
126126
const activity_lock = ReentrantLock()
127127
const activity_config = Ref{Union{Nothing,ActivityConfig}}(nothing)
128128

129-
function request_buffer(dest_ptr, sz_ptr, max_num_records_ptr)
129+
@gcunsafe_callback function request_buffer(dest_ptr, sz_ptr, max_num_records_ptr)
130130
# this function is called by CUPTI, but directly from the application, so it should be
131131
# fine to perform I/O or allocate memory here.
132132

@@ -157,7 +157,7 @@ function request_buffer(dest_ptr, sz_ptr, max_num_records_ptr)
157157
return
158158
end
159159

160-
function complete_buffer(ctx_handle, stream_id, buf_ptr, sz, valid_sz)
160+
@gcunsafe_callback function complete_buffer(ctx_handle, stream_id, buf_ptr, sz, valid_sz)
161161
# this function is called by a CUPTI worker thread while our application may be waiting
162162
# for `cuptiActivityFlushAll` to complete. that means we cannot do I/O here, or we could
163163
# yield while the application cannot make any progress.

Diff for: lib/curand/libcurand.jl

+70-67
Original file line numberDiff line numberDiff line change
@@ -141,181 +141,184 @@ const curandMethod_t = curandMethod
141141

142142
@checked function curandCreateGenerator(generator, rng_type)
143143
initialize_context()
144-
@ccall libcurand.curandCreateGenerator(generator::Ptr{curandGenerator_t},
145-
rng_type::curandRngType_t)::curandStatus_t
144+
@gcsafe_ccall libcurand.curandCreateGenerator(generator::Ptr{curandGenerator_t},
145+
rng_type::curandRngType_t)::curandStatus_t
146146
end
147147

148148
@checked function curandCreateGeneratorHost(generator, rng_type)
149149
initialize_context()
150-
@ccall libcurand.curandCreateGeneratorHost(generator::Ptr{curandGenerator_t},
151-
rng_type::curandRngType_t)::curandStatus_t
150+
@gcsafe_ccall libcurand.curandCreateGeneratorHost(generator::Ptr{curandGenerator_t},
151+
rng_type::curandRngType_t)::curandStatus_t
152152
end
153153

154154
@checked function curandDestroyGenerator(generator)
155155
initialize_context()
156-
@ccall libcurand.curandDestroyGenerator(generator::curandGenerator_t)::curandStatus_t
156+
@gcsafe_ccall libcurand.curandDestroyGenerator(generator::curandGenerator_t)::curandStatus_t
157157
end
158158

159159
@checked function curandGetVersion(version)
160-
@ccall libcurand.curandGetVersion(version::Ptr{Cint})::curandStatus_t
160+
@gcsafe_ccall libcurand.curandGetVersion(version::Ptr{Cint})::curandStatus_t
161161
end
162162

163163
@checked function curandGetProperty(type, value)
164-
@ccall libcurand.curandGetProperty(type::libraryPropertyType,
165-
value::Ptr{Cint})::curandStatus_t
164+
@gcsafe_ccall libcurand.curandGetProperty(type::libraryPropertyType,
165+
value::Ptr{Cint})::curandStatus_t
166166
end
167167

168168
@checked function curandSetStream(generator, stream)
169169
initialize_context()
170-
@ccall libcurand.curandSetStream(generator::curandGenerator_t,
171-
stream::cudaStream_t)::curandStatus_t
170+
@gcsafe_ccall libcurand.curandSetStream(generator::curandGenerator_t,
171+
stream::cudaStream_t)::curandStatus_t
172172
end
173173

174174
@checked function curandSetPseudoRandomGeneratorSeed(generator, seed)
175175
initialize_context()
176-
@ccall libcurand.curandSetPseudoRandomGeneratorSeed(generator::curandGenerator_t,
177-
seed::Culonglong)::curandStatus_t
176+
@gcsafe_ccall libcurand.curandSetPseudoRandomGeneratorSeed(generator::curandGenerator_t,
177+
seed::Culonglong)::curandStatus_t
178178
end
179179

180180
@checked function curandSetGeneratorOffset(generator, offset)
181181
initialize_context()
182-
@ccall libcurand.curandSetGeneratorOffset(generator::curandGenerator_t,
183-
offset::Culonglong)::curandStatus_t
182+
@gcsafe_ccall libcurand.curandSetGeneratorOffset(generator::curandGenerator_t,
183+
offset::Culonglong)::curandStatus_t
184184
end
185185

186186
@checked function curandSetGeneratorOrdering(generator, order)
187187
initialize_context()
188-
@ccall libcurand.curandSetGeneratorOrdering(generator::curandGenerator_t,
189-
order::curandOrdering_t)::curandStatus_t
188+
@gcsafe_ccall libcurand.curandSetGeneratorOrdering(generator::curandGenerator_t,
189+
order::curandOrdering_t)::curandStatus_t
190190
end
191191

192192
@checked function curandSetQuasiRandomGeneratorDimensions(generator, num_dimensions)
193193
initialize_context()
194-
@ccall libcurand.curandSetQuasiRandomGeneratorDimensions(generator::curandGenerator_t,
195-
num_dimensions::Cuint)::curandStatus_t
194+
@gcsafe_ccall libcurand.curandSetQuasiRandomGeneratorDimensions(generator::curandGenerator_t,
195+
num_dimensions::Cuint)::curandStatus_t
196196
end
197197

198198
@checked function curandGenerate(generator, outputPtr, num)
199199
initialize_context()
200-
@ccall libcurand.curandGenerate(generator::curandGenerator_t, outputPtr::CuPtr{UInt32},
201-
num::Csize_t)::curandStatus_t
200+
@gcsafe_ccall libcurand.curandGenerate(generator::curandGenerator_t,
201+
outputPtr::CuPtr{UInt32},
202+
num::Csize_t)::curandStatus_t
202203
end
203204

204205
@checked function curandGenerateLongLong(generator, outputPtr, num)
205206
initialize_context()
206-
@ccall libcurand.curandGenerateLongLong(generator::curandGenerator_t,
207-
outputPtr::CuPtr{Culonglong},
208-
num::Csize_t)::curandStatus_t
207+
@gcsafe_ccall libcurand.curandGenerateLongLong(generator::curandGenerator_t,
208+
outputPtr::CuPtr{Culonglong},
209+
num::Csize_t)::curandStatus_t
209210
end
210211

211212
@checked function curandGenerateUniform(generator, outputPtr, num)
212213
initialize_context()
213-
@ccall libcurand.curandGenerateUniform(generator::curandGenerator_t,
214-
outputPtr::CuPtr{Cfloat},
215-
num::Csize_t)::curandStatus_t
214+
@gcsafe_ccall libcurand.curandGenerateUniform(generator::curandGenerator_t,
215+
outputPtr::CuPtr{Cfloat},
216+
num::Csize_t)::curandStatus_t
216217
end
217218

218219
@checked function curandGenerateUniformDouble(generator, outputPtr, num)
219220
initialize_context()
220-
@ccall libcurand.curandGenerateUniformDouble(generator::curandGenerator_t,
221-
outputPtr::CuPtr{Cdouble},
222-
num::Csize_t)::curandStatus_t
221+
@gcsafe_ccall libcurand.curandGenerateUniformDouble(generator::curandGenerator_t,
222+
outputPtr::CuPtr{Cdouble},
223+
num::Csize_t)::curandStatus_t
223224
end
224225

225226
@checked function curandGenerateNormal(generator, outputPtr, n, mean, stddev)
226227
initialize_context()
227-
@ccall libcurand.curandGenerateNormal(generator::curandGenerator_t,
228-
outputPtr::CuPtr{Cfloat}, n::Csize_t,
229-
mean::Cfloat, stddev::Cfloat)::curandStatus_t
228+
@gcsafe_ccall libcurand.curandGenerateNormal(generator::curandGenerator_t,
229+
outputPtr::CuPtr{Cfloat}, n::Csize_t,
230+
mean::Cfloat,
231+
stddev::Cfloat)::curandStatus_t
230232
end
231233

232234
@checked function curandGenerateNormalDouble(generator, outputPtr, n, mean, stddev)
233235
initialize_context()
234-
@ccall libcurand.curandGenerateNormalDouble(generator::curandGenerator_t,
235-
outputPtr::CuPtr{Cdouble}, n::Csize_t,
236-
mean::Cdouble,
237-
stddev::Cdouble)::curandStatus_t
236+
@gcsafe_ccall libcurand.curandGenerateNormalDouble(generator::curandGenerator_t,
237+
outputPtr::CuPtr{Cdouble},
238+
n::Csize_t, mean::Cdouble,
239+
stddev::Cdouble)::curandStatus_t
238240
end
239241

240242
@checked function curandGenerateLogNormal(generator, outputPtr, n, mean, stddev)
241243
initialize_context()
242-
@ccall libcurand.curandGenerateLogNormal(generator::curandGenerator_t,
243-
outputPtr::CuPtr{Cfloat}, n::Csize_t,
244-
mean::Cfloat, stddev::Cfloat)::curandStatus_t
244+
@gcsafe_ccall libcurand.curandGenerateLogNormal(generator::curandGenerator_t,
245+
outputPtr::CuPtr{Cfloat}, n::Csize_t,
246+
mean::Cfloat,
247+
stddev::Cfloat)::curandStatus_t
245248
end
246249

247250
@checked function curandGenerateLogNormalDouble(generator, outputPtr, n, mean, stddev)
248251
initialize_context()
249-
@ccall libcurand.curandGenerateLogNormalDouble(generator::curandGenerator_t,
250-
outputPtr::CuPtr{Cdouble}, n::Csize_t,
251-
mean::Cdouble,
252-
stddev::Cdouble)::curandStatus_t
252+
@gcsafe_ccall libcurand.curandGenerateLogNormalDouble(generator::curandGenerator_t,
253+
outputPtr::CuPtr{Cdouble},
254+
n::Csize_t, mean::Cdouble,
255+
stddev::Cdouble)::curandStatus_t
253256
end
254257

255258
@checked function curandCreatePoissonDistribution(lambda, discrete_distribution)
256259
initialize_context()
257-
@ccall libcurand.curandCreatePoissonDistribution(lambda::Cdouble,
258-
discrete_distribution::Ptr{curandDiscreteDistribution_t})::curandStatus_t
260+
@gcsafe_ccall libcurand.curandCreatePoissonDistribution(lambda::Cdouble,
261+
discrete_distribution::Ptr{curandDiscreteDistribution_t})::curandStatus_t
259262
end
260263

261264
@checked function curandDestroyDistribution(discrete_distribution)
262265
initialize_context()
263-
@ccall libcurand.curandDestroyDistribution(discrete_distribution::curandDiscreteDistribution_t)::curandStatus_t
266+
@gcsafe_ccall libcurand.curandDestroyDistribution(discrete_distribution::curandDiscreteDistribution_t)::curandStatus_t
264267
end
265268

266269
@checked function curandGeneratePoisson(generator, outputPtr, n, lambda)
267270
initialize_context()
268-
@ccall libcurand.curandGeneratePoisson(generator::curandGenerator_t,
269-
outputPtr::CuPtr{UInt32}, n::Csize_t,
270-
lambda::Cdouble)::curandStatus_t
271+
@gcsafe_ccall libcurand.curandGeneratePoisson(generator::curandGenerator_t,
272+
outputPtr::CuPtr{UInt32}, n::Csize_t,
273+
lambda::Cdouble)::curandStatus_t
271274
end
272275

273276
@checked function curandGeneratePoissonMethod(generator, outputPtr, n, lambda, method)
274277
initialize_context()
275-
@ccall libcurand.curandGeneratePoissonMethod(generator::curandGenerator_t,
276-
outputPtr::CuPtr{UInt32}, n::Csize_t,
277-
lambda::Cdouble,
278-
method::curandMethod_t)::curandStatus_t
278+
@gcsafe_ccall libcurand.curandGeneratePoissonMethod(generator::curandGenerator_t,
279+
outputPtr::CuPtr{UInt32},
280+
n::Csize_t, lambda::Cdouble,
281+
method::curandMethod_t)::curandStatus_t
279282
end
280283

281284
@checked function curandGenerateBinomial(generator, outputPtr, num, n, p)
282285
initialize_context()
283-
@ccall libcurand.curandGenerateBinomial(generator::curandGenerator_t,
284-
outputPtr::CuPtr{UInt32}, num::Csize_t,
285-
n::Cuint, p::Cdouble)::curandStatus_t
286+
@gcsafe_ccall libcurand.curandGenerateBinomial(generator::curandGenerator_t,
287+
outputPtr::CuPtr{UInt32}, num::Csize_t,
288+
n::Cuint, p::Cdouble)::curandStatus_t
286289
end
287290

288291
@checked function curandGenerateBinomialMethod(generator, outputPtr, num, n, p, method)
289292
initialize_context()
290-
@ccall libcurand.curandGenerateBinomialMethod(generator::curandGenerator_t,
291-
outputPtr::CuPtr{UInt32}, num::Csize_t,
292-
n::Cuint, p::Cdouble,
293-
method::curandMethod_t)::curandStatus_t
293+
@gcsafe_ccall libcurand.curandGenerateBinomialMethod(generator::curandGenerator_t,
294+
outputPtr::CuPtr{UInt32},
295+
num::Csize_t, n::Cuint, p::Cdouble,
296+
method::curandMethod_t)::curandStatus_t
294297
end
295298

296299
@checked function curandGenerateSeeds(generator)
297300
initialize_context()
298-
@ccall libcurand.curandGenerateSeeds(generator::curandGenerator_t)::curandStatus_t
301+
@gcsafe_ccall libcurand.curandGenerateSeeds(generator::curandGenerator_t)::curandStatus_t
299302
end
300303

301304
@checked function curandGetDirectionVectors32(vectors, set)
302305
initialize_context()
303-
@ccall libcurand.curandGetDirectionVectors32(vectors::Ptr{Ptr{curandDirectionVectors32_t}},
304-
set::curandDirectionVectorSet_t)::curandStatus_t
306+
@gcsafe_ccall libcurand.curandGetDirectionVectors32(vectors::Ptr{Ptr{curandDirectionVectors32_t}},
307+
set::curandDirectionVectorSet_t)::curandStatus_t
305308
end
306309

307310
@checked function curandGetScrambleConstants32(constants)
308311
initialize_context()
309-
@ccall libcurand.curandGetScrambleConstants32(constants::Ptr{Ptr{Cuint}})::curandStatus_t
312+
@gcsafe_ccall libcurand.curandGetScrambleConstants32(constants::Ptr{Ptr{Cuint}})::curandStatus_t
310313
end
311314

312315
@checked function curandGetDirectionVectors64(vectors, set)
313316
initialize_context()
314-
@ccall libcurand.curandGetDirectionVectors64(vectors::Ptr{Ptr{curandDirectionVectors64_t}},
315-
set::curandDirectionVectorSet_t)::curandStatus_t
317+
@gcsafe_ccall libcurand.curandGetDirectionVectors64(vectors::Ptr{Ptr{curandDirectionVectors64_t}},
318+
set::curandDirectionVectorSet_t)::curandStatus_t
316319
end
317320

318321
@checked function curandGetScrambleConstants64(constants)
319322
initialize_context()
320-
@ccall libcurand.curandGetScrambleConstants64(constants::Ptr{Ptr{Culonglong}})::curandStatus_t
323+
@gcsafe_ccall libcurand.curandGetScrambleConstants64(constants::Ptr{Ptr{Culonglong}})::curandStatus_t
321324
end

0 commit comments

Comments
 (0)