What is julia doing with your threads?

Somewhat related discussion around that the main thread is in the :default or :interactive pool depending on if one starts Julia with interactive threads.

2 Likes

Thatā€™s not thread-safe. Breaking thread-safety not only affects accuracy, but it often ends up harming performance, too. I highly suspect that (or GC, as in the other thread that reminded me of this one) as being the problem here.

2 Likes

Somehow I donā€™t see how the compiler could mind the access to s.
Otherwise it would know it was not thread-safe and it could be made
be thread-safe by the compilerā€¦ :wink: But I donā€™t think it does.

The problem isnā€™t the access, itā€™s the mutation. The threads will trample over eachother as they all try to grow the memory.

This is definitely a problem:

julia> function f(s, n)
           Threads.@threads for i in 1:n
               push!(s, i)
           end
           return s
       end
f (generic function with 1 method)

julia> f(Float64[], 10000)
4630-element Vector{Float64}:
    1.0
    2.0
    3.0
    ā‹®
 6249.0
 6250.0

julia> length(f(Float64[], 10000))
3162

julia> length(f(Float64[], 10000))
6250

julia> length(f(Float64[], 10000))
1678

julia> minimum(f(Float64[], 10000))
5.2150196471021e-310
5 Likes