I want to use a SharedArray in Julia and sometimes need to operate on the same cell. To do this I tried to use a lock, but this fails. After running the code below increment_array should have one entry with 10000 but instead it ends up with 99xx. I start up julia with julia -p auto, after which nprocs() returns 17.
using Distributed
using SharedArrays
@everywhere using Distributed, SharedArrays
@everywhere safe_increment_lock = ReentrantLock()
function main()
increment_array = SharedArray{Int}(1)
@sync @distributed for i in range(1, 10000)
lock(safe_increment_lock) do
increment_array[1] += 1
end
end
return increment_array
end
Locks are for thread safety (see Multi-Threading) not for distribution. Just try the following to verify:
as you can see, each process has its own lock, they are not shared.
A version that uses multi-threading instead of distribution could look like this:
Runnning with
julia -t auto:Various techniques exist if you really need distribution, often it makes sense to let each process work as much as possible on its own and aggregate the result at the end. In your example, something like:
This is of course not always as easy or even possible and there are other techniques. I recommend taking a look at the Transducers.jl package which could help in structuring the logic and simplify running it using distribution or multi-threading.