I am developing an OpenMP code in C++ (the compiler is g++ 4.8.2). In a part of my code I need to perform an add atomically on a struct data. The strcut is defined as:
struct real3
{
float x;
float y;
float z;
};
and I defined addition operator for it as follows:
inline real3 operator+(real3 a, real3 b)
{
return make_real3(a.x + b.x, a.y + b.y, a.z + b.z);
}
I use this strcut in all parts of my code. In one part of my program, I need to perform a add operation atomically:
real3 * m_cforce;
real3 fn, ft;
int i;
/*
. . . . some code is here
*/
#pragma omp atomic
m_cforce[i] = m_cforce[i] + (fn + ft);
The compile does not accept the struct real3 as the operands for atomic add. one solution is to use the following code instead:
#pragma omp atomic
m_cforce[i].x = m_cforce[i].x + (fn + ft).x;
#pragma omp atomic
m_cforce[i].y = m_cforce[i].y + (fn + ft).y;
#pragma omp atomic
m_cforce[i].z = m_cforce[i].z + (fn + ft).z;
in this way, I use atomics 3 times more, and it will cost more time for me. Is there any save way for me to do this operation with lower computational overhead?