If I use a shared variable, let’s say a double, to calculate some kind of sum along the execution of the program. Would that in anyway be vulnerable to non-stable operations? I mean, would it be possible that more than one core would access this variable in an asynchronous way and cause unstable results?
For example:
this is a global variable:
double totalTime = 0;
and in each core a command is called:
totalTime += elapsedTime;
This last operation/statement is executed by taking the value of totalTime, put it the the CPU register, and then do the addition. I can imagine that more than one core would take the same value at the same instant, and then add the new elapsedTime, and then the value stored in totalTime would be overwritten with the wrong value, due to latency. Is that possible? and how can I solve this?
Thank you.
Clearly this operation is not thread-safe since, as you mentioned yourself, it involves several assembler instructions. In fact, openMP even has a special directive for this kind of operations.
You will need the
atomicpragma to make it, well, “atomic”:Note that
atomiconly works when you have a single update to a memory location, like an addition, increment, etc.If you have a series of instructions that need to atomic together you must use the
criticaldirective:Edit: Here’s a good suggestion from “snemarch”: If you are repeatedly updating the global variable
totalTimein a parallel loop you can consider using thereductionclause to automatize the process and also make it much more efficient:At the end
totalTimewill correctly contain the sum of the localelapsedTimevalues without need for explicit synchronization.