Here is a piece of code (not a MWE…)
function parallelkernel!(elca,idof,R,...) # fast typestable kernel @threads for iel ∈ eachindex(elca) r = incremental(elca[iel],...) #lock(lk) do R[ idof[iel]] .+= r # potential BUG race condition #end end end
I think the code is quite uncomplicated: within a threaded loop I compute vectors
r, which I add into larger vector
Several threads can thus be accessing the same elements in
R, and in my understanding this is the classical “race condition” which I need to avoid. I have run this code hundreds of times, and always got the correct result, though (famous last words).
I believe I cannot use
Atomic here, because
r is not a primitive type (right?) but Julia’s manual mentions
lock. However if I uncomment the
do bracket in the above code I get
Possibly, I have a subtle bug in my code, not visible in the above simplified code. In that case, I am on my own. What I would like to know is, did I completely misunderstand how
lock is supposed to be used? The example of usage in the manual does not show the context of a threaded loop, was I wrong in assuming this usage to be the pattern?