Skip to content

Commit e5e4631

Browse files
authored
Merge pull request #58 from JuliaMath/sf/ilp64_accelerate
Use LBT to forward BLAS and LAPACK calls to Accelerate
2 parents 67cab4f + e3753ce commit e5e4631

File tree

5 files changed

+395
-239
lines changed

5 files changed

+395
-239
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ jobs:
1616
- '1'
1717
- 'nightly'
1818
os:
19+
- macOS-13
1920
- ubuntu-latest
20-
- macOS-12
21-
# - windows-latest
21+
- windows-latest
2222
arch:
2323
- x64
2424
steps:

Project.toml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
name = "AppleAccelerate"
22
uuid = "13e28ba4-7ad8-5781-acae-3021b1ed3924"
3-
version = "0.3.3"
3+
version = "0.4.0"
44

55
[deps]
6+
LAPACK32_jll = "17f450c3-bd24-55df-bb84-8c51b4b939e3"
7+
LAPACK_jll = "51474c39-65e3-53ba-86ba-03b1b862ec14"
68
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
9+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
710

811
[compat]
9-
julia = "1.6"
12+
julia = "1.9"
13+
LAPACK32_jll = "3.11"
14+
LAPACK_jll = "3.11"
1015

1116
[extras]
12-
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
13-
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
17+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
18+
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
19+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
20+
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
1421
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
22+
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
23+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
24+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
25+
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
1526
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1627
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1728

1829
[targets]
19-
test = ["DSP", "Statistics", "LinearAlgebra", "Test", "Random"]
30+
test = ["Dates", "Distributed", "Printf", "Random", "REPL", "Sockets", "SparseArrays", "SpecialFunctions", "DSP", "Statistics", "Test"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ functions, though there does appear to be some reduced accuracy.
1010

1111
## OS Requirements
1212

13-
MacOS 12 is required in order to run AppleAccelerate.jl. On older MacOS versions, your mileage may vary.
13+
MacOS 13.3 is required in order to run AppleAccelerate.jl. On older MacOS versions, your mileage may vary.
1414

1515
## Supported Functions
1616

src/AppleAccelerate.jl

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,100 @@
11
module AppleAccelerate
22

3-
using Libdl
3+
using LinearAlgebra
4+
using Libdl, LAPACK_jll, LAPACK32_jll
45

5-
if Sys.isapple()
6+
# For now, only use BLAS from Accelerate (that is to say, vecLib)
7+
global const libacc = "/System/Library/Frameworks/Accelerate.framework/Accelerate"
8+
global const libacc_info_plist = "/System/Library/Frameworks/Accelerate.framework/Versions/Current/Resources/Info.plist"
9+
10+
function forward_accelerate(interface::Symbol;
11+
new_lapack::Bool = interface == :ilp64,
12+
clear::Bool = false,
13+
verbose::Bool = false)
14+
kwargs = Dict{Symbol,String}()
15+
if new_lapack
16+
if interface == :ilp64
17+
kwargs[:suffix_hint] = "\x1a\$NEWLAPACK\$ILP64"
18+
else
19+
kwargs[:suffix_hint] = "\x1a\$NEWLAPACK"
20+
end
21+
else
22+
if interface == :ilp64
23+
throw(ArgumentError("ILP64 accelerate requires new_lapack"))
24+
end
25+
end
26+
BLAS.lbt_forward(libacc; clear, verbose, kwargs...)
27+
end
28+
29+
"""
30+
load_accelerate(;clear = true, verbose = false)
31+
32+
Load Accelerate, replacing the current LBT forwarding tables if `clear` is `true`.
33+
Attempts to load the ILP64 symbols if `load_ilp64` is `true`, and errors out if unable.
34+
"""
35+
function load_accelerate(;clear::Bool = true, verbose::Bool = false, load_ilp64::Bool = true, use_external_lapack::Bool = true)
36+
# Silently exit on non-Accelerate-capable platforms
37+
@static if !Sys.isapple()
38+
return
39+
end
40+
libacc_hdl = dlopen_e(libacc)
41+
if libacc_hdl == C_NULL
42+
return
43+
end
44+
45+
# Check to see if we can load ILP64 symbols
46+
if load_ilp64 && dlsym_e(libacc_hdl, "dgemm\$NEWLAPACK\$ILP64") == C_NULL
47+
error("Unable to load ILP64 interface from '$(libacc)'; You are running macOS version $(get_macos_version()), you need v13.3+")
48+
end
49+
50+
# First, load :lp64 symbols, optionally clearing the current LBT forwarding tables
51+
forward_accelerate(:lp64; new_lapack=true, clear, verbose)
52+
if load_ilp64
53+
forward_accelerate(:ilp64; new_lapack=true, verbose)
54+
end
655

7-
try
8-
global const libacc = "/System/Library/Frameworks/Accelerate.framework/Accelerate"
9-
let accel_lib = dlopen(libacc)
10-
dlclose(accel_lib)
56+
# Next, load an external LAPACK, if requested
57+
if use_external_lapack
58+
if load_ilp64
59+
BLAS.lbt_forward(LAPACK_jll.liblapack_path; suffix_hint="64_", verbose)
1160
end
12-
catch
13-
error("Accelerate framework not found.")
61+
BLAS.lbt_forward(LAPACK32_jll.liblapack32_path; verbose)
62+
end
63+
end
64+
65+
function get_macos_version()
66+
@static if !Sys.isapple()
67+
return nothing
1468
end
1569

70+
plist_lines = split(String(read("/System/Library/CoreServices/SystemVersion.plist")), "\n")
71+
vers_idx = findfirst(l -> occursin("ProductVersion", l), plist_lines)
72+
if vers_idx === nothing
73+
return nothing
74+
end
75+
76+
m = match(r">([\d\.]+)<", plist_lines[vers_idx+1])
77+
if m === nothing
78+
return nothing
79+
end
1680

81+
return VersionNumber(only(m.captures))
82+
end
83+
84+
function __init__()
85+
# Default to loading the ILP64 interface on macOS 13.3+
86+
ver = get_macos_version()
87+
ver == nothing && return
88+
load_ilp64 = ver >= v"13.3"
89+
# dsptrf has a bug in the initial release of the $NEWLAPACK symbols, so if we're
90+
# on a version older than macOS 13.4, use an external LAPACK:
91+
load_accelerate(; load_ilp64, use_external_lapack = ver < v"13.4")
92+
end
93+
94+
if Sys.isapple()
1795
include("Array.jl")
1896
include("DSP.jl")
1997
include("Util.jl")
20-
2198
end
2299

23100
end # module

0 commit comments

Comments
 (0)