How to integrate CUDA and C++

Since I just installed the WordPress Plugin for using the Google Syntax Highlighter, I thought it would be a good opportunity to actually post a little bit of code in order to check it out.

For certain reasons I recently needed to wrap my head around integrating CUDA into an existing application that was written in C++. As mostly this turned out to be fairly easy, once you have discovered about ten ways how to do it wrong…

CUDA is written as an extension to the C-Programming Language. That means if you want to call your kernel functions from your C++ host code you need to put all the CUDA routines into separate .cu-files and access them using wrapper-functions. This really isn’t as complicated as it may sound.

Put the following snippet into your main.cpp.

// prototype wrapper function
extern "C" 
void launch();

int main(int argc, char **argv)
{
  // 1. prepare input data
  // 2. call the kernel wrapper function
  launch();
  // 3. process result and free input data
  return EXIT_SUCCESS;
}

Add another file and give it the extension “.cu”. The extension is important, because the CUDA-code is compiled with a different compiler. To determine which file gets compiled with VC and which one with NVCC, the build rule uses the extension of the source file.

#ifndef __Kernel_CU__
#define __Kernel_CU__

__global__ void 
kernel()
{
  // do all sorts of kernel crazyness 
}

extern "C" 
void launch()
{
  // 1. alloc device memory
  // 2. transfer data from host to device
  // 3. configure and launch the kernel
  dim3 grid(8, 8);
  dim3 block(1, 1);
  kernel(grid, block)();
  // 4. transfer data back from device to host
  // 5. free device memory
}

#endif

Please note, that line 18 in the above snippt should be written with triple angle brackets (<<< >>>) as explained in the CUDA programming guide, but unfortunately that kind of syntax messes up the plugin.

Well thats it. Enjoy…

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>