Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.
1693 Discussions

## MKL Distance Between Vector

Beginner
560 Views
Hi To All

Does anybody know if the MKL contains a function tocalaulate the distance between 2 vectors.

sqrt((x1 - y1)(x1-y1) + (x2 - y2)(x2 - y2) ........(Xn - Yn)(Xn - Yn))

BB

8 Replies
Moderator
560 Views
The Intel MKL doesn't contain such functionality. You can try to develop your owner code and build it by the Intel Compiler with some high-level optimizations options. It would produce very good performance results.
Moderator
560 Views
Hi BB,
i've forget to add, you can find the similarfunctionalityinto another performance library - IPP. see Signal Processingdomain: functions Sub (Subtracts the elements of two vectors) and then Sqrt (Computes a square root of each element of a vector.
Black Belt
560 Views
As this appears to involve 3 IPP functions (vector subtract, vector multiply, sum reduction), it may take twice as long as plain C or Fortran with a vectorizing compiler, and won't be a lot more readable even than what you would write with intrinsics in MS compiler.
Beginner
560 Views
that is what I am tring to do I have a written code and I am tring to get it to run faster.
how do I use the intel compiler? hwat are the optimization options?
Beginner
560 Views
is there a function to sum a vector?
Moderator
560 Views

yes. See the ?asum routines which compute the sum of magnitudes of the vector elements.

please refer to the MKLdocumentationfor more details.

Beginner
560 Views
?sum (add without taking absolute value) should work here too.
Since you say you want speed, plain C or Fortran code, with a vectorizing compiler, would be a better bet.
If you are using the current Intel C or Fortran, and haven't figured out how to use the simd reduction directive, let us know which compiler you want sample code for. Do you know Fortran ?
sqrt(dot_product(x-y,x-y))

If that doesn't vectorize (requires /fp:fast), it's embarrassing, but you could then fall back on the directive.
If your cases are long enough for threading to be useful, and auto-parallel doesn't do the entire job, we could give you examples in openmp, or maybe someone on TBB or cilk or ArBB forum could help you, if you have chosen one of those C++ namespaces.

sum = 0
!dir\$ omp parallel do reduction(+: sum) private (diff) if(n>9999)
!dir\$ simd reduction(+: sum)
do i=1,n
diff = x(i) - y(i)
sum = sum + diff**2
end do
yourresult = sqrt(sum)

In fact, the Qparallel /Qpar-threshold0 appear to handle this better than the OpenMP and simd directives.

In my example, when alignment is asserted, the compiler chooses only vectorization, no threading, even at par-threshold0. As the SSE2 vector code produces 4 parallel sums, which are added implicitly at the end, implementing 8 loop iterations per pass, you can see that a big data set would be needed before threading could prove beneficial.
Black Belt
560 Views
BB

How large is n in:

sqrt((x1 - y1)(x1-y1) + (x2 - y2)(x2 - y2) ........(Xn - Yn)(Xn - Yn))

How frequently is the above set differenced? (integrated)

And how many different matched such vector differences are made?

The answers to these questions will alter the coding strategy.

Also is: sqrt((x1 - y1)(x1-y1) + (x2 - y2)(x2 - y2) ........(Xn - Yn)(Xn - Yn))

Used for an absolute distance or for use in ordering distances between vector pairings? (ordering by proximity). IOW can you eliminate the ^2 and sqrt by using sum of abs?

Jim Dempsey