Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Commit 739ff9a

Browse files
authored
Merge pull request #514 from JuliaGPU/tb/libname
Use at-runtime_ccall's ability to delay the library lookup.
2 parents cdcd4cc + 2a6583a commit 739ff9a

File tree

15 files changed

+1546
-1638
lines changed

15 files changed

+1546
-1638
lines changed

src/CuArrays.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ const __initialized__ = Ref(false)
5555
functional() = __initialized__[]
5656

5757
export has_cudnn, has_cutensor
58-
const libraries = Dict{String,Union{String,Nothing}}()
59-
has_cudnn() = libraries["cudnn"] !== nothing && CUDNN.libcudnn !== nothing
60-
has_cutensor() = libraries["cutensor"] !== nothing && CUTENSOR.libcutensor !== nothing
58+
has_cudnn() = Libdl.dlopen_e(CUDNN.libcudnn[]) !== C_NULL
59+
has_cutensor() = Libdl.dlopen_e(CUTENSOR.libcutensor[]) !== C_NULL
6160

6261
function __init__()
6362
precompiling = ccall(:jl_generating_output, Cint, ()) != 0
@@ -76,18 +75,20 @@ function __init__()
7675
for name in ("cublas", "cusparse", "cusolver", "cufft", "curand", "cudnn", "cutensor")
7776
mod = getfield(CuArrays, Symbol(uppercase(name)))
7877
lib = Symbol("lib$name")
79-
path = find_cuda_library(name, toolkit)
80-
libraries[name] = path
81-
82-
# only push the load path if we couldn't find the library
83-
if path !== nothing
84-
file = basename(path)
85-
handle = first(split(file,'.'))
86-
if Libdl.dlopen_e(handle) == C_NULL
87-
dir = dirname(path)
88-
if !(dir in Libdl.DL_LOAD_PATH)
89-
push!(Libdl.DL_LOAD_PATH, dir)
90-
end
78+
handle = getfield(mod, lib)
79+
80+
# on Windows, the library name is version dependent
81+
if Sys.iswindows()
82+
cuda = CUDAnative.version()
83+
suffix = cuda >= v"10.1" ? "$(cuda.major)" : "$(cuda.major)$(cuda.minor)"
84+
handle[] = "$(name)$(Sys.WORD_SIZE)_$(suffix)"
85+
end
86+
87+
# check if we can't find the library
88+
if Libdl.dlopen_e(handle[]) == C_NULL
89+
path = find_cuda_library(name, toolkit)
90+
if path !== nothing
91+
handle[] = path
9192
end
9293
end
9394
end

src/blas/CUBLAS.jl

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@ using LinearAlgebra
1313

1414
using CEnum
1515

16-
const libcublas = if Sys.iswindows()
17-
# no ccall by soname, we need the filename
18-
# NOTE: we discover the full path here, while only the wordsize and toolkit versions
19-
# would have been enough to construct "cublas64_10.dll"
20-
toolkit = find_toolkit()
21-
path = find_cuda_library("cublas", toolkit)
22-
if path === nothing
23-
error("Could not find libcublas")
24-
end
25-
const libcublas = basename(path)
26-
else
27-
# ccall by soname; CuArrays.__init__ will have populated Libdl.DL_LOAD_PATH
28-
const libcublas = "libcublas"
29-
end
16+
const libcublas = Ref("libcublas")
3017

3118
# core library
3219
include("libcublas_common.jl")

src/blas/libcublas.jl

Lines changed: 319 additions & 319 deletions
Large diffs are not rendered by default.

src/dnn/CUDNN.jl

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,7 @@ import ..CuArrays.unsafe_free!
1616

1717
import NNlib
1818

19-
const libcudnn = if Sys.iswindows()
20-
# no ccall by soname, we need the filename
21-
# NOTE: we discover the full path here, while only the wordsize and toolkit versions
22-
# would have been enough to construct "cudnn64_10.dll"
23-
toolkit = find_toolkit()
24-
path = find_cuda_library("cudnn", toolkit)
25-
if path === nothing
26-
nothing
27-
else
28-
basename(path)
29-
end
30-
else
31-
# ccall by soname; CuArrays.__init__ will have populated Libdl.DL_LOAD_PATH
32-
"libcudnn"
33-
end
19+
const libcudnn = Ref("libcudnn")
3420

3521
# core library
3622
include("libcudnn_common.jl")

src/dnn/libcudnn.jl

Lines changed: 231 additions & 231 deletions
Large diffs are not rendered by default.

src/fft/CUFFT.jl

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@ using CUDAdrv: CUstream
1010

1111
using CEnum
1212

13-
const libcufft = if Sys.iswindows()
14-
# no ccall by soname, we need the filename
15-
# NOTE: we discover the full path here, while only the wordsize and toolkit versions
16-
# would have been enough to construct "cufft64_10.dll"
17-
toolkit = find_toolkit()
18-
path = find_cuda_library("cufft", toolkit)
19-
if path === nothing
20-
error("Could not find libcufft")
21-
end
22-
basename(path)
23-
else
24-
# ccall by soname; CuArrays.__init__ will have populated Libdl.DL_LOAD_PATH
25-
"libcufft"
26-
end
13+
const libcufft = Ref("libcufft")
2714

2815
# core library
2916
include("libcufft_common.jl")

src/fft/libcufft.jl

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,53 @@
33

44

55
function cufftPlan1d(plan, nx, type, batch)
6-
@check @runtime_ccall((:cufftPlan1d, libcufft), cufftResult,
6+
@check @runtime_ccall((:cufftPlan1d, libcufft[]), cufftResult,
77
(Ptr{cufftHandle}, Cint, cufftType, Cint),
88
plan, nx, type, batch)
99
end
1010

1111
function cufftPlan2d(plan, nx, ny, type)
12-
@check @runtime_ccall((:cufftPlan2d, libcufft), cufftResult,
12+
@check @runtime_ccall((:cufftPlan2d, libcufft[]), cufftResult,
1313
(Ptr{cufftHandle}, Cint, Cint, cufftType),
1414
plan, nx, ny, type)
1515
end
1616

1717
function cufftPlan3d(plan, nx, ny, nz, type)
18-
@check @runtime_ccall((:cufftPlan3d, libcufft), cufftResult,
18+
@check @runtime_ccall((:cufftPlan3d, libcufft[]), cufftResult,
1919
(Ptr{cufftHandle}, Cint, Cint, Cint, cufftType),
2020
plan, nx, ny, nz, type)
2121
end
2222

2323
function cufftPlanMany(plan, rank, n, inembed, istride, idist, onembed, ostride, odist,
2424
type, batch)
25-
@check @runtime_ccall((:cufftPlanMany, libcufft), cufftResult,
25+
@check @runtime_ccall((:cufftPlanMany, libcufft[]), cufftResult,
2626
(Ptr{cufftHandle}, Cint, Ptr{Cint}, Ptr{Cint}, Cint, Cint, Ptr{Cint},
2727
Cint, Cint, cufftType, Cint),
2828
plan, rank, n, inembed, istride, idist, onembed, ostride, odist, type,
2929
batch)
3030
end
3131

3232
function cufftMakePlan1d(plan, nx, type, batch, workSize)
33-
@check @runtime_ccall((:cufftMakePlan1d, libcufft), cufftResult,
33+
@check @runtime_ccall((:cufftMakePlan1d, libcufft[]), cufftResult,
3434
(cufftHandle, Cint, cufftType, Cint, Ptr{Csize_t}),
3535
plan, nx, type, batch, workSize)
3636
end
3737

3838
function cufftMakePlan2d(plan, nx, ny, type, workSize)
39-
@check @runtime_ccall((:cufftMakePlan2d, libcufft), cufftResult,
39+
@check @runtime_ccall((:cufftMakePlan2d, libcufft[]), cufftResult,
4040
(cufftHandle, Cint, Cint, cufftType, Ptr{Csize_t}),
4141
plan, nx, ny, type, workSize)
4242
end
4343

4444
function cufftMakePlan3d(plan, nx, ny, nz, type, workSize)
45-
@check @runtime_ccall((:cufftMakePlan3d, libcufft), cufftResult,
45+
@check @runtime_ccall((:cufftMakePlan3d, libcufft[]), cufftResult,
4646
(cufftHandle, Cint, Cint, Cint, cufftType, Ptr{Csize_t}),
4747
plan, nx, ny, nz, type, workSize)
4848
end
4949

5050
function cufftMakePlanMany(plan, rank, n, inembed, istride, idist, onembed, ostride, odist,
5151
type, batch, workSize)
52-
@check @runtime_ccall((:cufftMakePlanMany, libcufft), cufftResult,
52+
@check @runtime_ccall((:cufftMakePlanMany, libcufft[]), cufftResult,
5353
(cufftHandle, Cint, Ptr{Cint}, Ptr{Cint}, Cint, Cint, Ptr{Cint}, Cint,
5454
Cint, cufftType, Cint, Ptr{Csize_t}),
5555
plan, rank, n, inembed, istride, idist, onembed, ostride, odist, type,
@@ -58,7 +58,7 @@ end
5858

5959
function cufftMakePlanMany64(plan, rank, n, inembed, istride, idist, onembed, ostride,
6060
odist, type, batch, workSize)
61-
@check @runtime_ccall((:cufftMakePlanMany64, libcufft), cufftResult,
61+
@check @runtime_ccall((:cufftMakePlanMany64, libcufft[]), cufftResult,
6262
(cufftHandle, Cint, Ptr{Clonglong}, Ptr{Clonglong}, Clonglong, Clonglong,
6363
Ptr{Clonglong}, Clonglong, Clonglong, cufftType, Clonglong, Ptr{Csize_t}),
6464
plan, rank, n, inembed, istride, idist, onembed, ostride, odist, type,
@@ -67,147 +67,147 @@ end
6767

6868
function cufftGetSizeMany64(plan, rank, n, inembed, istride, idist, onembed, ostride,
6969
odist, type, batch, workSize)
70-
@check @runtime_ccall((:cufftGetSizeMany64, libcufft), cufftResult,
70+
@check @runtime_ccall((:cufftGetSizeMany64, libcufft[]), cufftResult,
7171
(cufftHandle, Cint, Ptr{Clonglong}, Ptr{Clonglong}, Clonglong, Clonglong,
7272
Ptr{Clonglong}, Clonglong, Clonglong, cufftType, Clonglong, Ptr{Csize_t}),
7373
plan, rank, n, inembed, istride, idist, onembed, ostride, odist, type,
7474
batch, workSize)
7575
end
7676

7777
function cufftEstimate1d(nx, type, batch, workSize)
78-
@check @runtime_ccall((:cufftEstimate1d, libcufft), cufftResult,
78+
@check @runtime_ccall((:cufftEstimate1d, libcufft[]), cufftResult,
7979
(Cint, cufftType, Cint, Ptr{Csize_t}),
8080
nx, type, batch, workSize)
8181
end
8282

8383
function cufftEstimate2d(nx, ny, type, workSize)
84-
@check @runtime_ccall((:cufftEstimate2d, libcufft), cufftResult,
84+
@check @runtime_ccall((:cufftEstimate2d, libcufft[]), cufftResult,
8585
(Cint, Cint, cufftType, Ptr{Csize_t}),
8686
nx, ny, type, workSize)
8787
end
8888

8989
function cufftEstimate3d(nx, ny, nz, type, workSize)
90-
@check @runtime_ccall((:cufftEstimate3d, libcufft), cufftResult,
90+
@check @runtime_ccall((:cufftEstimate3d, libcufft[]), cufftResult,
9191
(Cint, Cint, Cint, cufftType, Ptr{Csize_t}),
9292
nx, ny, nz, type, workSize)
9393
end
9494

9595
function cufftEstimateMany(rank, n, inembed, istride, idist, onembed, ostride, odist, type,
9696
batch, workSize)
97-
@check @runtime_ccall((:cufftEstimateMany, libcufft), cufftResult,
97+
@check @runtime_ccall((:cufftEstimateMany, libcufft[]), cufftResult,
9898
(Cint, Ptr{Cint}, Ptr{Cint}, Cint, Cint, Ptr{Cint}, Cint, Cint,
9999
cufftType, Cint, Ptr{Csize_t}),
100100
rank, n, inembed, istride, idist, onembed, ostride, odist, type, batch,
101101
workSize)
102102
end
103103

104104
function cufftCreate(handle)
105-
@check @runtime_ccall((:cufftCreate, libcufft), cufftResult,
105+
@check @runtime_ccall((:cufftCreate, libcufft[]), cufftResult,
106106
(Ptr{cufftHandle},),
107107
handle)
108108
end
109109

110110
function cufftGetSize1d(handle, nx, type, batch, workSize)
111-
@check @runtime_ccall((:cufftGetSize1d, libcufft), cufftResult,
111+
@check @runtime_ccall((:cufftGetSize1d, libcufft[]), cufftResult,
112112
(cufftHandle, Cint, cufftType, Cint, Ptr{Csize_t}),
113113
handle, nx, type, batch, workSize)
114114
end
115115

116116
function cufftGetSize2d(handle, nx, ny, type, workSize)
117-
@check @runtime_ccall((:cufftGetSize2d, libcufft), cufftResult,
117+
@check @runtime_ccall((:cufftGetSize2d, libcufft[]), cufftResult,
118118
(cufftHandle, Cint, Cint, cufftType, Ptr{Csize_t}),
119119
handle, nx, ny, type, workSize)
120120
end
121121

122122
function cufftGetSize3d(handle, nx, ny, nz, type, workSize)
123-
@check @runtime_ccall((:cufftGetSize3d, libcufft), cufftResult,
123+
@check @runtime_ccall((:cufftGetSize3d, libcufft[]), cufftResult,
124124
(cufftHandle, Cint, Cint, Cint, cufftType, Ptr{Csize_t}),
125125
handle, nx, ny, nz, type, workSize)
126126
end
127127

128128
function cufftGetSizeMany(handle, rank, n, inembed, istride, idist, onembed, ostride,
129129
odist, type, batch, workArea)
130-
@check @runtime_ccall((:cufftGetSizeMany, libcufft), cufftResult,
130+
@check @runtime_ccall((:cufftGetSizeMany, libcufft[]), cufftResult,
131131
(cufftHandle, Cint, Ptr{Cint}, Ptr{Cint}, Cint, Cint, Ptr{Cint}, Cint,
132132
Cint, cufftType, Cint, Ptr{Csize_t}),
133133
handle, rank, n, inembed, istride, idist, onembed, ostride, odist, type,
134134
batch, workArea)
135135
end
136136

137137
function cufftGetSize(handle, workSize)
138-
@check @runtime_ccall((:cufftGetSize, libcufft), cufftResult,
138+
@check @runtime_ccall((:cufftGetSize, libcufft[]), cufftResult,
139139
(cufftHandle, Ptr{Csize_t}),
140140
handle, workSize)
141141
end
142142

143143
function cufftSetWorkArea(plan, workArea)
144-
@check @runtime_ccall((:cufftSetWorkArea, libcufft), cufftResult,
144+
@check @runtime_ccall((:cufftSetWorkArea, libcufft[]), cufftResult,
145145
(cufftHandle, CuPtr{Cvoid}),
146146
plan, workArea)
147147
end
148148

149149
function cufftSetAutoAllocation(plan, autoAllocate)
150-
@check @runtime_ccall((:cufftSetAutoAllocation, libcufft), cufftResult,
150+
@check @runtime_ccall((:cufftSetAutoAllocation, libcufft[]), cufftResult,
151151
(cufftHandle, Cint),
152152
plan, autoAllocate)
153153
end
154154

155155
function cufftExecC2C(plan, idata, odata, direction)
156-
@check @runtime_ccall((:cufftExecC2C, libcufft), cufftResult,
156+
@check @runtime_ccall((:cufftExecC2C, libcufft[]), cufftResult,
157157
(cufftHandle, CuPtr{cufftComplex}, CuPtr{cufftComplex}, Cint),
158158
plan, idata, odata, direction)
159159
end
160160

161161
function cufftExecR2C(plan, idata, odata)
162-
@check @runtime_ccall((:cufftExecR2C, libcufft), cufftResult,
162+
@check @runtime_ccall((:cufftExecR2C, libcufft[]), cufftResult,
163163
(cufftHandle, CuPtr{cufftReal}, CuPtr{cufftComplex}),
164164
plan, idata, odata)
165165
end
166166

167167
function cufftExecC2R(plan, idata, odata)
168-
@check @runtime_ccall((:cufftExecC2R, libcufft), cufftResult,
168+
@check @runtime_ccall((:cufftExecC2R, libcufft[]), cufftResult,
169169
(cufftHandle, CuPtr{cufftComplex}, CuPtr{cufftReal}),
170170
plan, idata, odata)
171171
end
172172

173173
function cufftExecZ2Z(plan, idata, odata, direction)
174-
@check @runtime_ccall((:cufftExecZ2Z, libcufft), cufftResult,
174+
@check @runtime_ccall((:cufftExecZ2Z, libcufft[]), cufftResult,
175175
(cufftHandle, CuPtr{cufftDoubleComplex}, CuPtr{cufftDoubleComplex}, Cint),
176176
plan, idata, odata, direction)
177177
end
178178

179179
function cufftExecD2Z(plan, idata, odata)
180-
@check @runtime_ccall((:cufftExecD2Z, libcufft), cufftResult,
180+
@check @runtime_ccall((:cufftExecD2Z, libcufft[]), cufftResult,
181181
(cufftHandle, CuPtr{cufftDoubleReal}, CuPtr{cufftDoubleComplex}),
182182
plan, idata, odata)
183183
end
184184

185185
function cufftExecZ2D(plan, idata, odata)
186-
@check @runtime_ccall((:cufftExecZ2D, libcufft), cufftResult,
186+
@check @runtime_ccall((:cufftExecZ2D, libcufft[]), cufftResult,
187187
(cufftHandle, CuPtr{cufftDoubleComplex}, CuPtr{cufftDoubleReal}),
188188
plan, idata, odata)
189189
end
190190

191191
function cufftSetStream(plan, stream)
192-
@check @runtime_ccall((:cufftSetStream, libcufft), cufftResult,
192+
@check @runtime_ccall((:cufftSetStream, libcufft[]), cufftResult,
193193
(cufftHandle, CUstream),
194194
plan, stream)
195195
end
196196

197197
function cufftDestroy(plan)
198-
@check @runtime_ccall((:cufftDestroy, libcufft), cufftResult,
198+
@check @runtime_ccall((:cufftDestroy, libcufft[]), cufftResult,
199199
(cufftHandle,),
200200
plan)
201201
end
202202

203203
function cufftGetVersion(version)
204-
@check @runtime_ccall((:cufftGetVersion, libcufft), cufftResult,
204+
@check @runtime_ccall((:cufftGetVersion, libcufft[]), cufftResult,
205205
(Ptr{Cint},),
206206
version)
207207
end
208208

209209
function cufftGetProperty(type, value)
210-
@check @runtime_ccall((:cufftGetProperty, libcufft), cufftResult,
210+
@check @runtime_ccall((:cufftGetProperty, libcufft[]), cufftResult,
211211
(libraryPropertyType, Ptr{Cint}),
212212
type, value)
213213
end

src/rand/CURAND.jl

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,7 @@ import CUDAnative
1212

1313
using CEnum
1414

15-
const libcurand = if Sys.iswindows()
16-
# no ccall by soname, we need the filename
17-
# NOTE: we discover the full path here, while only the wordsize and toolkit versions
18-
# would have been enough to construct "curand64_10.dll"
19-
toolkit = find_toolkit()
20-
path = find_cuda_library("curand", toolkit)
21-
if path === nothing
22-
error("Could not find libcurand")
23-
end
24-
basename(path)
25-
else
26-
# ccall by soname; CuArrays.__init__ will have populated Libdl.DL_LOAD_PATH
27-
"libcurand"
28-
end
15+
const libcurand = Ref("libcurand")
2916

3017
# core library
3118
include("libcurand_common.jl")

0 commit comments

Comments
 (0)