A fairly trivial question

A fairly trivial question

A seemingly trivial question really.

I am trying to calculate the sum of squares or a vector of single precision values whilst maximizing performance and minimizing the accumulated error typically found in summing floats.

Currently I am using a method like the following:

// Use vspowx to square the values in a vector
vspowx(n, a, 2, y);

// Sum the values in a loop
float sumOfSquares();
for ( int i(); i < n; i++ ){
sumOfSquares += y[i];

Can anyone think ofa faster and/or more accurate MKL way of summing a vector of floats (in this case absolute values) without casting to doubles ?

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

In most cases the following example will be faster:

for ( i=0 ; i < n; i++ )
sumOfSquares += a[i]*a[i];

If your compiler will not be able to vectorise this code, then the following code might be faster then the previous example (but accuracy will suffer):

sumOfSquares = snrm2(&n, a, &incx );
sumOfSquares *= sumOfSquares;

Leave a Comment

Please sign in to add a comment. Not a member? Join today