问题描述
我有Windows XP 64位,MATLAB R2012a(R2010a也可以在这里,但没有安装),VS 2010(VS 2008 / VS 2012也可以,但没有安装)和CUDA 5.0安装..
是否可以在MATLAB中使用这些代码编译CUDA代码来创建一个mex文件?
提前感谢
使用MATLAB 2013
如果您可以升级到2013年,否则请转到页面底部以获得有关2012版本的一些建议。即使您不打算升级,仍然阅读第一部分,它可能会给您一些线索。
使用MATLAB 2012编译MEX函数。升级到2013使我很容易编译CUDA MEX文件,而不必担心在MATLAB中使用所有疯狂的标志调用NVCC。
所有你需要的是复制 mexopts.bat
文件从您的MATLAB安装:
matlabroot \toolbox\distcomp \gpu\extern\src\mex\win64\mexopts.bat
(其中matlabroot是MATLAB安装文件夹,类似: C:\ ProgramProgram Files \MATLAB\Matlab2013a
。)
到您的项目文件夹包含 .cu
文件,具有 mex
入口点函数。此外,您需要向同一文件夹添加两个CUDA库: cuda.lib
和 cudart.lib
的CUDA安装文件夹。我已将这两个库包含在我的页的MEX文件和CUDA。另请参阅此,了解有关在MATLAB中编译MEX文件,它可能给你一些涉及的事情的想法。一旦你浏览了这些页面,看看NVCC编译器和如何在MATLAB中正确调用它。我一直很难得到它对我来说,我从来没有成功。但不要气馁。关键是看看你是否可以使用MATLAB中的编译器编译基本的C或C ++文件。一旦成功,转到编译CUDA。如果无效,请在Visual Studio中直接查看此。祝你好运!
参考文献:
2:
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
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
(where matlabroot is the MATLAB installation folder, something like:C:\Program Files\MATLAB\Matlab2013a
.)
to your project folder containing the .cu
file with mex
entry point function. In addition, you would need to add two CUDA libraries: cuda.lib
and cudart.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 call f
as a function in MATLAB and MATLAB won't care where it came from. To See if it actually works, you can make a gpuArray
and call the f
function on it. Like this:
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
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
这篇关于从CUDA代码创建mex文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!