Wrong results with multi-threading in Julia master


#1

I was playing with multi-threading in Julia master and noticed that my code currently gives wrong results when more than one thread is enabled.

Julia 0.5:

julia> versioninfo()
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
  System: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, haswell)

julia> Threads.nthreads()
4

julia> function foo()
           p = Vector{Float64}(20)
           Threads.@threads for i in eachindex(p)
               p[i] = i
           end
           return p
       end
foo (generic function with 1 method)

julia> foo()
20-element Array{Float64,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0
 11.0
 12.0
 13.0
 14.0
 15.0
 16.0
 17.0
 18.0
 19.0
 20.0

Julia master:

julia> versioninfo()
Julia Version 0.6.0-dev.1924
Commit 2356ebb3f (2017-01-05 06:38 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

julia> Threads.nthreads()
4

julia> function foo()
           p = Vector{Float64}(20)
           Threads.@threads for i in eachindex(p)
               p[i] = i
           end
           return p
       end
foo (generic function with 1 method)

julia> foo()
20-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

Also assigning p[i] = Threads.threadid(), like in the example of the manual, gives wrong results.

Am I doing something wrong? Is the different behavior to be expected? I checked out the manual, but didn’t find any difference in the multi-threading section.


#2

Cross-reference: I opened a ticket on GitHub