Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark all CUDA ccalls as GC safe #2262

Merged
merged 11 commits into from
Feb 14, 2024
7 changes: 6 additions & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ steps:
println("--- :julia: Instantiating project")
withenv("JULIA_PKG_PRECOMPILE_AUTO" => 0) do
Pkg.activate(joinpath(pwd(), "lib", lowercase("{{matrix.package}}")))
Pkg.instantiate()
try
Pkg.instantiate()
catch
# if we fail to instantiate, assume that we need a newer CUDA.jl
Pkg.develop(path=".")
end

Pkg.add("CUDA_Runtime_jll")
write("LocalPreferences.toml", "[CUDA_Runtime_jll]\nversion = \"{{matrix.cuda}}\"")
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "CUDA"
uuid = "052768ef-5323-5732-b1bb-66c8b64840ba"
version = "5.2.0"
version = "5.3.0"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
2 changes: 1 addition & 1 deletion lib/cublas/CUBLAS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function log_message(ptr)
return
end

function _log_message(blob)
@gcunsafe_callback function _log_message(blob)
# the message format isn't documented, but it looks like a message starts with a capital
# and the severity (e.g. `I!`), and subsequent lines start with a lowercase mark (`!i`)
#
Expand Down
5,916 changes: 3,214 additions & 2,702 deletions lib/cublas/libcublas.jl

Large diffs are not rendered by default.

1,939 changes: 1,016 additions & 923 deletions lib/cudadrv/libcuda.jl

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions lib/cudadrv/occupancy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ end
# HACK: callback function for `launch_configuration` on platforms without support for
# trampolines as used by `@cfunction` (JuliaLang/julia#27174, JuliaLang/julia#32154)
_shmem_cb = nothing
_shmem_cint_cb(x::Cint) = Cint(something(_shmem_cb)(x))
@gcunsafe_callback function _shmem_cint_cb(x::Cint)
Cint(something(_shmem_cb)(x))
end
_shmem_cb_lock = Threads.ReentrantLock()

"""
Expand All @@ -58,7 +60,9 @@ function launch_configuration(fun::CuFunction; shmem::Union{Integer,Base.Callabl
if isa(shmem, Integer)
cuOccupancyMaxPotentialBlockSize(blocks_ref, threads_ref, fun, C_NULL, shmem, max_threads)
elseif Sys.ARCH == :x86 || Sys.ARCH == :x86_64
shmem_cint = threads -> Cint(shmem(threads))
@gcunsafe_callback function shmem_cint(threads)
Cint(shmem(threads))
end
cb = @cfunction($shmem_cint, Cint, (Cint,))
cuOccupancyMaxPotentialBlockSize(blocks_ref, threads_ref, fun, cb, 0, max_threads)
else
Expand Down
4 changes: 2 additions & 2 deletions lib/cudnn/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "cuDNN"
uuid = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
authors = ["Tim Besard <tim.besard@gmail.com>"]
version = "1.3.0"
version = "1.3.1"

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

[compat]
CEnum = "0.2, 0.3, 0.4, 0.5"
CUDA = "~5.1, ~5.2"
CUDA = "~5.3"
CUDA_Runtime_Discovery = "0.2"
CUDNN_jll = "~8.9"
julia = "1.8"
2 changes: 1 addition & 1 deletion lib/cudnn/src/cuDNN.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function log_message(sev, udata, dbg_ptr, ptr)
return
end

function _log_message(sev, dbg, str)
@gcunsafe_callback function _log_message(sev, dbg, str)
lines = split(str, '\0')
msg = join(lines, '\n')
if sev == CUDNN_SEV_INFO
Expand Down
3,281 changes: 1,694 additions & 1,587 deletions lib/cudnn/src/libcudnn.jl

Large diffs are not rendered by default.

165 changes: 88 additions & 77 deletions lib/cufft/libcufft.jl

Large diffs are not rendered by default.

461 changes: 235 additions & 226 deletions lib/cupti/libcupti.jl

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/cupti/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ end
# multiple subscribers aren't supported, so make sure we only call CUPTI once
const callback_lock = ReentrantLock()

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

# decode the callback data
Expand Down Expand Up @@ -126,7 +126,7 @@ end
const activity_lock = ReentrantLock()
const activity_config = Ref{Union{Nothing,ActivityConfig}}(nothing)

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

Expand Down Expand Up @@ -157,7 +157,7 @@ function request_buffer(dest_ptr, sz_ptr, max_num_records_ptr)
return
end

function complete_buffer(ctx_handle, stream_id, buf_ptr, sz, valid_sz)
@gcunsafe_callback function complete_buffer(ctx_handle, stream_id, buf_ptr, sz, valid_sz)
# this function is called by a CUPTI worker thread while our application may be waiting
# for `cuptiActivityFlushAll` to complete. that means we cannot do I/O here, or we could
# yield while the application cannot make any progress.
Expand Down
137 changes: 70 additions & 67 deletions lib/curand/libcurand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,181 +141,184 @@ const curandMethod_t = curandMethod

@checked function curandCreateGenerator(generator, rng_type)
initialize_context()
@ccall libcurand.curandCreateGenerator(generator::Ptr{curandGenerator_t},
rng_type::curandRngType_t)::curandStatus_t
@gcsafe_ccall libcurand.curandCreateGenerator(generator::Ptr{curandGenerator_t},
rng_type::curandRngType_t)::curandStatus_t
end

@checked function curandCreateGeneratorHost(generator, rng_type)
initialize_context()
@ccall libcurand.curandCreateGeneratorHost(generator::Ptr{curandGenerator_t},
rng_type::curandRngType_t)::curandStatus_t
@gcsafe_ccall libcurand.curandCreateGeneratorHost(generator::Ptr{curandGenerator_t},
rng_type::curandRngType_t)::curandStatus_t
end

@checked function curandDestroyGenerator(generator)
initialize_context()
@ccall libcurand.curandDestroyGenerator(generator::curandGenerator_t)::curandStatus_t
@gcsafe_ccall libcurand.curandDestroyGenerator(generator::curandGenerator_t)::curandStatus_t
end

@checked function curandGetVersion(version)
@ccall libcurand.curandGetVersion(version::Ptr{Cint})::curandStatus_t
@gcsafe_ccall libcurand.curandGetVersion(version::Ptr{Cint})::curandStatus_t
end

@checked function curandGetProperty(type, value)
@ccall libcurand.curandGetProperty(type::libraryPropertyType,
value::Ptr{Cint})::curandStatus_t
@gcsafe_ccall libcurand.curandGetProperty(type::libraryPropertyType,
value::Ptr{Cint})::curandStatus_t
end

@checked function curandSetStream(generator, stream)
initialize_context()
@ccall libcurand.curandSetStream(generator::curandGenerator_t,
stream::cudaStream_t)::curandStatus_t
@gcsafe_ccall libcurand.curandSetStream(generator::curandGenerator_t,
stream::cudaStream_t)::curandStatus_t
end

@checked function curandSetPseudoRandomGeneratorSeed(generator, seed)
initialize_context()
@ccall libcurand.curandSetPseudoRandomGeneratorSeed(generator::curandGenerator_t,
seed::Culonglong)::curandStatus_t
@gcsafe_ccall libcurand.curandSetPseudoRandomGeneratorSeed(generator::curandGenerator_t,
seed::Culonglong)::curandStatus_t
end

@checked function curandSetGeneratorOffset(generator, offset)
initialize_context()
@ccall libcurand.curandSetGeneratorOffset(generator::curandGenerator_t,
offset::Culonglong)::curandStatus_t
@gcsafe_ccall libcurand.curandSetGeneratorOffset(generator::curandGenerator_t,
offset::Culonglong)::curandStatus_t
end

@checked function curandSetGeneratorOrdering(generator, order)
initialize_context()
@ccall libcurand.curandSetGeneratorOrdering(generator::curandGenerator_t,
order::curandOrdering_t)::curandStatus_t
@gcsafe_ccall libcurand.curandSetGeneratorOrdering(generator::curandGenerator_t,
order::curandOrdering_t)::curandStatus_t
end

@checked function curandSetQuasiRandomGeneratorDimensions(generator, num_dimensions)
initialize_context()
@ccall libcurand.curandSetQuasiRandomGeneratorDimensions(generator::curandGenerator_t,
num_dimensions::Cuint)::curandStatus_t
@gcsafe_ccall libcurand.curandSetQuasiRandomGeneratorDimensions(generator::curandGenerator_t,
num_dimensions::Cuint)::curandStatus_t
end

@checked function curandGenerate(generator, outputPtr, num)
initialize_context()
@ccall libcurand.curandGenerate(generator::curandGenerator_t, outputPtr::CuPtr{UInt32},
num::Csize_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerate(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32},
num::Csize_t)::curandStatus_t
end

@checked function curandGenerateLongLong(generator, outputPtr, num)
initialize_context()
@ccall libcurand.curandGenerateLongLong(generator::curandGenerator_t,
outputPtr::CuPtr{Culonglong},
num::Csize_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateLongLong(generator::curandGenerator_t,
outputPtr::CuPtr{Culonglong},
num::Csize_t)::curandStatus_t
end

@checked function curandGenerateUniform(generator, outputPtr, num)
initialize_context()
@ccall libcurand.curandGenerateUniform(generator::curandGenerator_t,
outputPtr::CuPtr{Cfloat},
num::Csize_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateUniform(generator::curandGenerator_t,
outputPtr::CuPtr{Cfloat},
num::Csize_t)::curandStatus_t
end

@checked function curandGenerateUniformDouble(generator, outputPtr, num)
initialize_context()
@ccall libcurand.curandGenerateUniformDouble(generator::curandGenerator_t,
outputPtr::CuPtr{Cdouble},
num::Csize_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateUniformDouble(generator::curandGenerator_t,
outputPtr::CuPtr{Cdouble},
num::Csize_t)::curandStatus_t
end

@checked function curandGenerateNormal(generator, outputPtr, n, mean, stddev)
initialize_context()
@ccall libcurand.curandGenerateNormal(generator::curandGenerator_t,
outputPtr::CuPtr{Cfloat}, n::Csize_t,
mean::Cfloat, stddev::Cfloat)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateNormal(generator::curandGenerator_t,
outputPtr::CuPtr{Cfloat}, n::Csize_t,
mean::Cfloat,
stddev::Cfloat)::curandStatus_t
end

@checked function curandGenerateNormalDouble(generator, outputPtr, n, mean, stddev)
initialize_context()
@ccall libcurand.curandGenerateNormalDouble(generator::curandGenerator_t,
outputPtr::CuPtr{Cdouble}, n::Csize_t,
mean::Cdouble,
stddev::Cdouble)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateNormalDouble(generator::curandGenerator_t,
outputPtr::CuPtr{Cdouble},
n::Csize_t, mean::Cdouble,
stddev::Cdouble)::curandStatus_t
end

@checked function curandGenerateLogNormal(generator, outputPtr, n, mean, stddev)
initialize_context()
@ccall libcurand.curandGenerateLogNormal(generator::curandGenerator_t,
outputPtr::CuPtr{Cfloat}, n::Csize_t,
mean::Cfloat, stddev::Cfloat)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateLogNormal(generator::curandGenerator_t,
outputPtr::CuPtr{Cfloat}, n::Csize_t,
mean::Cfloat,
stddev::Cfloat)::curandStatus_t
end

@checked function curandGenerateLogNormalDouble(generator, outputPtr, n, mean, stddev)
initialize_context()
@ccall libcurand.curandGenerateLogNormalDouble(generator::curandGenerator_t,
outputPtr::CuPtr{Cdouble}, n::Csize_t,
mean::Cdouble,
stddev::Cdouble)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateLogNormalDouble(generator::curandGenerator_t,
outputPtr::CuPtr{Cdouble},
n::Csize_t, mean::Cdouble,
stddev::Cdouble)::curandStatus_t
end

@checked function curandCreatePoissonDistribution(lambda, discrete_distribution)
initialize_context()
@ccall libcurand.curandCreatePoissonDistribution(lambda::Cdouble,
discrete_distribution::Ptr{curandDiscreteDistribution_t})::curandStatus_t
@gcsafe_ccall libcurand.curandCreatePoissonDistribution(lambda::Cdouble,
discrete_distribution::Ptr{curandDiscreteDistribution_t})::curandStatus_t
end

@checked function curandDestroyDistribution(discrete_distribution)
initialize_context()
@ccall libcurand.curandDestroyDistribution(discrete_distribution::curandDiscreteDistribution_t)::curandStatus_t
@gcsafe_ccall libcurand.curandDestroyDistribution(discrete_distribution::curandDiscreteDistribution_t)::curandStatus_t
end

@checked function curandGeneratePoisson(generator, outputPtr, n, lambda)
initialize_context()
@ccall libcurand.curandGeneratePoisson(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32}, n::Csize_t,
lambda::Cdouble)::curandStatus_t
@gcsafe_ccall libcurand.curandGeneratePoisson(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32}, n::Csize_t,
lambda::Cdouble)::curandStatus_t
end

@checked function curandGeneratePoissonMethod(generator, outputPtr, n, lambda, method)
initialize_context()
@ccall libcurand.curandGeneratePoissonMethod(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32}, n::Csize_t,
lambda::Cdouble,
method::curandMethod_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGeneratePoissonMethod(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32},
n::Csize_t, lambda::Cdouble,
method::curandMethod_t)::curandStatus_t
end

@checked function curandGenerateBinomial(generator, outputPtr, num, n, p)
initialize_context()
@ccall libcurand.curandGenerateBinomial(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32}, num::Csize_t,
n::Cuint, p::Cdouble)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateBinomial(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32}, num::Csize_t,
n::Cuint, p::Cdouble)::curandStatus_t
end

@checked function curandGenerateBinomialMethod(generator, outputPtr, num, n, p, method)
initialize_context()
@ccall libcurand.curandGenerateBinomialMethod(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32}, num::Csize_t,
n::Cuint, p::Cdouble,
method::curandMethod_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateBinomialMethod(generator::curandGenerator_t,
outputPtr::CuPtr{UInt32},
num::Csize_t, n::Cuint, p::Cdouble,
method::curandMethod_t)::curandStatus_t
end

@checked function curandGenerateSeeds(generator)
initialize_context()
@ccall libcurand.curandGenerateSeeds(generator::curandGenerator_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGenerateSeeds(generator::curandGenerator_t)::curandStatus_t
end

@checked function curandGetDirectionVectors32(vectors, set)
initialize_context()
@ccall libcurand.curandGetDirectionVectors32(vectors::Ptr{Ptr{curandDirectionVectors32_t}},
set::curandDirectionVectorSet_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGetDirectionVectors32(vectors::Ptr{Ptr{curandDirectionVectors32_t}},
set::curandDirectionVectorSet_t)::curandStatus_t
end

@checked function curandGetScrambleConstants32(constants)
initialize_context()
@ccall libcurand.curandGetScrambleConstants32(constants::Ptr{Ptr{Cuint}})::curandStatus_t
@gcsafe_ccall libcurand.curandGetScrambleConstants32(constants::Ptr{Ptr{Cuint}})::curandStatus_t
end

@checked function curandGetDirectionVectors64(vectors, set)
initialize_context()
@ccall libcurand.curandGetDirectionVectors64(vectors::Ptr{Ptr{curandDirectionVectors64_t}},
set::curandDirectionVectorSet_t)::curandStatus_t
@gcsafe_ccall libcurand.curandGetDirectionVectors64(vectors::Ptr{Ptr{curandDirectionVectors64_t}},
set::curandDirectionVectorSet_t)::curandStatus_t
end

@checked function curandGetScrambleConstants64(constants)
initialize_context()
@ccall libcurand.curandGetScrambleConstants64(constants::Ptr{Ptr{Culonglong}})::curandStatus_t
@gcsafe_ccall libcurand.curandGetScrambleConstants64(constants::Ptr{Ptr{Culonglong}})::curandStatus_t
end
Loading