|
1 | 1 | using Base.BinaryPlatforms |
2 | 2 |
|
3 | | -using Libdl |
4 | | - |
5 | | -# re-use the CUDA_Runtime_jll preference to select the appropriate compiler |
6 | | -const CUDA_Runtime_jll_uuid = Base.UUID("76a88914-d11a-5bdc-97e0-2f5a05c973a2") |
7 | | -const preferences = Base.get_preferences(CUDA_Runtime_jll_uuid) |
8 | | -function parse_version_preference(key) |
9 | | - if haskey(preferences, key) |
10 | | - if isa(preferences[key], String) |
11 | | - version = tryparse(VersionNumber, preferences[key]) |
12 | | - if version === nothing |
13 | | - @error "CUDA $key preference is not valid; expected a version number, but got '$(preferences[key])'" |
14 | | - missing |
15 | | - else |
16 | | - version |
17 | | - end |
18 | | - else |
19 | | - @error "CUDA $key preference is not valid; expected a version number, but got '$(preferences[key])'" |
20 | | - missing |
21 | | - end |
22 | | - else |
23 | | - missing |
24 | | - end |
25 | | -end |
26 | | -const version_preference = parse_version_preference("version") |
27 | | - |
28 | | -if ismissing(version_preference) |
29 | | - # before loading CUDA_Driver_jll, try to find out where the system driver is located. |
30 | | - let |
31 | | - name = if Sys.iswindows() |
32 | | - Libdl.find_library("nvcuda") |
33 | | - else |
34 | | - Libdl.find_library(["libcuda.so.1", "libcuda.so"]) |
35 | | - end |
36 | | - |
37 | | - # if we've found a system driver, put a dependency on it, |
38 | | - # so that we get recompiled if the driver changes. |
39 | | - if name != "" |
40 | | - handle = Libdl.dlopen(name) |
41 | | - path = Libdl.dlpath(handle) |
42 | | - Libdl.dlclose(handle) |
43 | | - |
44 | | - @debug "Adding include dependency on $path" |
45 | | - Base.include_dependency(path) |
46 | | - end |
47 | | - end |
48 | | -end |
49 | | - |
50 | | -# platform augmentation hooks run in an ill-defined environment, where: |
51 | | -# - CUDA_Driver_jll may not be available |
52 | | -# - the wrong version of CUDA_Driver_jll may be available |
53 | | -# |
54 | | -# because of that, we need to be very careful about using that dependency. |
55 | | -# currently, we support all existing versions of CUDA_Driver_jll, but if we |
56 | | -# ever need to introduce a breaking change, we'll need some way to identify |
57 | | -# the version of CUDA_Driver_jll from its module (e.g. a global constant). |
58 | | -# |
59 | | -# ref: https://github.com/JuliaLang/Pkg.jl/issues/3225 |
60 | | -# can't use Preferences for the same reason |
61 | 3 | try |
62 | | - using CUDA_Driver_jll |
63 | | -catch err |
64 | | - # we'll handle this below |
| 4 | + using CUDA_Runtime_jll |
| 5 | +catch |
| 6 | + # during initial package installation, CUDA_Runtime_jll may not be available. |
| 7 | + # in that case, we just won't select an artifact. |
65 | 8 | end |
66 | 9 |
|
67 | | -# get the version of the available CUDA driver by querying either CUDA_Driver_jll's |
68 | | -# driver, or the system driver if CUDA_Driver_jll is not available |
69 | | -function get_driver_version() |
70 | | - if !@isdefined(CUDA_Driver_jll) |
71 | | - # driver JLL not available because we're in the middle of installing packages |
72 | | - @debug "CUDA_Driver_jll not available; not selecting an artifact" |
73 | | - return nothing |
74 | | - end |
75 | | - |
76 | | - cuda_driver = if CUDA_Driver_jll.is_available() |
77 | | - @debug "Using CUDA_Driver_jll for driver discovery" |
78 | | - |
79 | | - if !isdefined(CUDA_Driver_jll, :libcuda) || # [email protected] |
80 | | - isnothing(CUDA_Driver_jll.libcuda) # https://github.com/JuliaLang/julia/issues/48999 |
81 | | - # no driver found |
82 | | - @debug "CUDA_Driver_jll reports no driver found" |
83 | | - return nothing |
84 | | - end |
85 | | - CUDA_Driver_jll.libcuda |
86 | | - else |
87 | | - # CUDA_Driver_jll only kicks in for supported platforms, so fall back to |
88 | | - # a system search if the artifact isn't available (JLLWrappers.jl#50) |
89 | | - @debug "CUDA_Driver_jll unavailable, falling back to system search" |
| 10 | +# can't use Preferences for the same reason |
| 11 | +const CUDA_Runtime_jll_uuid = Base.UUID("76a88914-d11a-5bdc-97e0-2f5a05c973a2") |
| 12 | +const preferences = Base.get_preferences(CUDA_Runtime_jll_uuid) |
| 13 | +Base.record_compiletime_preference(CUDA_Runtime_jll_uuid, "version") |
| 14 | +Base.record_compiletime_preference(CUDA_Runtime_jll_uuid, "local") |
90 | 15 |
|
91 | | - driver_name = if Sys.iswindows() |
92 | | - Libdl.find_library("nvcuda") |
| 16 | +function augment_platform!(platform::Platform) |
| 17 | + platform["cuda"] = if @isdefined(CUDA_Runtime_jll) |
| 18 | + cuda = cuda_toolkit_tag() |
| 19 | + if cuda === nothing |
| 20 | + "none" |
93 | 21 | else |
94 | | - Libdl.find_library(["libcuda.so.1", "libcuda.so"]) |
95 | | - end |
96 | | - if driver_name == "" |
97 | | - # no driver found |
98 | | - @debug "CUDA_Driver_jll unavailable, and no system CUDA driver found" |
99 | | - return nothing |
| 22 | + # extract major version |
| 23 | + cuda_version = parse(VersionNumber, cuda) |
| 24 | + "$(cuda_version.major)" |
100 | 25 | end |
101 | | - |
102 | | - driver_name |
103 | | - end |
104 | | - @debug "Found CUDA driver at '$cuda_driver'" |
105 | | - |
106 | | - # minimal API call wrappers we need |
107 | | - function cuDriverGetVersion(library_handle) |
108 | | - function_handle = Libdl.dlsym(library_handle, "cuDriverGetVersion"; throw_error=false) |
109 | | - if function_handle === nothing |
110 | | - @debug "Driver library seems invalid (does not contain 'cuDriverGetVersion')" |
111 | | - return nothing |
112 | | - end |
113 | | - version_ref = Ref{Cint}() |
114 | | - status = ccall(function_handle, Cint, (Ptr{Cint},), version_ref) |
115 | | - if status != 0 |
116 | | - @debug "Call to 'cuDriverGetVersion' failed with status $status" |
117 | | - return nothing |
118 | | - end |
119 | | - major, ver = divrem(version_ref[], 1000) |
120 | | - minor, patch = divrem(ver, 10) |
121 | | - return VersionNumber(major, minor, patch) |
122 | | - end |
123 | | - |
124 | | - driver_handle = Libdl.dlopen(cuda_driver; throw_error=false) |
125 | | - if driver_handle === nothing |
126 | | - @debug "Failed to load CUDA driver" |
127 | | - return nothing |
128 | | - end |
129 | | - |
130 | | - cuDriverGetVersion(driver_handle) |
131 | | -end |
132 | | - |
133 | | -# returns the value for the "cuda" tag we should use in the platform ("$MAJOR") |
134 | | -# or nothing if no CUDA driver was found. |
135 | | -function cuda_driver_tag() |
136 | | - if version_preference !== missing |
137 | | - @debug "CUDA version override: $version_preference" |
138 | | - "$(version_preference.major)" |
139 | 26 | else |
140 | | - cuda_driver = get_driver_version() |
141 | | - if cuda_driver === nothing |
142 | | - @debug "Failed to query CUDA driver version" |
143 | | - return nothing |
144 | | - end |
145 | | - @debug "CUDA driver version: $cuda_driver" |
146 | | - |
147 | | - "$(cuda_driver.major)" |
148 | | - end |
149 | | -end |
150 | | - |
151 | | -function augment_platform!(platform::Platform) |
152 | | - if !haskey(platform, "cuda") |
153 | | - platform["cuda"] = something(cuda_driver_tag(), "none") |
154 | | - # XXX: use "none" when we couldn't find a compatible toolkit. |
155 | | - # we can't just leave off the platform tag or Pkg would select *any* artifact. |
| 27 | + "none" |
156 | 28 | end |
157 | 29 |
|
158 | 30 | return platform |
|
0 commit comments