-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Open
Labels
multithreadingBase.Threads and related functionalityBase.Threads and related functionality
Description
I ran into this very strange bug while exploring the new multithreading stuff. The smallest reproducible case I can come up with:
using Base.Threads
function right(v)
ws = ntuple(w->Vector{Int}(length(v)), nthreads())
@threads for t in 1:nthreads()
w = ws[t]
for i in 1:length(v)
w[i] = v[i]
end
end
w2 = 1 # unused binding to an unused variable name
return ws
end
function wrong(v)
ws = ntuple(w->Vector{Int}(length(v)), nthreads())
@threads for t in 1:nthreads()
w = ws[t]
for i in 1:length(v)
w[i] = v[i]
end
end
w = 1 # unused binding to the name of the previously used loop variable
return ws
end
Note that the only difference between the above two functions is w2 = 1
and w = 1
, and that neither variable is actually used after being assigned.
Running the above in the REPL:
julia> v = collect(1:3)
julia> right(v) # gives the correct output every time
([1,2,3],[1,2,3],[1,2,3],[1,2,3])
julia> wrong(v)
([4624170704,4658607408,4624170720],[4563189968,4574717712,4563189968],[1,2,3],[1,2,3])
julia> wrong(v)
([1,2,3],[1,2,3],[1,2,3],[4624170800,4658607472,4624170816])
julia> wrong(v)
([1,2,3],[1,2,3],[1,2,3],[1,2,3])
julia> wrong(v)
([1,4658458992,4624197552],[1,2,3],[4624197648,4624197664,4624197680],[1,2,3])
It appears that some iterations of the multithreaded loop in wrong(v)
aren't always being executed, or possibly that the binding of the loop variable w
to ws[t]
is broken. Either way, one can induce the correct behavior by making sure the post-loop binding to 1
doesn't match the loop variable name.
I thought maybe that this was a escaping/scoping bug in _threadsfor
, but I didn't see anything that jumped out at me.
julia> versioninfo()
Julia Version 0.5.0-dev+2502
Commit baf336f (2016-02-04 18:01 UTC)
Platform Info:
System: Darwin (x86_64-apple-darwin13.4.0)
CPU: Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.7.1
schneiderfelipe
Metadata
Metadata
Assignees
Labels
multithreadingBase.Threads and related functionalityBase.Threads and related functionality