title: Understanding Cublas: vector addition (asum)
body: <p>According to <a href="http://docs.nvidia.com/cuda/cublas/index.html#topic_6_3" rel="nofollow">CUBLAS reference</a>, asum function (for getting the sum of the elements of a vector) is:</p>
  
  <pre><code>cublasStatus_t  cublasSasum(cublasHandle_t handle, int n, const float *x, int incx, float *result)
  </code></pre>
  
  <p>You can see in the link to the reference the parameters explanation, roughly we have a vector <code>x</code> of <code>n</code> elements with <code>incx</code> distance between elements.</p>
  
  <p>My code is (quite simplified, but I also tested this one and there is still the error):</p>
  
  <pre><code>int arraySize = 10;
  float* a = (float*) malloc (sizeof(float) * arraySize);
  
  float* d_a;
  cudaMalloc((void**) &amp;d_a, sizeof(float) * arraySize);
  
  for (int i=0; i&lt;arraySize; i++)
      a[i]=0.8f;
  
  cudaMemcpy(d_a, a, sizeof(float) * arraySize, cudaMemcpyHostToDevice);
  
  cublasStatus_t ret;  
  cublasHandle_t handle;
  ret = cublasCreate(&amp;handle);
  
  float* cb_result = (float*) malloc (sizeof(float));
  
  ret = cublasSasum(handle, arraySize, d_a, sizeof(float), cb_result);
  
  printf("\n\nCUBLAS: %.3f", *cb_result);
  
  cublasDestroy(handle);
  </code></pre>
  
  <p>I have removed error checking for simplifying the code (there are no errors, CUBLAS functions return <code>CUDA_STATUS_SUCCESS</code>) and <code>free</code> and <code>cudaFree</code>.</p>
  
  <p>It compiles, it runs, it doesn't throw any error, but result printed is <code>0</code>, and, debugging, it is actually <code>1.QNAN</code>.</p>
  
  <p>What did i miss?</p>