@threads is really working in parallel?

Hi,

on my system:

julia> versioninfo()
Julia Version 1.1.1
Commit 55e36cc308 (2019-05-16 04:10 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, haswell)
Environment:
  JULIA_EDITOR = "C:\Users\WildKev\AppData\Local\atom\app-1.38.2\atom.exe"  -a
  JULIA_NUM_THREADS = 4

the following code

using Plots

a = falses(1000)
a[100] = true

function tfunc(a)
   stop = Threads.Atomic{Bool}(false)
   threadid = zeros(size(a))

   Threads.@threads for i = 1:length(a)
     threadid[i] = Threads.threadid()
     stop[] && break
     a[i] && Threads.atomic_xchg!(stop, true)
   end

   return threadid
end

plot(tfunc(a))

produces the following threadid over element plot.

It looks like the splitted iteration space runs sequential.

what does Threads.nthreads() say?

julia> Threads.nthreads()
4

In the plot the y-axis also indicates that they are in use.

Which part makes you think “It looks like the splitted iteration space runs sequential.” Did you expect everything to start exactly at the same time?

You are hitting thread start up time…it takes time to start the 3 additional threads.

If you change it to:

   a[600] = true

You will get some interesting graphs and on my machine (4 core) the result is different every run.

This might be more what you are looking for. Basically I give each thread 3 seconds to start and become stable before processing the array:

using Plots
using Dates

a = falses(1000)
a[100] = true

function tfunc(a)
   stop = Threads.Atomic{Bool}(false)
   threadid = zeros(size(a))

   start = (second(now()) + 3) % 60
   Threads.@threads for i = 1:length(a)
     while second(now()) != start
     end

     threadid[i] = Threads.threadid()
     stop[] && break
     a[i] && Threads.atomic_xchg!(stop, true)
   end

   return threadid
end

plot(tfunc(a))