1
votes

I try to write matlab mex code with Cuda integrated but it is just hard enough to compile and debug all around. Is there any better approach to code and test? I am on Matlab 2012b.

Currently I am writing code in sublime then compile it on matlab but I am also newbie at CUDA as well thus it is just hard to code it without seeing the result instantly.

2
It's not necessary to use the mex mechanism anymore to run CUDA code from within matlab. This non-mex technique should allow you to write independent (non-matlab) test cases to test your kernels functionality before stitching them into matlab. - Robert Crovella

2 Answers

2
votes

The comment by Robert Crovella is interesting.

I just wanted to mention the way I was used to compile mex file with CUDA instructions (and which works also on different versions of MATLAB).

1) Compile by nvcc and transform the source code in C++ code by the command

 system(sprintf('nvcc -I"%s/extern/include" -cuda "mex-fun.cu" -output-file "mexfun.cpp"', matlabroot));

2) Link it to Matlab by

mex -I/opt/cuda/include -L/opt/cuda/lib -lcudart mex-fun.cpp

This was originally suggested at the MATLAB Newsreader page.

1
votes

I have both a matlab entry point (i.e. a file with the function "mexFunction") and a C++ entry point (a file with "main"), and the rest of my actual CUDA code is independant of what entry point was used. This way, I can debug the code used for my MEX files using the standard set of CUDA tools (i.e. nvprof, cuda-memcheck, etc) without having to deal with the MEX file. Then once I'm sure I have no bugs or memory leaks, I just compile it to a MEX file. Alternately you can always just attach cuda-gdb to your MEX file, although your mileage may vary with this.