Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - Is it possible to use a progress bar and Threads.@threads in same for loop?

Suppose I have this for loop:

for i in 1:100000
   1+1 
   # Let's do some long computations here
end

Those two for loops work:

Threads.@threads for i in 1:100000
   1+1 
   # Let's do some long computations here
end

using ProgressMeter
@showprogress 1 "Computing..." for i in 1:100000
   1+1 
   # Let's do some long computations here
end

But none of the following works:

@showprogress 1 "Computing..." Threads.@threads for i in 1:100000
   1+1 
   # Let's do some long computations here
end

using ProgressMeter
Threads.@threads @showprogress 1 "Computing..." for i in 1:100000
   1+1 
   # Let's do some long computations here
end

So is it possible in Julia to have parallelism in a for loop as well as a Progress bar?

like image 236
JKHA Avatar asked Oct 20 '25 05:10

JKHA


1 Answers

This code works nicely:

using ProgressMeter
N = 200
p = Progress(N);
update!(p,0)
jj = Threads.Atomic{Int}(0)

l = Threads.SpinLock()
Threads.@threads for i in 1:N
   sum(rand(10_000_000)) # some big computation
   Threads.atomic_add!(jj, 1)
   Threads.lock(l)
   update!(p, jj[])
   Threads.unlock(l)  
end

If you do not want to use locks you might also consider updating only on the first thread (however in that case the progress bar runs less smoothly):

Threads.threadid() == 1 && update!(p, jj[])
like image 129
Przemyslaw Szufel Avatar answered Oct 23 '25 01:10

Przemyslaw Szufel