Skip to content

Setup a managedCuda project

Michael Kunz edited this page Aug 1, 2015 · 2 revisions

You need: Microsoft Visual Studio 201x, Nvidia Cuda Toolkit 7.5, Nvidia Parallel Nsight for debugging and of course managedCuda.

  • Create a normal C# project (library, WinForms, WPF, etc.).
  • Add a new CudaRuntime 7.5 project to the solution. newCudaProject
  • Delete the entire Cuda sample code. To enable proper IntelliSense functionality you need to include the following header files to your *.cu file (from toolkit-include folder):
 #include <cuda.h> 
 #include <device_launch_parameters.h> 
 #include <texture_fetch_functions.h> 
 #include <builtin_types.h> 
 #include <vector_functions.h> 
 #include “float.h”
  • Also add the following defines:
 #define _SIZE_T_DEFINED 
 #ifndef __CUDACC__ 
 #define __CUDACC__ 
 #endif 
 #ifndef __cplusplus 
 #define __cplusplus 
 #endif
  • Write your kernel code in an “extern C{}” scope:
// Texture reference
texture<float2, 2> texref;
 
extern "C"  
{
	//kernel code
	__global__ void kernel(/* parameters */)
	{
		
	}
}
  • You can also omit ‘extern “C”’ in order to use templated kernels. But then kernel names get mangled (“_Z18GMMReductionKernelILi4ELb1EEviPfiPK6uchar4iPhiiiPj” instead of “GMMReductionKernel”. To look up the right mangled name open the compiled ptx file with a text editor). To load a kernel you need the full mangled name.

  • Change the following project properties of the CudaRuntime 7.5 project:

    • General:
      • Output directory: Set it to the source file directory of the C# project
      • Application type: help application. This avoids a call to the VisualC++ compiler (we have no C++ host code), no C++ output will be created. ProjectProperties1_2
    • CUDA C/C++:
      • Compiler Output: $(OutDir)%(FileName).ptx or .cubin
      • NVCC Compilation Type: “Generate .ptx file (-ptx)” or “Generate .cubin file (-cubin)” respectively ProjectProperties2_2
    • You need to set these properties for all possible targets and configurations (x86/x64, Debug/Release). To handle mixed mode platform kernels, give a different kernel name for x86 and x64, for example $(OutDir)%(FileName)_x86.ptx and $(OutDir)%(FileName)_x64.ptx.
  • Delete the post build event: We don’t need the CUDA runtime libraries copied. DeletePostbuild_2

  • Build the Cuda project once for each platform.

  • In the C# project add the newly build kernel files in the C# project source directory to the project. Set the file properties either to embedded resource (access files by stream (byte[]) when loading kernel images) or set “copy to output directory” to “always” and load the kernel image from file.

  • Add a reference to the managedCuda assembly.