NMAKE Makefile for MATLAB + CUDA Projects This Makefile let's you compile to a mex file without

Hi *,

I’ve been trying to get my project finally into a mex file so I can call it from MATLAB. Since I found NVMEX etc. very inflexible I created my own Makefile (NMAKE which is used by VS) which works great for me. I have it attached to this post. It is far from perfect but it works and can easily be extended.
Please, if you modify and add more functionality. Share it here I will try to keep this first post updated.

What you can do with it:
Compile your project under VS without many modifications and immediately get a mex file which you can call from MATLAB.
Use debug, emu, release.
Create an exe file for command line use in the same project.

To use it create a new VS project and choose Makefile project as project type.
Then in the following screen specify the following command line commands:

  • Rebuild/Build command line: “nmake dbg=1”
  • Clean command line: “nmake clean” (This is not yet implemented)
  • Output: Debug\yourapplication.exe

For the following release build screen similar but omit the “dbg=1” assignments.

Later then you can also create a new Configuration and call it for instance “EmuDebug” where you call nmake the following: “nmake dbg=1 emu=1”.

Your output will be in the subfolders Debug, Release and EmuDebug.

Please edit the Makefile before you use it. All sections where you have to do adjustments are marked with “TODO” so search for this. You CAN of course easily change the command line arguments how nvcc is called. For example if you want to keep immedate files or see registry, shared memory use. The Makefile is kept short and should be easy to understand.

If you cannot link because the mex file is in use run a “clear functions” command in MATLAB.

I recommend you also to use the following file structure which has proven to work very well for me:
kernel_xxx.cu which contains the kernel and a C++ wrapper function which knows how to call the kernel.
kernel_xxx.hpp this file is included by everyfile which want the call the kernels wrapper function.
main.cpp Which is the main function for the command line program (optional)
mexinterface.cpp Which is the entry point for the MATLAB mex function.

This is just how it is done in CUBLAS/CUFFT. You call a wrapper function which knows how many threads/blocks to start. This makes it easy to reuse it.

To debug your mex file just press CTRL+ALT+P and attach it your MATLAB process. Create a breakpoint in your mexFunction and it should get hit. For some reason I cannot set breakpoints in kernels when I compile EmuDebug and call the mex file. Works however in the exe file just as usual.

I hope this helps some people. Critique is welcome.


PS: I use MATLAB 7.7.0 & VS 2005 Team Suite.