Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Defer CUDA initialization to run time using libcuda getter. #211

Merged
merged 1 commit into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions src/CUDAdrv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ include("pointer.jl")
const CUdeviceptr = CuPtr{Cvoid}

# low-level wrappers
const libcuda = Sys.iswindows() ? :nvcuda : :libcuda
include("libcuda_common.jl")
include("error.jl")
include("libcuda.jl")
Expand All @@ -38,36 +37,47 @@ include("deprecated.jl")

## initialization

const __initialized__ = Ref(false)
functional() = __initialized__[]

function __init__()
precompiling = ccall(:jl_generating_output, Cint, ()) != 0
silent = parse(Bool, get(ENV, "JULIA_CUDA_SILENT", "false")) || precompiling
verbose = parse(Bool, get(ENV, "JULIA_CUDA_VERBOSE", "false"))

try
if haskey(ENV, "_") && basename(ENV["_"]) == "rr"
error("Running under rr, which is incompatible with CUDA")
end

cuInit(0)

if version() < v"9"
silent || @warn "CUDAdrv.jl only supports NVIDIA drivers for CUDA 9.0 or higher (yours is for CUDA $(version()))"
end

__initialized__[] = true
catch ex
# don't actually fail to keep the package loadable
if !silent
if verbose
@error "CUDAdrv.jl failed to initialize" exception=(ex, catch_backtrace())
else
@info "CUDAdrv.jl failed to initialize, GPU functionality unavailable (set JULIA_CUDA_SILENT or JULIA_CUDA_VERBOSE to silence or expand this message)"
end
end
# initialization of CUDA is deferred to run-time (to avoid package import latency,
# and to improve use on systems without a GPU), at the point of the first ccall.

const __initialized__ = Ref{Union{Nothing,Bool}}(nothing)
const __libcuda = Sys.iswindows() ? :nvcuda : :libcuda

"""
functional(show_reason=false)

Check if the package has been initialized successfully and is ready to use.

This call is intended for packages that support conditionally using an available GPU. If you
fail to check whether CUDA is functional, actual use of functionality might warn and error.
"""
function functional(show_reason::Bool=false)
if __initialized__[] === nothing
__runtime_init__(show_reason)
end
__initialized__[]
end

function __runtime_init__(show_reason::Bool)
__initialized__[] = false

if haskey(ENV, "_") && basename(ENV["_"]) == "rr"
show_reason && @error("Running under rr, which is incompatible with CUDA")
return
end

@debug "Initializing CUDA driver"
ccall((:cuInit, __libcuda), CUresult, (UInt32,), 0)
__initialized__[] = true

if version() < v"9"
@warn "CUDAdrv.jl only supports NVIDIA drivers for CUDA 9.0 or higher (yours is for CUDA $(version()))"
end
end

function libcuda()
@assert functional(true) "CUDAdrv.jl is not functional"
__libcuda
end

end
Loading