Memory conflict I think....

Hello again!!!

I’m having a problem when I’m ordering 16 threads to write to same position of an array, and the value that is written it isn’t correct.

global void calculateRadius (Neighbor *matrixDevice,float *radiusDevice, int numvertex){

int i=blockIdx.x*blockDim.x+threadIdx.x;



if( i < numvertex*16 && (matrixDevice[i].neighbor2.x != 0.0f) && (matrixDevice[i].neighbor3.x != 0.0f)){ 
	
	float radius = calculate(matrixDevice[i].neighbor1, matrixDevice[i].neighbor2, matrixDevice[i].neighbor3);
	
	
	int index = i/16;
	
	if(radius > radiusDevice[index])
		
		raiosDevice[index]= radius; 
	
	}

}

I’ve calculated the value that should be in the first position and it isn’t right, so I believe it is a memory conflict. If someone know how to solve this, please let me know.

IIRC, the behavior is undefined when many threads do unsynchronised writes to shared memory. What happens is maybe that thread A checks the value, thread B checks it too, then thread A writes, then B writes, even though B isn’t supposed to write over A. Looks like you’ll have to do a synctreads() and step through the threads. Or find a way to have no overlap in the array positions of the threads.

Thanks for the reply

But, Sorry to ask, but there is a way to step through the threads??

cause the other way you said, it’s not gonna be a optimal solution.

What are you trying to do? Find the Maximum value among 16 threads? You can’t do that by having multiple threads writing to the same memory location, that causes race conditions as was already pointed out.

You’ll need to use a tree-like reduction: see the reduction example in the SDK: It is as fast as it gets.