Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding double within a parallel loop - std::atomic<double>

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?

like image 918
Jixxy Avatar asked Feb 20 '26 04:02

Jixxy


1 Answers

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.

like image 139
LWimsey Avatar answered Feb 21 '26 18:02

LWimsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!