Skip to content

@threads does not work with JULIA_COPY_STACKS=1 #44589

@chriselrod

Description

@chriselrod
julia> a = zeros(Int, 100_000);

julia> Threads.@threads for i in 1:length(a)
         a[i] = Threads.threadid()
       end

julia> all(isone, a)
true

julia> versioninfo()
Julia Version 1.9.0-DEV.151
Commit b7b46afcf2* (2022-03-07 22:01 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: 28 × Intel(R) Core(TM) i9-9940X CPU @ 3.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake-avx512)
  Threads: 28 on 28 virtual cores
Environment:
  JULIA_COPY_STACKS = 1
  JULIA_NUM_THREADS = 28

julia> Threads.nthreads()
28

First noted in: https://discourse.julialang.org/t/possible-threading-bug-on-m1-max-only-arm-build/77779

Behavior is worse on Julia 1.7, where only thread 1 actually writes, but the array is still divided up among all threads:

julia> a = zeros(Int, 100_000);

julia> Threads.@threads for i in 1:length(a)
         a[i] = Threads.threadid()
       end

julia> all(isone, a)
false

julia> all(<=(1), a)
true

julia> sum(==(1), a)/length(a)
0.03572

julia> versioninfo()
Julia Version 1.7.3-pre.0
Commit 2ca8b0cf3d (2022-02-07 18:56 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Core(TM) i9-9940X CPU @ 3.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake-avx512)
Environment:
  JULIA_COPY_STACKS = 1
  JULIA_NUM_THREADS = 28

julia> 1/28
0.03571428571428571

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions