title: Basic CUDA - getting kernels to run on the device using C++
body: <p>I'm new to CUDA &amp; trying to get a basic kernel to run on the device.  I have compiled the examples &amp; then run so I know the device drivers work/CUDA can run successfully.  My goal is to get my C++ code to call CADU to greatly speed up a task.  I've been reading over a bunch of different posts online about how to do this.  Specifically, [here]: <a href="https://stackoverflow.com/questions/3811539/can-i-call-cuda-function-calls-in-c">Can I call CUDA runtime function from C++ code not compiled by nvcc?</a>.</p>
  
  <p>My question is very simple (embracingly so) when I compile &amp; run my code (posted below) I get no errrors but the kernel does not appear to run.  This should be trivial to fix but after 6 hours I'm at a loss.  I'd post this on the NVIDIA forums but they're still down :/.  I'm sure the answer is very basic - any help?  Below is: my code, how I compile it, &amp; the  terminal outputs I see:</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  extern void kernel_wrapper(int *a, int *b);
  
  int main(int argc, char *argv[]){
  int a = 2;
  int b = 3;
  
  printf("Input: a = %d, b = %d\n",a,b);
  kernel_wrapper(&amp;a, &amp;b);
  printf("Ran: a = %d, b = %d\n",a,b);
  return 0;
  }
  </code></pre>
  
  <p>kernel.cu</p>
  
  <pre><code>#include "cuPrintf.cu"
  #include &lt;stdio.h&gt;
  __global__ void kernel(int *a, int *b){
  int tx = threadIdx.x;
  cuPrintf("tx = %d\n", tx);
  switch( tx ){
    case 0:
      *a = *a + 10;
      break;
    case 1:
      *b = *b + 3;
      break;
    default:
      break;
    }
  }
  
  void kernel_wrapper(int *a, int *b){
    cudaPrintfInit();
    //cuPrintf("Anything...?");
    printf("Anything...?\n");
    int *d_1, *d_2;
    dim3 threads( 2, 1 );
    dim3 blocks( 1, 1 );
  
    cudaMalloc( (void **)&amp;d_1, sizeof(int) );
    cudaMalloc( (void **)&amp;d_2, sizeof(int) );
  
    cudaMemcpy( d_1, a, sizeof(int), cudaMemcpyHostToDevice );
    cudaMemcpy( d_2, b, sizeof(int), cudaMemcpyHostToDevice );
  
    kernel&lt;&lt;&lt; blocks, threads &gt;&gt;&gt;( a, b );
    cudaMemcpy( a, d_1, sizeof(int), cudaMemcpyDeviceToHost );
    cudaMemcpy( b, d_2, sizeof(int), cudaMemcpyDeviceToHost );
    printf("Output: a = %d\n", a[0]);
    cudaFree(d_1);
    cudaFree(d_2);
  
    cudaPrintfDisplay(stdout, true);
    cudaPrintfEnd();
  }
  </code></pre>
  
  <p>I compile the above code from the terminal using the commands:</p>
  
  <pre><code>g++ -c main.cpp
  nvcc -c kernel.cu -I/home/clj/NVIDIA_GPU_Computing_SDK/C/src/simplePrintf
  nvcc -o main main.o kernel.o
  </code></pre>
  
  <p>When I run the code I get the following terminal output:</p>
  
  <pre><code>$./main
  Input: a = 2, b = 3
  Anything...?
  Output: a = 2
  Ran: a = 2, b = 3
  </code></pre>
  
  <p>It's clear that the main.cpp is being compiled correctly &amp; calling the kernel.cu code.  The obvious problem is that the kernel does not appear to run.  I'm sure the answer to this is basic - VERY VERY BASIC.  But I don't know what's happening - help please?</p>