Tutorial MATLAB CUDA without nvmex and Parallel Computing Toolbox

With the following steps you can build your CUDA kernel and use it in MATLAB without nvmex (deprecated) and “Parallel Computing Toolbox” (available in MATLAB 2010b or above); I prefer the following way than use parallel toolbox because this last is not cheap and I hate the MATLAB way to manage CUDA via parallel toolbox (new ugly syntax, 8xxx and 9xxx are not supported and more).

First step

From Visual Studio create your new project but instead of console application you should select static lib and set it to empty project;

Second step

Add cu and cuh files to your project, in cuh create a function prototype that you can call from host (host function) like

extern void CallKernel();

It is essentials to use extern instead of extern C!! with extern C there is no way to call CallKernel from mex.

In cu file add your kernel function (global) and code for host function CallKernel like

__global__ void Kernel()

{

}

Void CallKernel()

{

	Kernel<<<1, 1>>>();

}

Third step

Build your CUDA project and copy your lib to mex path file;

Fourth step

Add at top of mex file.

extern void KernelCall();

Now you can call KernelCall from your CUDA lib file in mex file.

Fifth step

Build your mex and link to it all libs that you need, from MATLAB

mex file.mex cuda.lib ‘C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\lib\x64\cudart.lib’

In my sample I need only cudart.lib but if you need other libs you should add them like above.

Last, but no less important, if you use MATLAB x64 you should configure CUDA compiler in Visual Studio to x64!

I have tested this trick on:

  • Windows Server 2008 R2;

  • MATLAB 2010a;

  • Visual Studio 2008;

  • Parallel NSight 1.5.1

  • CUDA 3.2.

I hope if this trick help someone out there that want to use CUDA without “Parallel Computing Toolbox”.

Cheers

You could also use cmake. That way you don’t need to do any of these steps, but cmake does them for you. It’s also portable between linux and windows (32 and 64 bit). http://forums.nvidia.com/index.php?showtopic=152839

Good to know :D

Cheers