简体   繁体   中英

C++: OpenMP shared memory protection

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 atomic pragma to make it, well, "atomic":

#pragma omp atomic
totalTime += elapsedTime;

Note that atomic only 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 critical directive:

#pragma omp critical
{
    // atomic sequence of instructions
}

Edit : Here's a good suggestion from "snemarch": If you are repeatedly updating the global variable totalTime in a parallel loop you can consider using the reduction clause to automatize the process and also make it much more efficient:

double totalTime = 0;

#pragma omp parallel for reduction(+:totalTime)
for(...)
{
    ...
    totalTime += elapsedTime;
}

At the end totalTime will correctly contain the sum of the local elapsedTime values without need for explicit synchronization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM