I have Windows XP 64 bit, MATLAB R2012a( R2010a is also available here, but not installed), VS 2010 (VS 2008/VS 2012 is also available, but not installed) and CUDA 5.0 installed..
Is it possible to compile CUDA codes with these in MATLAB to create a mex file? Can anyone help with the necessary steps or any thing further need to be installed??
Thanks in advance
I noticed that in Matlab2015b, you can compile .cu file like below:
which I've been using for a while, and it's much easier! Hope this could help you, though it might not help a lot since you are not using 2015b.
I would suggest using the procedure in Compiling CUDA mex files with Visual Studio:
The procedure has been tested for CUDA 5.0, Visual Studio 2010 and Matlab 2010a/2012b, but perhaps it could be of interest also to people using other versions of the above products.
Using MATLAB 2013
If you can upgrade to 2013 read on, otherwise go to the bottom of the page for some suggestions for 2012 version. Even if you are not planning to upgrade, still read the first section, it might give you some clues.
I have always had issues using MATLAB 2012 to compile MEX functions. Upgrading to 2013 made it very easy for me to compile CUDA MEX files without worrying about invoking NVCC inside MATLAB with all the crazy flags.
All you would need is to copy the
mexopts.bat
file from your MATLAB installation:matlabroot\toolbox\distcomp\gpu\extern\src\mex\win64\mexopts.bat
2 (where matlabroot is the MATLAB installation folder, something like:C:\Program Files\MATLAB\Matlab2013a
.)to your project folder containing the
.cu
file withmex
entry point function. In addition, you would need to add two CUDA libraries:cuda.lib
andcudart.lib
to the same folder from the CUDA installation folder. I have included these two libs in my project, they should also be available somewhere in your CUDA Toolkit installation folder.Then you would go to MATLAB and run
mex -setup
to select a compiler. Since you have Visual Studio 2010 installed you should see it in the list. Just follow the instructions on the screen and select your compiler -- more details. If you don't see the compiler or things go wrong, you can try installing the Windows SDK. Also look at this post about compilers and the version of Visual Studio.If you want to see your MEX compiles checkout my project. Download the content and put them in a folder somewhere in your computer and open up MATLAB. Then go to that directory and in MATLAB confirm that you are indeed in that directory by using
pwd
in the command window; it will tell you if you are in that directory, but make sure that the folder and the subfolders are added to the file path (right click on the folder in MATLAB and select add path > folder and subfolders)Then, run
mex f.cu
. This will compile the CUDA MEX file and puts the library in the same folder, and you can callf
as a function in MATLAB and MATLAB won't care where it came from. To See if it actually works, you can make agpuArray
and call thef
function on it. Like this:You would get something like this:
Suggestions for compiling MEX files in MATLAB 2012:
I have never had success compiling MEX files on MATLAB 2012, 64bit Windows, even with Visual Studio 2010 Professional. But to start, you can look at this doc page for MEX files and CUDA. Also look at this page for some general information about compiling MEX files in MATLAB, it might give you some ideas of the things involved. Once you went through these pages, look at NVCC compiler and how to invoke it properly in MATLAB. I has always been difficult to get it right for me and I never had success. But don't get discouraged. The key is to see if you can compile basic C or C++ files using a compiler in MATLAB. And once that is successful, move onto compiling CUDA. If nothing works, check this post out about compiling directly in Visual Studio. Good luck !
References:
2: http://www.mathworks.com/help/distcomp/create-and-run-mex-files-containing-cuda-code.html