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!
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;
}