alphalinux.org high-perf list (1998-2001)
 help / color / mirror / Atom feed
* request for perf improvement suggestions
@ 1998-11-25 21:15 Jonathan L Dubois
  1998-11-25 21:44 ` Adam
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jonathan L Dubois @ 1998-11-25 21:15 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1122 bytes --]

Hello everyone,

I have some code I would like to optomize and I could use some suggestions

and pointers for where to get started.

The function which is called most often and the most likely candidate

for improvement (I think) calculates the distance between two vectors.

The source I am currently using is locate below:

void r12(double *rij,double *x1,double *x2){

  int i;

  double tmp1,tmp2;

  tmp2 = 0;

  for(i=0;i<3;i++){

    tmp1 = x1[i]-x2[i];

    tmp2 += tmp1*tmp1;

  }

  tmp2 = sqrt(tmp2);

  *rij = tmp2;

}

my guess is that this is a pretty classic problem and there are some nifty

tricks I could apply in assembly to make this quite fast.  I'm fairly new

to this though (my only assembly experience was with the MC6800 several

years ago and I don't think I really understand all the issues involved

with optomization on a RISC chip)  So my questions are,  is this the best

form for the C code?  Is there an existing assembly routine for finding

the distance between two vectors?  If not, any suggestions for the

implementation of one?

Kind Regards,

Jonathan DuBois

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: request for perf improvement suggestions
  1998-11-25 21:15 request for perf improvement suggestions Jonathan L Dubois
@ 1998-11-25 21:44 ` Adam
  1998-11-26  9:45 ` Stefan Schroepfer
  1998-11-26 15:48 ` Joachim Wesner
  2 siblings, 0 replies; 4+ messages in thread
From: Adam @ 1998-11-25 21:44 UTC (permalink / raw)


Jonathan L Dubois wrote:

> Hello everyone,

>

> I have some code I would like to optomize and I could use some suggestions

> and pointers for where to get started.

>

> The function which is called most often and the most likely candidate

> for improvement (I think) calculates the distance between two vectors.

Under some conditions you can go a long way by using K. Goto's optimized BLAS

routines (gemm, copy, dot, axpy).  If you have two separate sets of n vectors

and want to calculate the n distances between corresponding vectors of each

set, you can store them as two arrays of coordinates, copy the first array,

use axpy to subtract the second array, and then use dot for each vector (dot

itself) to get the sum of squares.  You might get some speedup too by saving

all of the square roots for last, and then doing them all at once, so

whatever tables are used by sqrt sit in cache the whole time.

On the other hand, if you have a collection of n vectors and want to get all

n(n+1)/2 distances between them, you'll have to be more creative.  For

example, to get all of the distances from the first vector to the others

(assuming they're all stored as one big array of coordinates), you could

create an array of n-1 ones, use gemm to "multiply" the 3x1 vector by this

1x(n-1) array of ones to give a 3x(n-1) matrix with each column being the

first vector, then use axpy to subtract all of the other vectors from this,

and dot each column with itself to get sums of squares.  Now repeat for the

second vector, and all the others.  Finally, square root everything.

On the other hand, if you have n vectors and want some arbitrary subset of

the n(n+1)/2 distanes, well, this may not help much.

This approach would vectorize the subtractions very nicely, but the dot

products might be slowed down by the overhead of the function calls to ddot_

as much as they are sped up by the assembly acceleration- maybe there's a way

to use another BLAS function call to do this...  (If gemm could be convinced

to only calculate the diagonals of the product, you could multiply the

transpose of the difference matrix by its non-transpose to give a vector of

the dot products; but gemm doesn't do this.)  Oh- you'll also have to

substantially change the program architecture.  (Fun! :-)

Hope this helps (and isn't too obscure).

Zeen,

-Adam `Cold Fusion' Powell, IV http://www.ctcms.nist.gov/~powell/ ____

USDoC, National Institute of Standards & Technology (NIST)  |\ ||<  |

Center for Theoretical and Computational Materials Science  | \||_> |

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: request for perf improvement suggestions
  1998-11-25 21:15 request for perf improvement suggestions Jonathan L Dubois
  1998-11-25 21:44 ` Adam
@ 1998-11-26  9:45 ` Stefan Schroepfer
  1998-11-26 15:48 ` Joachim Wesner
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Schroepfer @ 1998-11-26  9:45 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1115 bytes --]

Jonathan L Dubois wrote:

> 

> void r12(double *rij,double *x1,double *x2){

>   int i;

>   double tmp1,tmp2;

> 

>   tmp2 = 0;

>   for(i=0;i<3;i++){

>     tmp1 = x1[i]-x2[i];

>     tmp2 += tmp1*tmp1;

>   }

>   tmp2 = sqrt(tmp2);

>   *rij = tmp2;

> }

From my experience I would start from something like the following.

I assume usage of C++ to have `inline' available as part of the

standard and not as a GNU CC extension. In C I would prefer a macro

implementation.

inline void r12(double *rij,double *x1,double *x2){

  double   tmp1, tmp2, tmp3;

  tmp1 = x1[0] - x2[0];

  tmp2 = x1[1] - x2[1];

  tmp3 = x1[2] - x2[2];

  *rij = sqrt (tmp1 * tmp1 + tmp2 * tmp2 + tmp3 * tmp3);

}

Improvements over the first version version:

 - Entirely avoided inner loop.

 - Differences can be calculated independently.

Be sure that

 - you are using libffm's sqrt ()

 - you really need the distance computed every time you call

   this function and not its sqare (an alternative would be

   to do the sqrt () selectively in the calling functions)

Stefan Schroepfer

scr@iis.fhg.de

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: request for perf improvement suggestions
  1998-11-25 21:15 request for perf improvement suggestions Jonathan L Dubois
  1998-11-25 21:44 ` Adam
  1998-11-26  9:45 ` Stefan Schroepfer
@ 1998-11-26 15:48 ` Joachim Wesner
  2 siblings, 0 replies; 4+ messages in thread
From: Joachim Wesner @ 1998-11-26 15:48 UTC (permalink / raw)


Stefan Schroepfer wrote:

> 

> Jonathan L Dubois wrote:

> >

> > void r12(double *rij,double *x1,double *x2){

> >   int i;

> >   double tmp1,tmp2;

> >

> >   tmp2 = 0;

> >   for(i=0;i<3;i++){

> >     tmp1 = x1[i]-x2[i];

> >     tmp2 += tmp1*tmp1;

> >   }

> >   tmp2 = sqrt(tmp2);

> >   *rij = tmp2;

> > }

> 

>  - you really need the distance computed every time you call

>    this function and not its sqare (an alternative would be

>    to do the sqrt () selectively in the calling functions)

> 

What also will help is to use the vectorized sqrt functions by Goto

that are in the new version of libffm. Even if initially wanted to

wait for all routines to have full arguments checking in version 0.30, I

now think I should better already release the actual status as, say

0.28. Give me one or two more days, it will be out this weekend.

JW

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-11-26 15:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-11-25 21:15 request for perf improvement suggestions Jonathan L Dubois
1998-11-25 21:44 ` Adam
1998-11-26  9:45 ` Stefan Schroepfer
1998-11-26 15:48 ` Joachim Wesner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox