I start julia in Linux terminal with julia -t 6
. The code
Threads.@threads for i=1:100
println(i," ",Threads.threadid());
sleep(3); end
on my 6-core CPU returns
18 2
52 1
35 4
69 6
85 3
1 5
70 6
53 1
19 2
86 3
36 4
2 5
87 3
37 4
20 2
71 6
54 1
3 5
...
This is surprising to me, as I expected each thread to pick the first of the remaining elements in the collection. I expected the first 6 numbers on the left to be a permutation of 1:6, the next block a permutation of 7:12, the next block a permutation of 13:18, …
For solving my problems, this shuffling makes parallelization much less efficient. Is there a way to force the threads to iterate in the natural order of the elements in the collection?
So to be clear, if some thread early on picks an element which requires the longest and finishes last, that’s fine. I just want each thread to pick the first/left-most available element from the collection.