use float AtomicAdd to write to a float4

For “Fermi” cards, the arithmetic function atomicAdd can be applied to floats. I want to use it for a float3, without having to revise how my data is stored.

It seems like I should be able to hand the atomicAdd the address of the components of the float3 and then use three atomicAdds.

        // write out to the force array in global/cache with an atomicadd
        float * addr = &d_force[n_idx];
        atomicAdd(addr+0, val_x);
        atomicAdd(addr+4, val_y);
        atomicAdd(addr+8, val_z);

However, my compiler has not been fooled

error: a value of type “float3 *” cannot be used to initialize an entity of type "float *

Suggestions?

1 Like

Yes:

// write out to the force array in global/cache with an atomicadd

			float * addr = &(d_force[n_idx].x);

			atomicAdd(addr+0, val_x);

			atomicAdd(addr+1, val_y);

			atomicAdd(addr+2, val_z);

or better (because it prevents accidental mistakes with flawed pointer arithmetics, like in your case):

// write out to the force array in global/cache with an atomicadd

			atomicAdd(&(d_force[n_idx].x), val_x);

			atomicAdd(&(d_force[n_idx].y), val_y);

			atomicAdd(&(d_force[n_idx].z), val_z);
1 Like

Yes:

// write out to the force array in global/cache with an atomicadd

			float * addr = &(d_force[n_idx].x);

			atomicAdd(addr+0, val_x);

			atomicAdd(addr+1, val_y);

			atomicAdd(addr+2, val_z);

or better (because it prevents accidental mistakes with flawed pointer arithmetics, like in your case):

// write out to the force array in global/cache with an atomicadd

			atomicAdd(&(d_force[n_idx].x), val_x);

			atomicAdd(&(d_force[n_idx].y), val_y);

			atomicAdd(&(d_force[n_idx].z), val_z);
1 Like

Thanks!

Thanks!