Skip to content

Commit ca6e405

Browse files
committed
CUDA_Driver_jll build 0.1.0+0
1 parent 3027543 commit ca6e405

File tree

6 files changed

+400
-19
lines changed

6 files changed

+400
-19
lines changed

Artifacts.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ os = "linux"
77

88
[[CUDA_Driver.download]]
99
sha256 = "a6cd6aa5c0ceb95e2d14064e012ecfd81e2ea1104612a7e0aec1ddabe59c153b"
10-
url = "https://github.com/JuliaBinaryWrappers/CUDA_Driver_jll.jl/releases/download/CUDA_Driver-v11.8.0+0/CUDA_Driver.v11.8.0.x86_64-linux-gnu.tar.gz"
10+
url = "https://github.com/JuliaBinaryWrappers/CUDA_Driver_jll.jl/releases/download/CUDA_Driver-v0.1.0+0/CUDA_Driver.v0.1.0.x86_64-linux-gnu.tar.gz"
1111
[[CUDA_Driver]]
1212
arch = "powerpc64le"
1313
git-tree-sha1 = "7b4d06d1a0ff9386810fdf729b067d8ba77c97b5"
@@ -17,7 +17,7 @@ os = "linux"
1717

1818
[[CUDA_Driver.download]]
1919
sha256 = "c732d8d14860892002d8e1f492deb6cdce899bdf934d89c09b2f3e7bb5414a7e"
20-
url = "https://github.com/JuliaBinaryWrappers/CUDA_Driver_jll.jl/releases/download/CUDA_Driver-v11.8.0+0/CUDA_Driver.v11.8.0.powerpc64le-linux-gnu.tar.gz"
20+
url = "https://github.com/JuliaBinaryWrappers/CUDA_Driver_jll.jl/releases/download/CUDA_Driver-v0.1.0+0/CUDA_Driver.v0.1.0.powerpc64le-linux-gnu.tar.gz"
2121
[[CUDA_Driver]]
2222
arch = "aarch64"
2323
git-tree-sha1 = "6775921ab78b370d8da50fb0b79e1cff0baa94b9"
@@ -27,4 +27,4 @@ os = "linux"
2727

2828
[[CUDA_Driver.download]]
2929
sha256 = "2bbf905872c1d57d076c74aae9f7f57ecd2525ca3b36386eee8fbc18ca71dd7a"
30-
url = "https://github.com/JuliaBinaryWrappers/CUDA_Driver_jll.jl/releases/download/CUDA_Driver-v11.8.0+0/CUDA_Driver.v11.8.0.aarch64-linux-gnu.tar.gz"
30+
url = "https://github.com/JuliaBinaryWrappers/CUDA_Driver_jll.jl/releases/download/CUDA_Driver-v0.1.0+0/CUDA_Driver.v0.1.0.aarch64-linux-gnu.tar.gz"

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "CUDA_Driver_jll"
22
uuid = "4ee394cb-3365-5eb0-8335-949819d2adfc"
3-
version = "11.8.0+0"
3+
version = "0.1.0+0"
44

55
[deps]
66
JLLWrappers = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# `CUDA_Driver_jll.jl` (v11.8.0+0)
1+
# `CUDA_Driver_jll.jl` (v0.1.0+0)
22

33
[![deps](https://juliahub.com/docs/CUDA_Driver_jll/deps.svg)](https://juliahub.com/ui/Packages/CUDA_Driver_jll/QJyk7?page=2)
44

55
This is an autogenerated package constructed using [`BinaryBuilder.jl`](https://github.com/JuliaPackaging/BinaryBuilder.jl).
66

7-
The originating [`build_tarballs.jl`](https://github.com/JuliaPackaging/Yggdrasil/blob/4979fe23a14596cc3446f589763efdf1ff7ecc3b/C/CUDA/CUDA_Driver/build_tarballs.jl) script can be found on [`Yggdrasil`](https://github.com/JuliaPackaging/Yggdrasil/), the community build tree.
7+
The originating [`build_tarballs.jl`](https://github.com/JuliaPackaging/Yggdrasil/blob/558c4345b3f0cfb367490777144063d4582a0f77/C/CUDA/CUDA_Driver/build_tarballs.jl) script can be found on [`Yggdrasil`](https://github.com/JuliaPackaging/Yggdrasil/), the community build tree.
88

99
## Bug Reports
1010

@@ -30,5 +30,5 @@ The tarballs for `CUDA_Driver_jll.jl` have been built from these sources:
3030

3131
The code bindings within this package are autogenerated from the following `Products`:
3232

33-
* `LibraryProduct`: `libcuda`
33+
* `LibraryProduct`: `libcuda_compat`
3434
* `LibraryProduct`: `libnvidia_ptxjitcompiler`

src/wrappers/aarch64-linux-gnu.jl

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Autogenerated wrapper script for CUDA_Driver_jll for aarch64-linux-gnu
2-
export libcuda, libnvidia_ptxjitcompiler
2+
export libcuda_compat, libnvidia_ptxjitcompiler
33

44
JLLWrappers.@generate_wrapper_header("CUDA_Driver")
5-
JLLWrappers.@declare_library_product(libcuda, "libcuda.so.1")
5+
JLLWrappers.@declare_library_product(libcuda_compat, "libcuda.so.1")
66
JLLWrappers.@declare_library_product(libnvidia_ptxjitcompiler, "libnvidia-ptxjitcompiler.so.1")
77
function __init__()
88
JLLWrappers.@generate_init_header()
99
JLLWrappers.@init_library_product(
10-
libcuda,
10+
libcuda_compat,
1111
"lib/libcuda.so",
1212
nothing,
1313
)
@@ -19,6 +19,133 @@ function __init__()
1919
)
2020

2121
JLLWrappers.@generate_init_footer()
22-
global version = v"11.8.0"
22+
global compat_version = v"11.8.0"
23+
# minimal API call wrappers we need
24+
function driver_version(library_handle)
25+
function_handle = Libdl.dlsym(library_handle, "cuDriverGetVersion")
26+
version_ref = Ref{Cint}()
27+
status = ccall(function_handle, Cint, (Ptr{Cint},), version_ref)
28+
if status != 0
29+
return nothing
30+
end
31+
major, ver = divrem(version_ref[], 1000)
32+
minor, patch = divrem(ver, 10)
33+
return VersionNumber(major, minor, patch)
34+
end
35+
function init_driver(library_handle)
36+
function_handle = Libdl.dlsym(library_handle, "cuInit")
37+
status = ccall(function_handle, Cint, (UInt32,), 0)
38+
Libdl.dlclose(library_handle)
39+
return status
40+
end
41+
42+
# find the system driver
43+
system_driver = if Sys.iswindows()
44+
Libdl.find_library("nvcuda")
45+
else
46+
Libdl.find_library(["libcuda.so.1", "libcuda.so"])
47+
end
48+
if system_driver == ""
49+
@debug "No system CUDA driver found"
50+
return
51+
end
52+
global libcuda = system_driver
53+
54+
# check if the system driver is already loaded. in that case, we have to use it because
55+
# the code that loaded it in the first place might have made assumptions based on it.
56+
system_driver_loaded = Libdl.dlopen(system_driver, Libdl.RTLD_NOLOAD;
57+
throw_error=false) !== nothing
58+
if system_driver_loaded
59+
@debug "System CUDA driver already loaded, continuing using it"
60+
return
61+
end
62+
driver_handle = Libdl.dlopen(system_driver; throw_error=true)
63+
64+
# query the system driver version
65+
# XXX: apparently cuDriverGetVersion can be used before cuInit,
66+
# despite the docs stating "any function [...] will return
67+
# CUDA_ERROR_NOT_INITIALIZED"; is this a recent change?
68+
system_version = driver_version(driver_handle)
69+
if system_version === nothing
70+
@debug "Failed to query system CUDA driver version"
71+
# note that libcuda is already set here, so we'll continue using the system driver
72+
# and CUDA.jl will likely report the reason cuDriverGetVersion didn't work.
73+
return
74+
end
75+
@debug "System CUDA driver found at $system_driver, detected as version $system_version"
76+
77+
# check the user preference
78+
if !parse(Bool, get(ENV, "JULIA_CUDA_USE_COMPAT", "true"))
79+
@debug "User disallows using forward-compatible driver."
80+
return
81+
end
82+
83+
# check the version
84+
if system_version >= compat_version
85+
@debug "System CUDA driver is recent enough; not using forward-compatible driver"
86+
return
87+
end
88+
89+
# check if we can unload the system driver.
90+
# if we didn't, we can't consider a forward compatible library because that would
91+
# risk having multiple copies of libcuda.so loaded (also see NVIDIA bug #3418723)
92+
Libdl.dlclose(driver_handle)
93+
system_driver_loaded = Libdl.dlopen(system_driver, Libdl.RTLD_NOLOAD;
94+
throw_error=false) !== nothing
95+
if system_driver_loaded
96+
@debug "Could not unload the system CUDA library;" *
97+
" this prevents use of the forward-compatible driver"
98+
return
99+
end
100+
101+
# check if this process is hooked by CUDA's injection libraries, which prevents
102+
# unloading libcuda after dlopening. this is problematic, because we might want to
103+
# after loading a forwards-compatible libcuda and realizing we can't use it. without
104+
# being able to unload the library, we'd run into issues (see NVIDIA bug #3418723)
105+
hooked = haskey(ENV, "CUDA_INJECTION64_PATH")
106+
if hooked
107+
@debug "Running under CUDA injection tools;" *
108+
" this prevents use of the forward-compatible driver"
109+
return
110+
end
111+
112+
# check if we even have an artifact
113+
if !@isdefined(libcuda_compat)
114+
@debug "No forward-compatible CUDA library available for your platform."
115+
return
116+
end
117+
compat_driver = libcuda_compat
118+
@debug "Forward-compatible CUDA driver found at $compat_driver;" *
119+
" reported as version $(compat_version)"
120+
121+
# finally, load the compatibility driver to see if it supports this platform
122+
driver_handle = Libdl.dlopen(compat_driver; throw_error=true)
123+
# TODO: do we need to dlopen the JIT compiler library for it to be discoverable?
124+
# doesn't that clash with a system one if compat cuInit fails? or should
125+
# we load it _after_ the compat driver initialization succeeds?
126+
#compiler_handle = libnvidia_ptxjitcompiler
127+
#Libdl.dlopen(compiler_handle)
128+
129+
init_status = init_driver(driver_handle)
130+
if init_status != 0
131+
@debug "Could not use forward compatibility package (error $init_status)"
132+
133+
# see comment above about unloading the system driver
134+
Libdl.dlclose(driver_handle)
135+
compat_driver_loaded = Libdl.dlopen(compat_driver, Libdl.RTLD_NOLOAD;
136+
throw_error=false) !== nothing
137+
if compat_driver_loaded
138+
error("Could not unload the forward compatible CUDA driver library." *
139+
"This is probably caused by running Julia under a tool that hooks CUDA API calls." *
140+
"In that case, prevent Julia from loading multiple drivers" *
141+
" by setting JULIA_CUDA_USE_COMPAT=false in your environment.")
142+
end
143+
144+
return
145+
end
146+
147+
@debug "Successfully loaded forwards-compatible CUDA driver"
148+
global system_version = system_version
149+
global libcuda = compat_driver
23150

24151
end # __init__()

src/wrappers/powerpc64le-linux-gnu.jl

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Autogenerated wrapper script for CUDA_Driver_jll for powerpc64le-linux-gnu
2-
export libcuda, libnvidia_ptxjitcompiler
2+
export libcuda_compat, libnvidia_ptxjitcompiler
33

44
JLLWrappers.@generate_wrapper_header("CUDA_Driver")
5-
JLLWrappers.@declare_library_product(libcuda, "libcuda.so.1")
5+
JLLWrappers.@declare_library_product(libcuda_compat, "libcuda.so.1")
66
JLLWrappers.@declare_library_product(libnvidia_ptxjitcompiler, "libnvidia-ptxjitcompiler.so.1")
77
function __init__()
88
JLLWrappers.@generate_init_header()
99
JLLWrappers.@init_library_product(
10-
libcuda,
10+
libcuda_compat,
1111
"lib/libcuda.so",
1212
nothing,
1313
)
@@ -19,6 +19,133 @@ function __init__()
1919
)
2020

2121
JLLWrappers.@generate_init_footer()
22-
global version = v"11.8.0"
22+
global compat_version = v"11.8.0"
23+
# minimal API call wrappers we need
24+
function driver_version(library_handle)
25+
function_handle = Libdl.dlsym(library_handle, "cuDriverGetVersion")
26+
version_ref = Ref{Cint}()
27+
status = ccall(function_handle, Cint, (Ptr{Cint},), version_ref)
28+
if status != 0
29+
return nothing
30+
end
31+
major, ver = divrem(version_ref[], 1000)
32+
minor, patch = divrem(ver, 10)
33+
return VersionNumber(major, minor, patch)
34+
end
35+
function init_driver(library_handle)
36+
function_handle = Libdl.dlsym(library_handle, "cuInit")
37+
status = ccall(function_handle, Cint, (UInt32,), 0)
38+
Libdl.dlclose(library_handle)
39+
return status
40+
end
41+
42+
# find the system driver
43+
system_driver = if Sys.iswindows()
44+
Libdl.find_library("nvcuda")
45+
else
46+
Libdl.find_library(["libcuda.so.1", "libcuda.so"])
47+
end
48+
if system_driver == ""
49+
@debug "No system CUDA driver found"
50+
return
51+
end
52+
global libcuda = system_driver
53+
54+
# check if the system driver is already loaded. in that case, we have to use it because
55+
# the code that loaded it in the first place might have made assumptions based on it.
56+
system_driver_loaded = Libdl.dlopen(system_driver, Libdl.RTLD_NOLOAD;
57+
throw_error=false) !== nothing
58+
if system_driver_loaded
59+
@debug "System CUDA driver already loaded, continuing using it"
60+
return
61+
end
62+
driver_handle = Libdl.dlopen(system_driver; throw_error=true)
63+
64+
# query the system driver version
65+
# XXX: apparently cuDriverGetVersion can be used before cuInit,
66+
# despite the docs stating "any function [...] will return
67+
# CUDA_ERROR_NOT_INITIALIZED"; is this a recent change?
68+
system_version = driver_version(driver_handle)
69+
if system_version === nothing
70+
@debug "Failed to query system CUDA driver version"
71+
# note that libcuda is already set here, so we'll continue using the system driver
72+
# and CUDA.jl will likely report the reason cuDriverGetVersion didn't work.
73+
return
74+
end
75+
@debug "System CUDA driver found at $system_driver, detected as version $system_version"
76+
77+
# check the user preference
78+
if !parse(Bool, get(ENV, "JULIA_CUDA_USE_COMPAT", "true"))
79+
@debug "User disallows using forward-compatible driver."
80+
return
81+
end
82+
83+
# check the version
84+
if system_version >= compat_version
85+
@debug "System CUDA driver is recent enough; not using forward-compatible driver"
86+
return
87+
end
88+
89+
# check if we can unload the system driver.
90+
# if we didn't, we can't consider a forward compatible library because that would
91+
# risk having multiple copies of libcuda.so loaded (also see NVIDIA bug #3418723)
92+
Libdl.dlclose(driver_handle)
93+
system_driver_loaded = Libdl.dlopen(system_driver, Libdl.RTLD_NOLOAD;
94+
throw_error=false) !== nothing
95+
if system_driver_loaded
96+
@debug "Could not unload the system CUDA library;" *
97+
" this prevents use of the forward-compatible driver"
98+
return
99+
end
100+
101+
# check if this process is hooked by CUDA's injection libraries, which prevents
102+
# unloading libcuda after dlopening. this is problematic, because we might want to
103+
# after loading a forwards-compatible libcuda and realizing we can't use it. without
104+
# being able to unload the library, we'd run into issues (see NVIDIA bug #3418723)
105+
hooked = haskey(ENV, "CUDA_INJECTION64_PATH")
106+
if hooked
107+
@debug "Running under CUDA injection tools;" *
108+
" this prevents use of the forward-compatible driver"
109+
return
110+
end
111+
112+
# check if we even have an artifact
113+
if !@isdefined(libcuda_compat)
114+
@debug "No forward-compatible CUDA library available for your platform."
115+
return
116+
end
117+
compat_driver = libcuda_compat
118+
@debug "Forward-compatible CUDA driver found at $compat_driver;" *
119+
" reported as version $(compat_version)"
120+
121+
# finally, load the compatibility driver to see if it supports this platform
122+
driver_handle = Libdl.dlopen(compat_driver; throw_error=true)
123+
# TODO: do we need to dlopen the JIT compiler library for it to be discoverable?
124+
# doesn't that clash with a system one if compat cuInit fails? or should
125+
# we load it _after_ the compat driver initialization succeeds?
126+
#compiler_handle = libnvidia_ptxjitcompiler
127+
#Libdl.dlopen(compiler_handle)
128+
129+
init_status = init_driver(driver_handle)
130+
if init_status != 0
131+
@debug "Could not use forward compatibility package (error $init_status)"
132+
133+
# see comment above about unloading the system driver
134+
Libdl.dlclose(driver_handle)
135+
compat_driver_loaded = Libdl.dlopen(compat_driver, Libdl.RTLD_NOLOAD;
136+
throw_error=false) !== nothing
137+
if compat_driver_loaded
138+
error("Could not unload the forward compatible CUDA driver library." *
139+
"This is probably caused by running Julia under a tool that hooks CUDA API calls." *
140+
"In that case, prevent Julia from loading multiple drivers" *
141+
" by setting JULIA_CUDA_USE_COMPAT=false in your environment.")
142+
end
143+
144+
return
145+
end
146+
147+
@debug "Successfully loaded forwards-compatible CUDA driver"
148+
global system_version = system_version
149+
global libcuda = compat_driver
23150

24151
end # __init__()

0 commit comments

Comments
 (0)