JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.63k stars 5.48k forks source link

Access Violation with multithreading and FFTW #50441

Open judober opened 1 year ago

judober commented 1 year ago

The code (fairly reduced)

using Pkg
Pkg.activate(".")
using LinearAlgebra
using FFTW

function fun1()
    cache = FFT_Cache(830) # simplified

    for no in 1:13267
        # @show no
        execude_fft(cache)
        # do stuff here, not required for error
    end

    return nothing
end

struct FFT_Cache{FFTplanT}
    FFTVals1::Vector{ComplexF64}
    FFTVals2::Vector{ComplexF64}
    iFFTplan::FFTplanT
end

function FFT_Cache(size_FFT)
    FFTVals1 = Vector{ComplexF64}(undef, size_FFT)
    FFTVals2 = Vector{ComplexF64}(undef, size_FFT)
    iFFTplan = plan_ifft(FFTVals1)
    FFT_Cache{typeof(iFFTplan)}(FFTVals1, FFTVals2, iFFTplan)
end

function execude_fft(cache::FFT_Cache)
    (; iFFTplan, FFTVals1, FFTVals2) = cache
    valnumber = 1 # dummy value

    FFTVals1[1:valnumber] .= 1 # dummy value
    FFTVals1[valnumber+1:end] .= 0
    mul!(FFTVals2, iFFTplan, FFTVals1)  #<- commenting this line removes the stack overflow

    return nothing # dummy value
end

# function call
FFTW.set_num_threads(1)
fun1() # works

FFTW.set_num_threads(8) # 4 seems to work
fun1() # stack overflow

results in this access violation:

Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x688f888 -- .text at C:\Users\user\.julia\artifacts\b7dd1809d0626eac3bf6f97ba8ccfbb6cc63c509\bin\libfftw3-3.dll (unknown line)
in expression starting at C:\Users\user\path\test.jl:47
.text at C:\Users\user\.julia\artifacts\b7dd1809d0626eac3bf6f97ba8ccfbb6cc63c509\bin\libfftw3-3.dll (unknown line)
#2 at .\threadingconstructs.jl:404
unknown function (ip: 0000023cc9602972)
jl_apply at C:/workdir/src\julia.h:1879 [inlined]
start_task at C:/workdir/src\task.c:1092
Allocations: 700577 (Pool: 700003; Big: 574); GC: 1

when julia is started with julia --threads="auto" --startup-file="no" on windows.

julia> versioninfo()
Julia Version 1.9.2
Commit e4ee485e90 (2023-07-05 09:39 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, tigerlake)
  Threads: 8 on 8 virtual cores
Environment:
  JULIA_EDITOR = code

with FFTW 1.7.1

Either reducting the threads for FFTW or starting julia without threads solves this issue.

I'm not shure if this belongs here or in FFTW. I posted the issue there too.

brenhinkeller commented 1 year ago

Hmm, can't reproduce on 1.9.2 on mac (maybe os-specific?), but does max out cpu for a minute when Julia is started with #threads = #cores (vs returning more or less instantly when Julia is started with only a single thread)

It may be worth opening an issue on https://github.com/JuliaMath/FFTW.jl if you haven't already

judober commented 1 year ago

Yes, I opend issue 275 in FFTW (as linked in the opening post)

As for the os, I may be able to try macos myself to have another data point. What CPU are you using?

brenhinkeller commented 1 year ago
julia> versioninfo()
Julia Version 1.9.2
Commit e4ee485e909 (2023-07-05 09:39 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores
judober commented 1 year ago

Hmm, can't reproduce on 1.9.2 on mac (maybe os-specific?), but does max out cpu for a minute when Julia is started with #threads = #cores (vs returning more or less instantly when Julia is started with only a single thread)

I can confirm this behaviour on a similar setup:

julia> versioninfo()
Julia Version 1.9.2
Commit e4ee485e909 (2023-07-05 09:39 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 8 on 6 virtual cores
ovanvincq commented 9 months ago

I can also confirm this behaviour with FFTW 1.7.2 and Julia 1.10 on Windows.

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39 (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release        
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 28 × Intel(R) Xeon(R) W-2275 CPU @ 3.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, cascadelake)
  Threads: 20 on 28 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 14