I have a parallel code that does some computation and then adds a double to an outside-the-loop double variable. I tried using std::atomic but it does not have suport for arithmetic operations on std::atomic < double > variables.
double dResCross = 0.0;
std::atomic<double> dResCrossAT = 0.0;
Concurrency::parallel_for(0, iExperimentalVectorLength, [&](size_t m)
{
double value;
//some computation of the double value
atomic_fetch_add(&dResCrossAT, value);
});
dResCross += dResCrossAT;
Simply writing
dResCross += value;
does obviously otput nonsense. My question is, how can I solve this problem, without making the code serial?
A typical way to atomically perform arithmetic operations on a floating-point type is with a compare-and-swap (CAS) loop.
double value;
//some computation of the double value
double expected = atomic_load(&dResCrossAT);
while (!atomic_compare_exchange_weak(&dResCrossAT, &expected, expected + value));
A detailed explanation can be found in Jeff Preshing's article about this class of operation.
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