Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFTW planner is not thread safe. #66

Closed
KristofferC opened this issue Apr 6, 2018 · 2 comments · Fixed by #105
Closed

FFTW planner is not thread safe. #66

KristofferC opened this issue Apr 6, 2018 · 2 comments · Fixed by #105

Comments

@KristofferC
Copy link
Member

Moved from JuliaLang/julia#17972


Having fft/bfft/ifft/etc. calls in a threaded region exposes the fact that the planning portion of the fft call is not thread-safe in fftw. Below is a potential patch to dft.jl if you just want to lock the planner.

global planner_mutex = nothing

function __init__()
    global planner_mutex = Base.Threads.Mutex()
end

# implementations only need to provide plan_X(x, region)
# for X in (:fft, :bfft, ...):
for f in (:fft, :bfft, :ifft, :fft!, :bfft!, :ifft!, :rfft)
    pf = Symbol("plan_", f)
    @eval begin
        function $f(x::AbstractArray)
            Base.Threads.lock!(planner_mutex)
            the_plan = $pf(x)
            Base.Threads.unlock!(planner_mutex)
            the_plan * x
        end
        function $f(x::AbstractArray, region)
            Base.Threads.lock!(planner_mutex)
            the_plan = $pf(x, region)
            Base.Threads.unlock!(planner_mutex)
            the_plan * x
        end
        function $pf(x::AbstractArray; kws...)
            Base.Threads.lock!(planner_mutex)
            the_plan = $pf(x, 1:ndims(x); kws...)
            Base.Threads.unlock!(planner_mutex)
            the_plan
        end
    end
end
@stevengj
Copy link
Member

stevengj commented Apr 6, 2018

You could call fftw_make_planner_thread_safe instead, although it basically does the same thing.

If you are implementing your own locks, note that you also need to put a lock around the fftw_destroy_plan call.

@dlfivefifty
Copy link
Member

This would be nice to have for ApproxFun, as I think it is the one thing used that is not thread-safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants