Skip to content

Multithreading problem caused by FFTW dependency #80

Closed

Description

@VPetukhov

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);
@time for i in 1:500
    interp_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 @threads for i in 1:500
    interp_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 uses nthreads() * 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.

Activity

  1. andreasnoack commented on Feb 20, 2020

    @andreasnoack
    Member

    Which version of Julia and the package are you using?

  2. VPetukhov commented on Feb 20, 2020

    @VPetukhov
    Author

    Julia 1.3.1, FFTW v1.2.0, KernelDensity v0.5.1

  3. andreasnoack commented on Feb 20, 2020

    @andreasnoack
    Member

    @stevengj Any thoughts on this?

  4. changed the issue type fromtoon Aug 12, 2025
  5. andreasnoack commented on Aug 12, 2025

    @andreasnoack
    Member

    Related to #123

  6. stevengj commented on Aug 12, 2025

    @stevengj

    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=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.)

  7. devmotion commented on Jan 20, 2026

    @devmotion
    Member

    In the latest release we switched from FFTW to FFTA. On my computer the example above gives

    julia> using KernelDensity
    
    julia> using Base.Threads
    
    julia> interp_kde(coords::Array{Float64, 2}, bandwidth::Float64) =
               InterpKDE(kde((coords[1,:], coords[2,:]), bandwidth=(bandwidth, bandwidth)))
    interp_kde (generic function with 1 method)
    
    julia> td = rand(2, 100000);
    
    julia> @time for i in 1:500
               interp_kde(td, 1.0)
           end
      3.240257 seconds (5.40 M allocations: 3.045 GiB, 8.36% gc time, 98.95% compilation time)
    
    julia> @time @threads for i in 1:500
               interp_kde(td, 1.0)
           end
      0.688493 seconds (527.77 k allocations: 2.807 GiB, 13.66% gc time, 31 lock conflicts, 45.55% compilation time)
    
    julia> nthreads()
    6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions