Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Using MATLAB 2013</strong></p> <p><strong><em>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.</em></strong></p> <p>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.</p> <p>All you would need is to copy the <code>mexopts.bat</code> file from your MATLAB installation: </p> <p><code>matlabroot\toolbox\distcomp\gpu\extern\src\mex\win64\mexopts.bat</code><sup>2</sup> (where matlabroot is the MATLAB installation folder, something like:<code>C:\Program Files\MATLAB\Matlab2013a</code>.)</p> <p>to your project folder containing the <code>.cu</code> file with <code>mex</code> entry point function. In addition, you would need to add two CUDA libraries: <code>cuda.lib</code> and <code>cudart.lib</code> to the same folder from the CUDA installation folder. I have included these two libs in my <a href="http://goo.gl/WHSmTD" rel="nofollow noreferrer">project</a>, they should also be available somewhere in your CUDA Toolkit installation folder.</p> <p>Then you would go to MATLAB and run <code>mex -setup</code> 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 -- <a href="http://www.mathworks.com/help/distcomp/create-and-run-mex-files-containing-cuda-code.html" rel="nofollow noreferrer">more details</a>. If you don't see the compiler or things go wrong, you can try installing the <a href="http://www.mathworks.com/support/compilers/R2012a/win64.html" rel="nofollow noreferrer">Windows SDK</a>. Also look at this <a href="https://stackoverflow.com/questions/16159970/matlab-mex-file-compile?rq=1">post</a> about compilers and the version of Visual Studio.</p> <p>If you want to see your MEX compiles checkout my <a href="http://goo.gl/WHSmTD" rel="nofollow noreferrer">project</a>. 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 <code>pwd</code> 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)</p> <p>Then, run <code>mex f.cu</code>. This will compile the CUDA MEX file and puts the library in the same folder, and you can call <code>f</code> as a function in MATLAB and MATLAB won't care where it came from. To See if it actually works, you can make a <code>gpuArray</code> and call the <code>f</code> function on it. Like this:</p> <pre><code>input = gpuArray.ones(100,100); % makes an array of ones (100x100) on the gpu. y = f(input) % will perform the operation defined in f.cu </code></pre> <p>You would get something like this:</p> <p><img src="https://i.stack.imgur.com/Q9DLK.png" alt="enter image description here"></p> <p><strong>Suggestions for compiling MEX files in MATLAB 2012:</strong></p> <p>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 <a href="http://www.mathworks.com/help/distcomp/create-and-run-mex-files-containing-cuda-code.html" rel="nofollow noreferrer">page</a> for MEX files and CUDA. Also look at this <a href="http://www.mathworks.com/help/matlab/matlab_external/building-mex-files.html" rel="nofollow noreferrer">page</a> 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 <a href="http://www.orangeowlsolutions.com/?s=mex" rel="nofollow noreferrer">post</a> out about compiling directly in Visual Studio. Good luck !</p> <p>References:</p> <p>2: <a href="http://www.mathworks.com/help/distcomp/create-and-run-mex-files-containing-cuda-code.html" rel="nofollow noreferrer">http://www.mathworks.com/help/distcomp/create-and-run-mex-files-containing-cuda-code.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload