title: Cuda hello world example
body: <p>I'm trying to understand a simple addition within the hello world CUDA example.
  I have two arrays:</p>
  
  <pre><code>char a[N] = "Hello \0\0\0\0\0\0";
  int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  </code></pre>
  
  <p>The grid and block dimension are 1 and 16.
  I dont really get how when you perform this:</p>
  
  <pre><code>a[threadIdx.x] += b[threadIdx.x];
  </code></pre>
  
  <p>you get "Hello World!".
  This is a classical introductory example in CUDA and the logic behind parallelism it's easy to understand, but this sum...I don't really get it.
  For full source code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  const int N = 16; 
  const int blocksize = 16; 
  
  __global__ 
  void hello(char *a, int *b) 
  {
      a[threadIdx.x] += b[threadIdx.x];
  }
  
  int main()
  {
      char a[N] = "Hello \0\0\0\0\0\0";
      int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  
      char *ad;
      int *bd;
      const int csize = N*sizeof(char);
      const int isize = N*sizeof(int);
  
      printf("%s", a);
  
      cudaMalloc( (void**)&amp;ad, csize ); 
      cudaMalloc( (void**)&amp;bd, isize ); 
      cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice ); 
      cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice ); 
  
      dim3 dimBlock( blocksize, 1 );
      dim3 dimGrid( 1, 1 );
      hello&lt;&lt;&lt;dimGrid, dimBlock&gt;&gt;&gt;(ad, bd);
      cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost ); 
      cudaFree( ad );
      cudaFree( bd );
  
      printf("%s\n", a);
      sleep(1);
      return EXIT_SUCCESS;
  }
  </code></pre>