 	i = blockIdx.x;
 	for(count=0; count<k; count++)
 	{
-		out[i*k+count] = findMin(m, n, k, count, D, out, R);
+		out[i*k+count] = findMin(m, n, k, count, D, out);
+		__syncthreads();
 	}
 }
 

	int *V, *out;				//host copies
	int *d_V, *d_out;			//device copies
	int *D;						
	int *R;						
//	int *dim_D;					//be replaced with shared memory
	FILE *fp;
	if(argc != 2)

		SM[ out[i*k+num] ] = INIT_MAX;
	}
	__syncthreads();
	for(s=m/2; s>0; s>>=1) 
	{
		// check whether the jth point is the same point as the ith one
		// or has already in the k-nn list
		if(j < s) 
		{
			if(SM[j] == SM[j+s])
			{
				if(R[j] > R[j+s])
				{
					R[j] = R[j+s];
				}
			}
			else if(SM[j] > SM[j+s])
			{
				SM[j] = SM[j+s];
				R[j] = R[j+s];
			}
			__syncthreads();
		}
	}
	// when m is odd, the last element of SM needs to be compared
	if(m > (m/2)*2)
	{
		if(SM[0] > SM[m-1])
		{
			R[0] = R[m-1];
		}
	}
	__syncthreads();
	return R[0];
}

// compute the k nearest neighbors
__global__ void knn(int m, int n, int k, int *V, int *D, int *out)
{
	int i;
	int count;
