You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multithreading problem caused by FFTW dependency #80
tl;dr if you use many threads, running FFTW.set_num_threads(1) can be a good idea. Otherwise FFTW probably slows down computation and prevents outer parallelism. I suggest adding it to the README.
Full explanation
I was trying to do a lot of KDE in the loop, but it occurred that running the code in parallel slow down the process. Even if I simply set JULIA_NUM_THREADS=20 (for 56 core server) without using @threads:
using KernelDensity
using Base.Threads
interp_kde(coords::Array{Float64, 2}, bandwidth::Float64) =InterpKDE(kde((coords[1,:], coords[2,:]), bandwidth=(bandwidth, bandwidth)))
td =rand(2, 100000);
@timefor i in1:500interp_kde(td, 1.0)
end
It creates multiple threads with loading 30% and takes 15.9 seconds. The same code with JULIA_NUM_THREADS=1 takes 7.5 seconds, working fairly in single thread. Timing doesn't really change if I use `@threads:
@time@threadsfor i in1:500interp_kde(td, 1.0)
end
After some digging, the problem occurred to be in the FFTW package, which is called somewhere during interpolation and by default usesnthreads() * 4 threads inside its C code. To disable it you need to run FFTW.set_num_threads(1). After that, running with JULIA_NUM_THREADS=20 but without @threads takes 7.5 seconds, as it should be, and with @threads it takes 0.5 seconds.
I was trying different run configurations, but at the end, looks like having FFTW parallel improves situation comparing to single thread only with large arrays (>500000) and large number of iterations (>100) And it's always much worse than having outer loop parallel.
(However, if you want to ensure that no one has changed this, you can pass num_threads=1 to the plan_fft routines — this is better than setting the number of FFTW threads globally, which will affect other programs.)
Also, if you are repeatedly doing FFTs in a tight loop, you should really create the FFT plans once and re-use them. Right now your API doesn't provide a way to do this if you are calling kde(...) over and over with different data of the same length, as in the example above — this would be a big speedup independent of threading. (And it will really help threading because plan creation is serialized, but plan execution is not.) You might consider adding a lower-level API that constructs a re-usable KDEBuilder object to construct a kde of a given length, which saves the rfft/irfft plans.
(Another suggestion: use brfft instead of irfft and do the 1/n scaling inside your pointwise multiplication loop, to save a separate scaling pass over the array.)
Activity
andreasnoack commentedon Feb 20, 2020
Which version of Julia and the package are you using?
VPetukhov commentedon Feb 20, 2020
Julia 1.3.1, FFTW v1.2.0, KernelDensity v0.5.1
andreasnoack commentedon Feb 20, 2020
@stevengj Any thoughts on this?
andreasnoack commentedon Aug 12, 2025
Related to #123
stevengj commentedon Aug 12, 2025
Recent versions of FFTW.jl default to 1 thread.
(However, if you want to ensure that no one has changed this, you can pass
num_threads=1to theplan_fftroutines — this is better than setting the number of FFTW threads globally, which will affect other programs.)Also, if you are repeatedly doing FFTs in a tight loop, you should really create the FFT plans once and re-use them. Right now your API doesn't provide a way to do this if you are calling
kde(...)over and over with different data of the same length, as in the example above — this would be a big speedup independent of threading. (And it will really help threading because plan creation is serialized, but plan execution is not.) You might consider adding a lower-level API that constructs a re-usableKDEBuilderobject to construct a kde of a given length, which saves therfft/irfftplans.(Another suggestion: use
brfftinstead ofirfftand do the1/nscaling inside your pointwise multiplication loop, to save a separate scaling pass over the array.)devmotion commentedon Jan 20, 2026
In the latest release we switched from FFTW to FFTA. On my computer the example above gives