cublasDasum returns incorrect results

Hi,

I am trying to use cuBLAS function “cublasDasum” to compute sum of an array with 15 elements. The code gets compiled without any error. But the value of sum returned is incorrect. Please find the code below:

    const int NUM = 15;
    int main(int argc,char **argv) {

FILE *f1 = fopen("data.txt","r");

// Host side declarations
double *h_datain = (double*) malloc(NUM*sizeof(double));
double *h_acc_sum = (double*) malloc(1*sizeof(double));
unsigned int i;

// Device side declarations
double *d_datain, *d_acc_sum;
cudaMalloc((void **) &d_datain,NUM*sizeof(double));
cudaMalloc((void **) &d_acc_sum,1*sizeof(double));

// Read from file
    if(f1==NULL) return 1;
    for(i = 0; i < NUM; ++i) {
	fscanf(f1, "%lf",&h_datain[i]);
	printf("\n h_datain[%d]=%.6f\n",i,h_datain[i]);}

    cudaMemcpy(d_datain, h_datain, NUM*sizeof(double), cudaMemcpyHostToDevice);

   // Accumulated sum calculation using CUBLAS
   cublasStatus_t ret;
   cublasHandle_t handle;
   ret = cublasCreate(&handle);
   ret = cublasDasum(handle, NUM, d_datain, 1, h_acc_sum);
   cudaDeviceSynchronize();
   printf("\n Sum of elements using CUBLAS = %.6f\n", *h_acc_sum);
   cublasDestroy(handle);
return 0;

}

Filename: data.txt

4.44
6.92
1.10
3.79
7.66
7.23
9.37
5.23
1.94
0.88
2.41
1.76
1.53
-0.30
-3.42

Sum = 57.98 (returned by cublas function)

Expected sum = 50.539

What is the reason behind this incorrect result and how to resolve it?

read the documentation:

http://docs.nvidia.com/cuda/cublas/index.html#cublas-lt-t-gt-asum

which says:

“This function computes the sum of the absolute values of the elements of vector x.”

So the negative values in your input vector are treated as if they were positive values, for the purpose of this sum. That’s why the function is named asum and not just sum.

Does this mean using cuBLAS library, computation of sum of an array with positive and negative numbers is not feasible?

The documentation has functions to compute absolute sum of an array (with single precision,double precision and complex numbers only)

I’m sure there are a number of ways to compute the ordinary sum of an array, however I’m not aware of a BLAS function that does exactly that.

One way to do this is with the dot function. The dot product of your array with a vector of 1’s.

https://lists.gnu.org/archive/html/help-gsl/2006-05/msg00014.html