Atomics and flushing

I have the following code:

if (localID == 0) {

for (int i=0; i<49; i++) {
atom_add(&cov[idy], aux);

The problem is that if I just want to save those sums on cov array (which is global), outside the kernel I can read the data and everything is fine. But, if I want to do this after:

*f = cov[0];

The values stored on cov array (on any of its positions) are not consistent yet, even if before this line I put a fence or barrier. In this case, shouldn’t occur some sort of flushing on the atomic operations so the values on cov[] could be used?


I’m not sure I completely understand but maybe some of this will help. atom_add returns the old value before the add so you can use that to determine what the value is after the add. This will get you cov[idy], at any rate.

If you want cov[0] at some point, you will get a version of that that is consistent with all the other atom_adds that may be executing. That is, you will not get the value half-way through an atom_add, but you are not guaranteed to get a value after all your kernels have finished. There is no way to do that in OpenCL except to wait for the kernel to finish and then run another kernel. (You can do this within a work-group by using a barrier.)