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?
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[])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With