# convertion euclidean distance function to cuda

hey i have a sequence code for finding euclidean distance:

``````float euclidean_dist( float *productCoords,  float *coords points)
{
int n;
float ans=0.0;

for (n=0; n < WEEKS_NUM; n++)
{
ans += (productCoords[n]-coords[n]) * (productCoords[n]
coords[n]);
}

return(sqrt(ans));
}
``````

i want to convert that function to cuda so each time i will call euclidean_dist function will do that in cuda.

how can i do that? i tried everything

This doesn’t look like valid C or C++ code to me. I think an operator (-) got removed between two of your posted lines.

But in any event, as stated in a comment on your cross posting:

https://stackoverflow.com/questions/47351198/convertion-euclidean-distance-function-to-cuda

the design pattern to use here is a parallel reduction. You can find numerous examples and discussions of this on the web, and the CUDA samples include a parallel reduction sample code, along with an accompanying presentation. Google “Mark Harris reduction”

So it seems you are computing the element-wise difference of vectors ‘productCoords’ and ‘points’, then computing the norm of the resulting vector?

If the vectors are quite long, you could apply an element-wise subtraction, then apply cublasSnrm2() to compute the norm of the difference vector, which includes guards against overflow and underflow in intermediate computations. This two-pass approach may not be the fastest, however. And cublasSnrm2() is over-kill if the vectors are short.