Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

cblas_dasum is not adding up negative numbers

Auvi_R_
Beginner
581 Views

Hello,

I am trying to get the sum of a vector using MKL's cblas_dasum function in C using Intel C++ compiler but the negative numbers are not subtracted. I would expect 40.0 + 2.0 - 42.0 == 0.0 but I am getting 40.0 + 2.0 - 42.0 == 84.0. Here is the code:

/* compiled with  icc -mkl blastest.c -o blastest */

#include <stdio.h>
#include <stdlib.h>
#include "mkl.h"

#define N 3

int main() {
  double *v;
  v = (double *) mkl_malloc(N * sizeof( double ), 64);
  v[0] = (double) 40.0;
  v[1] = (double)  2.0;
  v[2] = (double) -42.0;
  printf("Sum  = %lf\n", cblas_dasum(N, v, 1));
  return 0;
}

Can anybody tell me what's wrong in the code as I am getting 84.0 as the answer.

Thanks,

Auvi

 

0 Kudos
6 Replies
mecej4
Honored Contributor III
581 Views

Please read the manual page containing the specification of ?asum. The routines sasum, dasum return the sum of the absolute values of the input vector array elements. The casum and zasum routines return the sum of the absolute values of the real plus the absolute values of the imaginary parts of the input vector array elements.

0 Kudos
Auvi_R_
Beginner
581 Views

Thanks for opening my eyes! This was an oversight on my part as I did download the documentation and tried to quickly locate a "sum" function. I don't see just a "dsum" function though that will work for my purpose. What is the best way to achieve this? A dot product of two vectors where one of them is just "1"s? summing up vectors is something I do all the time in other environments (numpy, MATLAB) and I'm looking for a straightforward way to achieve that in MKL.

Thanks in advance,

 

0 Kudos
TimP
Honored Contributor III
581 Views
Intel c++ offers __sec_reduce_add as well as accumulate and so on. ? Sum is a common blas extension not offered by netlib or mkl.
0 Kudos
Gennady_F_Intel
Moderator
581 Views

Auvi, 

mkl doesn't provide such functionality. In the case if this is "hot spot" into your application - you can use Tim's suggestion to use Cilk Plus option or  try to use another performance libraries 0 Intel(R) IPP which provides computing the sum of the elements of vectors:  ippsSum_32f and etc...  . In the case if this is not "hot spot" -- the plain code compiled by Intel Compiler would give you good results from performance point of view. 

--Gennady

0 Kudos
TimP
Honored Contributor III
581 Views
My tests show no performance difference among C++ STL, Cilk(tm) Plus, and plain C implementations. A threaded MKL or IPP function might show an advantage for large enough arrays (10000?), while in-line code should be faster for small arrays (<500).
0 Kudos
Auvi_R_
Beginner
581 Views

Thanks everyone for the comments. For the time being I'm keeping a vector called ones with sufficiently large number of 1.0's and using it with cblas_ddot whenever I need to sum. As the ones vector is created only once, I am okay with it.

0 Kudos
Reply