title: CUDA program fails to execute correctly despite that deviceQuery test passed
body: <p>I have just installed the nvidia CUDA toolkit on my fresh Ubuntu 20.04 installation. Nvcc compiles CUDA programs, and they run without crashing. However, none of the results are correct.</p>
  <p>Here is the output of the test script (deviceQuery) that Nvidia provides:</p>
  <pre><code>./deviceQuery Starting...
  
   CUDA Device Query (Runtime API) version (CUDART static linking)
  
  Detected 1 CUDA Capable device(s)
  
  Device 0: &quot;NVIDIA GeForce GTX 770&quot;
    CUDA Driver Version / Runtime Version          11.4 / 11.4
    CUDA Capability Major/Minor version number:    3.0
    Total amount of global memory:                 1997 MBytes (2093875200 bytes)
    (008) Multiprocessors, (192) CUDA Cores/MP:    1536 CUDA Cores
    GPU Max Clock rate:                            1110 MHz (1.11 GHz)
    Memory Clock rate:                             3505 Mhz
    Memory Bus Width:                              256-bit
    L2 Cache Size:                                 524288 bytes
    Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
    Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
    Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
    Total amount of constant memory:               65536 bytes
    Total amount of shared memory per block:       49152 bytes
    Total shared memory per multiprocessor:        49152 bytes
    Total number of registers available per block: 65536
    Warp size:                                     32
    Maximum number of threads per multiprocessor:  2048
    Maximum number of threads per block:           1024
    Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
    Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
    Maximum memory pitch:                          2147483647 bytes
    Texture alignment:                             512 bytes
    Concurrent copy and kernel execution:          Yes with 1 copy engine(s)
    Run time limit on kernels:                     Yes
    Integrated GPU sharing Host Memory:            No
    Support host page-locked memory mapping:       Yes
    Alignment requirement for Surfaces:            Yes
    Device has ECC support:                        Disabled
    Device supports Unified Addressing (UVA):      Yes
    Device supports Managed Memory:                Yes
    Device supports Compute Preemption:            No
    Supports Cooperative Kernel Launch:            No
    Supports MultiDevice Co-op Kernel Launch:      No
    Device PCI Domain ID / Bus ID / location ID:   0 / 2 / 0
    Compute Mode:
       &lt; Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) &gt;
  
  deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.4, CUDA Runtime Version = 11.4, NumDevs = 1 
  Result = PASS
  </code></pre>
  <p>And here is the very simple vector addition program I am trying to run:</p>
  <pre><code>#include &lt;cuda_runtime.h&gt;
  
  #include &lt;iostream&gt;
  #include &lt;cuda.h&gt;
  using namespace std;
  
  int *a, *b;  // host data
  int *c;  // results
  
  __global__ void vecAdd(int *A,int *B,int *C)
  {
     int i = blockIdx.x * blockDim.x + threadIdx.x;
     C[i] = A[i] + B[i];
  }
  
  int main(int argc,char **argv)
  {
     printf(&quot;Begin \n&quot;);
     int n=1000000;
     int nBytes = n*sizeof(int);
     int block_size, block_no;
     a = (int *)malloc(nBytes);
     b = (int *)malloc(nBytes);
     c = (int *)malloc(nBytes);
     int *a_d,*b_d,*c_d;
     block_size=1000;
     block_no = n/block_size;
     for(int i=0;i&lt;n;i++) {
        a[i]=i;
        b[i]=i;
     }
  
     printf(&quot;Allocating device memory on host..\n&quot;);
     cudaMalloc((void **)&amp;a_d,n*sizeof(int));
     cudaMalloc((void **)&amp;b_d,n*sizeof(int));
  
     printf(&quot;Copying to device..\n&quot;);
     cudaMemcpy(a_d,a,n*sizeof(int),cudaMemcpyHostToDevice);
     cudaMemcpy(b_d,b,n*sizeof(int),cudaMemcpyHostToDevice);
  
     printf(&quot;Doing GPU Vector add\n&quot;);
     vecAdd&lt;&lt;&lt;block_no,block_size&gt;&gt;&gt;(a_d,b_d,c_d);
  
     cudaMemcpy(c,c_d,n*sizeof(int),cudaMemcpyDeviceToHost);
     for(int i = 0; i &lt; 10; i++) {
          std::cout &lt;&lt; a[i] &lt;&lt; &quot; + &quot; &lt;&lt; b[i] &lt;&lt; &quot; = &quot; &lt;&lt; c[i] &lt;&lt; std::endl;
     }
     cudaFree(a_d);
     cudaFree(b_d);
     cudaFree(c_d);
     free(a);
     free(b);
     free(c);
     return 0;
  }
  
  </code></pre>
  <p>And last but not least, here is it's faulty output:</p>
  <pre><code>
  Begin
  Allocating device memory on host..
  Copying to device..
  Doing GPU Vector add
  0 + 0 = 0
  1 + 1 = 0
  2 + 2 = 0
  3 + 3 = 0
  4 + 4 = 0
  5 + 5 = 0
  6 + 6 = 0
  7 + 7 = 0
  8 + 8 = 0
  9 + 9 = 0
  </code></pre>
  <p>Any help is greatly appreciated.</p>