Skip to content
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
10 changes: 9 additions & 1 deletion src/FFTW.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ function __init__()
Base.depwarn("JULIA_FFTW_PROVIDER is deprecated; use FFTW.set_provider!() instead", :JULIA_FFTW_PROVIDER)
end

# Hook FFTW threads up to our partr runtime
# Hook FFTW threads up to our partr runtime, and re-assign the
# libfftw3{,f} refs at runtime, since we may have relocated and
# changed the path to the library since the last time we precompiled.
@static if fftw_provider == "fftw"
fftw_init_threads()
libfftw3[] = FFTW_jll.libfftw3_path
libfftw3f[] = FFTW_jll.libfftw3f_path
end
@static if fftw_provider == "mkl"
libfftw3[] = MKL_jll.libmkl_rt_path
libfftw3f[] = MKL_jll.libmkl_rt_path
end
end

Expand Down
76 changes: 38 additions & 38 deletions src/fft.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function plan_r2r end
## FFT: Implement fft by calling fftw.

const version = VersionNumber(split(unsafe_string(cglobal(
(:fftw_version,libfftw3), UInt8)), ['-', ' '])[2])
(:fftw_version,libfftw3[]), UInt8)), ['-', ' '])[2])

## Direction of FFT

Expand Down Expand Up @@ -141,39 +141,39 @@ alignment_of(A::FakeArray) = Int32(0)
@exclusive function export_wisdom(fname::AbstractString)
f = ccall(:fopen, Ptr{Cvoid}, (Cstring,Cstring), fname, :w)
systemerror("could not open wisdom file $fname for writing", f == C_NULL)
ccall((:fftw_export_wisdom_to_file,libfftw3), Cvoid, (Ptr{Cvoid},), f)
ccall((:fftw_export_wisdom_to_file,libfftw3[]), Cvoid, (Ptr{Cvoid},), f)
ccall(:fputs, Int32, (Ptr{UInt8},Ptr{Cvoid}), " "^256, f) # no NUL, hence no Cstring
ccall((:fftwf_export_wisdom_to_file,libfftw3f), Cvoid, (Ptr{Cvoid},), f)
ccall((:fftwf_export_wisdom_to_file,libfftw3f[]), Cvoid, (Ptr{Cvoid},), f)
ccall(:fclose, Cvoid, (Ptr{Cvoid},), f)
end

@exclusive function import_wisdom(fname::AbstractString)
f = ccall(:fopen, Ptr{Cvoid}, (Cstring,Cstring), fname, :r)
systemerror("could not open wisdom file $fname for reading", f == C_NULL)
if ccall((:fftw_import_wisdom_from_file,libfftw3),Int32,(Ptr{Cvoid},),f)==0||
ccall((:fftwf_import_wisdom_from_file,libfftw3f),Int32,(Ptr{Cvoid},),f)==0
if ccall((:fftw_import_wisdom_from_file,libfftw3[]),Int32,(Ptr{Cvoid},),f)==0||
ccall((:fftwf_import_wisdom_from_file,libfftw3f[]),Int32,(Ptr{Cvoid},),f)==0
error("failed to import wisdom from $fname")
end
ccall(:fclose, Cvoid, (Ptr{Cvoid},), f)
end

@exclusive function import_system_wisdom()
if ccall((:fftw_import_system_wisdom,libfftw3), Int32, ()) == 0 ||
ccall((:fftwf_import_system_wisdom,libfftw3f), Int32, ()) == 0
if ccall((:fftw_import_system_wisdom,libfftw3[]), Int32, ()) == 0 ||
ccall((:fftwf_import_system_wisdom,libfftw3f[]), Int32, ()) == 0
error("failed to import system wisdom")
end
end

@exclusive function forget_wisdom()
ccall((:fftw_forget_wisdom,libfftw3), Cvoid, ())
ccall((:fftwf_forget_wisdom,libfftw3f), Cvoid, ())
ccall((:fftw_forget_wisdom,libfftw3[]), Cvoid, ())
ccall((:fftwf_forget_wisdom,libfftw3f[]), Cvoid, ())
end

# Threads

@exclusive function set_num_threads(nthreads::Integer)
ccall((:fftw_plan_with_nthreads,libfftw3), Cvoid, (Int32,), nthreads)
ccall((:fftwf_plan_with_nthreads,libfftw3f), Cvoid, (Int32,), nthreads)
ccall((:fftw_plan_with_nthreads,libfftw3[]), Cvoid, (Int32,), nthreads)
ccall((:fftwf_plan_with_nthreads,libfftw3f[]), Cvoid, (Int32,), nthreads)
end

# pointer type for fftw_plan (opaque pointer)
Expand All @@ -187,9 +187,9 @@ const NO_TIMELIMIT = -1.0 # from fftw3.h

# only call these when fftwlock is held:
unsafe_set_timelimit(precision::fftwTypeDouble,seconds) =
ccall((:fftw_set_timelimit,libfftw3), Cvoid, (Float64,), seconds)
ccall((:fftw_set_timelimit,libfftw3[]), Cvoid, (Float64,), seconds)
unsafe_set_timelimit(precision::fftwTypeSingle,seconds) =
ccall((:fftwf_set_timelimit,libfftw3f), Cvoid, (Float64,), seconds)
ccall((:fftwf_set_timelimit,libfftw3f[]), Cvoid, (Float64,), seconds)
@exclusive set_timelimit(precision, seconds) = unsafe_set_timelimit(precision, seconds)

# Array alignment mod 16:
Expand All @@ -210,9 +210,9 @@ unsafe_set_timelimit(precision::fftwTypeSingle,seconds) =
convert(Int32, convert(Int64, pointer(A)) % 16)
else
alignment_of(A::StridedArray{T}) where {T<:fftwDouble} =
ccall((:fftw_alignment_of, libfftw3), Int32, (Ptr{T},), A)
ccall((:fftw_alignment_of, libfftw3[]), Int32, (Ptr{T},), A)
alignment_of(A::StridedArray{T}) where {T<:fftwSingle} =
ccall((:fftwf_alignment_of, libfftw3f), Int32, (Ptr{T},), A)
ccall((:fftwf_alignment_of, libfftw3f[]), Int32, (Ptr{T},), A)
end

# FFTWPlan (low-level)
Expand Down Expand Up @@ -268,9 +268,9 @@ unsafe_convert(::Type{PlanPtr}, p::FFTWPlan) = p.plan

# these functions should only be called while the fftwlock is held
unsafe_destroy_plan(plan::FFTWPlan{<:fftwDouble}) =
ccall((:fftw_destroy_plan,libfftw3), Cvoid, (PlanPtr,), plan)
ccall((:fftw_destroy_plan,libfftw3[]), Cvoid, (PlanPtr,), plan)
unsafe_destroy_plan(plan::FFTWPlan{<:fftwSingle}) =
ccall((:fftwf_destroy_plan,libfftw3f), Cvoid, (PlanPtr,), plan)
ccall((:fftwf_destroy_plan,libfftw3f[]), Cvoid, (PlanPtr,), plan)

const deferred_destroy_lock = ReentrantLock() # lock protecting the deferred_destroy_plans list
const deferred_destroy_plans = FFTWPlan[]
Expand Down Expand Up @@ -331,19 +331,19 @@ end
#################################################################################################

cost(plan::FFTWPlan{<:fftwDouble}) =
ccall((:fftw_cost,libfftw3), Float64, (PlanPtr,), plan)
ccall((:fftw_cost,libfftw3[]), Float64, (PlanPtr,), plan)
cost(plan::FFTWPlan{<:fftwSingle}) =
ccall((:fftwf_cost,libfftw3f), Float64, (PlanPtr,), plan)
ccall((:fftwf_cost,libfftw3f[]), Float64, (PlanPtr,), plan)

@exclusive function arithmetic_ops(plan::FFTWPlan{<:fftwDouble})
add, mul, fma = Ref(0.0), Ref(0.0), Ref(0.0)
ccall((:fftw_flops,libfftw3), Cvoid,
ccall((:fftw_flops,libfftw3[]), Cvoid,
(PlanPtr,Ref{Float64},Ref{Float64},Ref{Float64}), plan, add, mul, fma)
return (round(Int64, add[]), round(Int64, mul[]), round(Int64, fma[]))
end
@exclusive function arithmetic_ops(plan::FFTWPlan{<:fftwSingle})
add, mul, fma = Ref(0.0), Ref(0.0), Ref(0.0)
ccall((:fftwf_flops,libfftw3f), Cvoid,
ccall((:fftwf_flops,libfftw3f[]), Cvoid,
(PlanPtr,Ref{Float64},Ref{Float64},Ref{Float64}), plan, add, mul, fma)
return (round(Int64, add[]), round(Int64, mul[]), round(Int64, fma[]))
end
Expand Down Expand Up @@ -374,9 +374,9 @@ const has_sprint_plan = version >= v"3.3.4" && fftw_provider == "fftw"

@static if has_sprint_plan
sprint_plan_(plan::FFTWPlan{<:fftwDouble}) =
ccall((:fftw_sprint_plan,libfftw3), Ptr{UInt8}, (PlanPtr,), plan)
ccall((:fftw_sprint_plan,libfftw3[]), Ptr{UInt8}, (PlanPtr,), plan)
sprint_plan_(plan::FFTWPlan{<:fftwSingle}) =
ccall((:fftwf_sprint_plan,libfftw3f), Ptr{UInt8}, (PlanPtr,), plan)
ccall((:fftwf_sprint_plan,libfftw3f[]), Ptr{UInt8}, (PlanPtr,), plan)
function sprint_plan(plan::FFTWPlan)
p = sprint_plan_(plan)
str = unsafe_string(p)
Expand Down Expand Up @@ -457,49 +457,49 @@ _colmajorstrides(p) = ()
# Execute

unsafe_execute!(plan::FFTWPlan{<:fftwDouble}) =
ccall((:fftw_execute,libfftw3), Cvoid, (PlanPtr,), plan)
ccall((:fftw_execute,libfftw3[]), Cvoid, (PlanPtr,), plan)

unsafe_execute!(plan::FFTWPlan{<:fftwSingle}) =
ccall((:fftwf_execute,libfftw3f), Cvoid, (PlanPtr,), plan)
ccall((:fftwf_execute,libfftw3f[]), Cvoid, (PlanPtr,), plan)

unsafe_execute!(plan::cFFTWPlan{T},
X::StridedArray{T}, Y::StridedArray{T}) where {T<:fftwDouble} =
ccall((:fftw_execute_dft,libfftw3), Cvoid,
ccall((:fftw_execute_dft,libfftw3[]), Cvoid,
(PlanPtr,Ptr{T},Ptr{T}), plan, X, Y)

unsafe_execute!(plan::cFFTWPlan{T},
X::StridedArray{T}, Y::StridedArray{T}) where {T<:fftwSingle} =
ccall((:fftwf_execute_dft,libfftw3f), Cvoid,
ccall((:fftwf_execute_dft,libfftw3f[]), Cvoid,
(PlanPtr,Ptr{T},Ptr{T}), plan, X, Y)

unsafe_execute!(plan::rFFTWPlan{Float64,FORWARD},
X::StridedArray{Float64}, Y::StridedArray{Complex{Float64}}) =
ccall((:fftw_execute_dft_r2c,libfftw3), Cvoid,
ccall((:fftw_execute_dft_r2c,libfftw3[]), Cvoid,
(PlanPtr,Ptr{Float64},Ptr{Complex{Float64}}), plan, X, Y)

unsafe_execute!(plan::rFFTWPlan{Float32,FORWARD},
X::StridedArray{Float32}, Y::StridedArray{Complex{Float32}}) =
ccall((:fftwf_execute_dft_r2c,libfftw3f), Cvoid,
ccall((:fftwf_execute_dft_r2c,libfftw3f[]), Cvoid,
(PlanPtr,Ptr{Float32},Ptr{Complex{Float32}}), plan, X, Y)

unsafe_execute!(plan::rFFTWPlan{Complex{Float64},BACKWARD},
X::StridedArray{Complex{Float64}}, Y::StridedArray{Float64}) =
ccall((:fftw_execute_dft_c2r,libfftw3), Cvoid,
ccall((:fftw_execute_dft_c2r,libfftw3[]), Cvoid,
(PlanPtr,Ptr{Complex{Float64}},Ptr{Float64}), plan, X, Y)

unsafe_execute!(plan::rFFTWPlan{Complex{Float32},BACKWARD},
X::StridedArray{Complex{Float32}}, Y::StridedArray{Float32}) =
ccall((:fftwf_execute_dft_c2r,libfftw3f), Cvoid,
ccall((:fftwf_execute_dft_c2r,libfftw3f[]), Cvoid,
(PlanPtr,Ptr{Complex{Float32}},Ptr{Float32}), plan, X, Y)

unsafe_execute!(plan::r2rFFTWPlan{T},
X::StridedArray{T}, Y::StridedArray{T}) where {T<:fftwDouble} =
ccall((:fftw_execute_r2r,libfftw3), Cvoid,
ccall((:fftw_execute_r2r,libfftw3[]), Cvoid,
(PlanPtr,Ptr{T},Ptr{T}), plan, X, Y)

unsafe_execute!(plan::r2rFFTWPlan{T},
X::StridedArray{T}, Y::StridedArray{T}) where {T<:fftwSingle} =
ccall((:fftwf_execute_r2r,libfftw3f), Cvoid,
ccall((:fftwf_execute_r2r,libfftw3f[]), Cvoid,
(PlanPtr,Ptr{T},Ptr{T}), plan, X, Y)

# NOTE ON GC (garbage collection):
Expand Down Expand Up @@ -565,7 +565,7 @@ for (Tr,Tc,fftw,lib) in ((:Float64,:(Complex{Float64}),"fftw",:libfftw3),
unsafe_set_timelimit($Tr, timelimit)
R = isa(region, Tuple) ? region : copy(region)
dims, howmany = dims_howmany(X, Y, [size(X)...], R)
plan = ccall(($(string(fftw,"_plan_guru64_dft")),$lib),
plan = ccall(($(string(fftw,"_plan_guru64_dft")),$lib[]),
PlanPtr,
(Int32, Ptr{Int}, Int32, Ptr{Int},
Ptr{$Tc}, Ptr{$Tc}, Int32, UInt32),
Expand All @@ -585,7 +585,7 @@ for (Tr,Tc,fftw,lib) in ((:Float64,:(Complex{Float64}),"fftw",:libfftw3),
region = circshift(Int[region...],-1) # FFTW halves last dim
unsafe_set_timelimit($Tr, timelimit)
dims, howmany = dims_howmany(X, Y, [size(X)...], region)
plan = ccall(($(string(fftw,"_plan_guru64_dft_r2c")),$lib),
plan = ccall(($(string(fftw,"_plan_guru64_dft_r2c")),$lib[]),
PlanPtr,
(Int32, Ptr{Int}, Int32, Ptr{Int},
Ptr{$Tr}, Ptr{$Tc}, UInt32),
Expand All @@ -605,7 +605,7 @@ for (Tr,Tc,fftw,lib) in ((:Float64,:(Complex{Float64}),"fftw",:libfftw3),
region = circshift(Int[region...],-1) # FFTW halves last dim
unsafe_set_timelimit($Tr, timelimit)
dims, howmany = dims_howmany(X, Y, [size(Y)...], region)
plan = ccall(($(string(fftw,"_plan_guru64_dft_c2r")),$lib),
plan = ccall(($(string(fftw,"_plan_guru64_dft_c2r")),$lib[]),
PlanPtr,
(Int32, Ptr{Int}, Int32, Ptr{Int},
Ptr{$Tc}, Ptr{$Tr}, UInt32),
Expand All @@ -626,7 +626,7 @@ for (Tr,Tc,fftw,lib) in ((:Float64,:(Complex{Float64}),"fftw",:libfftw3),
knd = fix_kinds(region, kinds)
unsafe_set_timelimit($Tr, timelimit)
dims, howmany = dims_howmany(X, Y, [size(X)...], region)
plan = ccall(($(string(fftw,"_plan_guru64_r2r")),$lib),
plan = ccall(($(string(fftw,"_plan_guru64_r2r")),$lib[]),
PlanPtr,
(Int32, Ptr{Int}, Int32, Ptr{Int},
Ptr{$Tr}, Ptr{$Tr}, Ptr{Int32}, UInt32),
Expand All @@ -651,7 +651,7 @@ for (Tr,Tc,fftw,lib) in ((:Float64,:(Complex{Float64}),"fftw",:libfftw3),
dims[2:3, 1:size(dims,2)] *= 2
howmany[2:3, 1:size(howmany,2)] *= 2
howmany = [howmany [2,1,1]] # append loop over real/imag parts
plan = ccall(($(string(fftw,"_plan_guru64_r2r")),$lib),
plan = ccall(($(string(fftw,"_plan_guru64_r2r")),$lib[]),
PlanPtr,
(Int32, Ptr{Int}, Int32, Ptr{Int},
Ptr{$Tc}, Ptr{$Tc}, Ptr{Int32}, UInt32),
Expand Down
24 changes: 16 additions & 8 deletions src/providers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ end
# Read in preferences, see if any users have requested a particular backend
const fftw_provider = get_provider()

# We'll initialize `libfftw3` here (in the conditionals below), and
# it will get overwritten again in `__init__()`. This allows us to
# `ccall` at build time, and also be relocatable for PackageCompiler.
const libfftw3 = Ref{String}()
const libfftw3f = Ref{String}()

"""
set_provider!(provider; export_prefs::Bool = false)

Expand All @@ -41,7 +47,9 @@ end

# If we're using fftw_jll, load it in
@static if fftw_provider == "fftw"
using FFTW_jll
import FFTW_jll
libfftw3[] = FFTW_jll.libfftw3_path
libfftw3f[] = FFTW_jll.libfftw3f_path

# callback function that FFTW uses to launch `num` parallel
# tasks (FFTW/fftw3#175):
Expand All @@ -58,23 +66,23 @@ end
# (Previously, we called fftw_cleanup, but this invalidated existing
# plans, causing Base Julia issue #19892.)
function fftw_init_threads()
stat = ccall((:fftw_init_threads, libfftw3), Int32, ())
statf = ccall((:fftwf_init_threads, libfftw3f), Int32, ())
stat = ccall((:fftw_init_threads, libfftw3[]), Int32, ())
statf = ccall((:fftwf_init_threads, libfftw3f[]), Int32, ())
if stat == 0 || statf == 0
error("could not initialize FFTW threads")
end

if nthreads() > 1
cspawnloop = @cfunction(spawnloop, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t, Cint, Ptr{Cvoid}))
ccall((:fftw_threads_set_callback, libfftw3), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), cspawnloop, C_NULL)
ccall((:fftwf_threads_set_callback, libfftw3f), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), cspawnloop, C_NULL)
ccall((:fftw_threads_set_callback, libfftw3[]), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), cspawnloop, C_NULL)
ccall((:fftwf_threads_set_callback, libfftw3f[]), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), cspawnloop, C_NULL)
end
end
end

# If we're using MKL, load it in and set library paths appropriately.
@static if fftw_provider == "mkl"
using MKL_jll
const libfftw3 = MKL_jll.libmkl_rt_path
const libfftw3f = libfftw3
import MKL_jll
libfftw3[] = MKL_jll.libmkl_rt_path
libfftw3f[] = MKL_jll.libmkl_rt_path
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ end # fftw_provider == "fftw"
end

# check whether FFTW on this architecture has nontrivial alignment requirements
nontrivial_alignment = FFTW.fftw_provider == "fftw" && ccall((:fftwf_alignment_of, FFTW.libfftw3f), Int32, (Int,), 8) != 0
nontrivial_alignment = FFTW.fftw_provider == "fftw" && ccall((:fftwf_alignment_of, FFTW.libfftw3f[]), Int32, (Int,), 8) != 0
if nontrivial_alignment
@test_throws ArgumentError plan_rfft(Array{Float32}(undef, 32)) * view(A, 2:33)
@test_throws ArgumentError plan_fft(Array{Complex{Float32}}(undef, 32)) * view(Ac, 2:33)
Expand Down