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?