Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.7 kB
1
Indexable
Never
/* kernel function to calculate distance between every coordinate */
__global__ void calculateDistances(Coordinate *coordinates, float *distances, int numPoints) {
    int index = blockIdx.x * blockDim.x + threadIdx.x;
    int stride = blockDim.x * gridDim.x;

    for (int i = index; i < numPoints * numPoints; i += stride) {
        int coord1 = i / numPoints;
        int coord2 = i % numPoints;
        
        /* avoid calculating distance with the point itself */
        if (coord1 != coord2) {
            float dx = coordinates[coord2].x - coordinates[coord1].x;
            float dy = coordinates[coord2].y - coordinates[coord1].y;
            distances[i] = sqrt(dx * dx + dy * dy);
        } else {
            distances[i] = FLT_MAX;
        }
        
        //printf("Coord1 = (%f, %f), Coord2 = (%f, %f), distance = %f\n", coordinates[coord1].x, coordinates[coord1].y, coordinates[coord2].x, coordinates[coord2].y, distances[i]);
    }
}


/* kernel function to check proximity criteria for each point */
__global__ void checkProximityCriteria(float *distances, int *results, int *globalCounter, int numPoints, int K, float D) {
    int index = blockIdx.x * blockDim.x + threadIdx.x;
    int stride = blockDim.x * gridDim.x;

	for (int i = index; i < numPoints && *globalCounter < NUM_SATISFY; i += stride) {
	    int count = 0;
	    
		for (int j = 0; j < numPoints && *globalCounter < NUM_SATISFY; j++) {
        	if (i != j && distances[i * numPoints + j] < D) {
	            count++;
	        }
	    }
	    
	    if (count >= K) {
	    	results[i] = 1;
	    	atomicAdd(globalCounter, 1);
	    } else {
	    	results[i] = 0;
	    }
	}
}