I recently encountered a simple notion in programming but i stuck when i tried to implement it in cuda.
Suppose that i have thousands of elements and i want to find the closest pair between them. I use atomicMIN
in global memory (suppose that we dont want to reduce) so if the distance which is calculated by each thread is smaller than the distance stored in the global variable the atomicCAS will replace it with the smaller value. For example i have the global variable float gbl_min_dist
To do this I use the following code:
__device__ inline float atomicMin(float *addr, float value){
float old = *addr, assumed;
if( old <= value ) return old;
do{
assumed = old;
old = atomicCAS((unsigned int*)addr, __float_as_int(assumed), __float_as_int(value));
}while( old!=assumed );
return old;
}
Suppose now that we want to store the index of the two points that were closer together and for which the atomicMIN
has successfully replaced the old minimum distance with the one calculated by those two points. What I mean is that I only want to store the indeces of the two points that currently have the smaller distance if and only if its distance has just been successfully swaped in the global variable
typedef struct {float gbl_min_dist,
unsigned int point1,
unsigned int point2;} global_closest_points;
So here, when a thread executes the atomicMIN
, if the value that is proposed by that tread to be compared is swapped in the gbl_min_dist
then i also need to swap the p1, p2 with the values from the thread. If the gbl_min_dist
is not swapped then I dont want to store the points cause this would give wrong points but correct minimum distance.
Is there any return value to check if atomicCAS
has made the swap?
Any ideas on how to implement this within the atomicMIN
?
Thanks in advance