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

Crash when using a multithreaded loop with BigFloat on macOS #35702

Closed
giordano opened this issue May 2, 2020 · 2 comments
Closed

Crash when using a multithreaded loop with BigFloat on macOS #35702

giordano opened this issue May 2, 2020 · 2 comments
Labels
bignums BigInt and BigFloat system:mac Affects only macOS

Comments

@giordano
Copy link
Contributor

giordano commented May 2, 2020

I've been observing the this crash for a while on macOS with Julia v1.3+, including nightly, in CI of LombScargle.jl. Today I've been able to create a smallish self-contained code to reproduce it:

using LinearAlgebra, Base.Threads

function _generalised_lombscargle!(P, freqs, times, y, w, Y, YY, T)
    Threads.@threads for n in eachindex(freqs)
        ω = freqs[n] * 2 * pi
        # Find τ for current angular frequency
        C = zero(T)
	S = zero(T)
	CS = zero(T)
	CC = zero(T)
        for i in eachindex(times)
            W    = w[i]
            s, c = sincos* times[i])
            CS  += W*c*s
            CC  += W*c*c
            C   += W*c
            S   += W*s
        end
        CS -= C*S
        SS  = 1 - CC - S*S
        CC -= C*C
        ωτ   = atan(2CS, CC - SS) / 2
        # Now we can compute the power
        YC_τ = zero(T)
	YS_τ = zero(T)
	CC_τ = zero(T)
        for i in eachindex(times)
            W     = w[i]
            s, c  = sincos*times[i] - ωτ)
            YC_τ += W*y[i]*c
            YS_τ += W*y[i]*s
            CC_τ += W*c*c
        end
        # "C_τ" and "S_τ" are computed following equation (7) of Press &
        # Rybicki, this formula simply comes from angle difference trigonometric
        # identities.
        sin_ωτ, cos_ωτ = sincos(ωτ)
        C_τ    = C*cos_ωτ + S*sin_ωτ
        S_τ    = S*cos_ωτ - C*sin_ωτ
        YC_τ  -= Y*C_τ
        YS_τ  -= Y*S_τ
        SS_τ   = 1 - CC_τ - S_τ*S_τ
        CC_τ  -= C_τ*C_τ
        P[n] = (abs2(YC_τ)/CC_τ + abs2(YS_τ)/SS_τ)/YY
    end
    return P
end

times = 1:11
signal = big.(sin.(times))
w = fill!(similar(signal), one(eltype(signal))) / length(signal)
frequencies = 0.01:0.02:2.75
T = eltype(signal)
P = Vector{T}(undef, length(frequencies))
y = signal .- w  signal
Y = w  y
YY  = w  (y .^ 2) - Y * Y

_generalised_lombscargle!(P, frequencies, times, y, w, Y, YY, T)

When I run it on macOS with 2+ threads I get

julia(1539,0x700001a0e000) malloc: *** error for object 0x7f8eebd00520: pointer being realloc'd was not allocated
julia(1539,0x700001a0e000) malloc: *** set a breakpoint in malloc_error_break to debug

signal (6): Abort trap: 6
in expression starting at /Users/mose/foo.jl:59
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 1718565 (Pool: 1718182; Big: 383); GC: 2
Abort trap: 6

My understanding is that the nested for loops are the problem, when I comment them the code doesn't crash. I may be doing something silly, I wrote this code more than 2 years ago, but it has been working since then. It doesn't crash with Julia v1.2- nor any other operating system nor when using a single thread.

@mcabbott
Copy link
Contributor

mcabbott commented May 2, 2020

Can confirm this happens on my macs. Sometimes with a longer error message:

julia> _generalised_lombscargle!(P, frequencies, times, y, w, Y, YY, T)
julia(7335,0x7000103a5000) malloc: *** error for object 0x7f9fe130b8a0: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug
julia(7335,0x7fff9bc69380) malloc: *** error for object 0x7f9fdbd01e60: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

signal (6): Abort trap: 6
signal (6): Abort trap: 6
n expression starting at REPL[20]:1
in expression starting at REPL[20]:1
julia(7335,0x700011bae000) malloc: *** error for object 0x7f9fe132bee0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
julia(7335,0x7000103a5000) malloc: *** error for object 0x7f9fe132bad8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Allocations: 16516003 (Poolllocations: 16516003 (Pool: 16511892; Big: 4111); GC: 20
in expression starting at REPL[20]:1
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 16516003 (Pool: 16511892; Big: 4111); GC: 20
Abort trap: 6

@JeffBezanson JeffBezanson added bignums BigInt and BigFloat system:mac Affects only macOS labels May 6, 2020
@mcabbott
Copy link
Contributor

mcabbott commented Aug 9, 2020

Appears to be fixed by #36776

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bignums BigInt and BigFloat system:mac Affects only macOS
Projects
None yet
Development

No branches or pull requests

4 participants