question on atomic operation

I’ve been debugging my program for quite a while and finally I’ve found that the problem arises from the code below:

//Eventually counter_device_global gets the total number of new data generated by each thread

atomicAdd(&counter_device_global, newDataSize);

//Write the new data to the array

for(int i=counter_device_global-1;i<counter_device_global-newDataSize;i--)

{

     arrayInPageLockedMemory[i]=newData;

}

The catch is: different threads will read the same counter_device_global, and therefore write to the same memory address. Maybe using counter_device_global is really not a good idea at all, but is there any way that can write data to arrayInPageLockedMemory in a sequential manner? Thanks a lot for any suggestion! External Image

Finally worked out:

//Eventually counter_device_global gets the total number of new data generated by each thread

int temp=atomicAdd(&counter_device_global, newDataSize);

//Write the new data to the array

for(int i=0;i<newDataSize;i++)

{

     arrayInPageLockedMemory[temp+i]=newData;

}

I’d expect you want something like this:

//Eventually counter_device_global gets the total number of new data generated by each thread

int pos=atomicAdd(&counter_device_global, newDataSize);

//Write the new data to the array

for(int i=0;i<newDataSize;i++)

{

     arrayInPageLockedMemory[pos+i]=newData[i];

}

Note that this code will store the data from each thread consecutively, but the order of the data from different threads is undefined.

Hi tera,

Thanks for pointing that out. External Image I copy-pasted the wrong code. Now it’s correct.