很多后来的同学还是不知道怎么编译CUDA 到mexw文件。
首先先移步至: http://bbs.gpuworld.cn/forum.php?mod=viewthread&tid=9309&extra=page%3D1
了解了CUDA matlab加速的各种工具
下面再分享下:
方法1 利用nvmex: http://www.matlabsky.com/thread-27230-1-5.html
请重点看: http://www.matlabsky.com/thread-25951-1-4.html
http://www.matlabsky.com/thread-36877-1-4.html
程序代码: [选择]if ispc % Windows
CUDA_LIB_Location = 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\lib\Win32'; %CUDA LIB PATH
Host_Compiler_Location = '-ccbin "D:\Program Files\Microsoft Visual Studio 11.0\VC\bin"'; %VS BIN PATH
PIC_Option = '';
aa=' -I"D:\Program Files\MATLAB\R2013b\extern\include" ';
else % Mac and Linux (assuming gcc is on the path)
CUDA_LIB_Location = '/usr/local/cuda/lib64';
Host_Compiler_Location = '';
PIC_Option = ' --compiler-options -fPIC ';
end
% !!! End of things to modify !!!
[path,filename,zaet] = fileparts(cuFileName);
nvccCommandLine = [ ...
'nvcc -gencode arch=compute_20,code=sm_20 --compile ' cuFileName ' ' ' ' Host_Compiler_Location ' ' ...
' -o ' filename '.o ' ...
PIC_Option ...
aa ...
];
mexCommandLine = ['mex (''' filename '.o'', ''-L' CUDA_LIB_Location ''', ''-lcudart'',''-lcuda'',''-lcufft'', ''-lcublas'')'];
disp(nvccCommandLine);
status = system(nvccCommandLine);
if status < 0
error 'Error invoking nvcc';
end
disp(mexCommandLine);
eval(mexCommandLine);
end
程序代码: [选择]1.首先将nvmex.pl放在maltab的bin目录下D:\Program Files\MATLAB\R2012a\bin
2.修改nvmexopts.bat:
set VS100COMNTOOLS=%VS100COMNTOOLS% (在系统环境变量中)
set VSINSTALLDIR=D:\Program Files\Microsoft Visual Studio 10.0(vs 路径)
set VS100COMNTOOLS=%VS100COMNTOOLS%
以下将2.0改为3.5(vs2010是3.5,vs2008是2.0)
set
PATH=%VCINSTALLDIR%\BIN\;%VCINSTALLDIR%\PlatformSDK\bin;%VSINSTALLDIR%\Common7\IDE;%VSINSTALLDIR%\SDK\v3.5\bin;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\Tools\bin;%VCINSTALLDIR%\VCPackages;%MATLAB_BIN%;%PATH%
set
INCLUDE=%VCINSTALLDIR%\ATLMFC\INCLUDE;%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\PlatformSDK\INCLUDE;%VSINSTALLDIR%\SDK\v3.5\include;%INCLUDE%
set
LIB=%VCINSTALLDIR%\ATLMFC\LIB;%VCINSTALLDIR%\LIB;%VCINSTALLDIR%\PlatformSDK\lib;%VSINSTALLDIR%\SDK\v3.5\lib;%MATLAB%\extern\lib\win32;%LIB%
设置SDK 否则有可能会出现找不到kernel32.lib(vs2010是7.0,vs2008是6.0)
set SDK=c:/Program Files/Microsoft SDKs/Windows/v7.0A
对于VS2012 :
set SDK=C:/Program Files/Windows Kits/8.0
set PATH=%SDK%\Bin;%PATH%
set INCLUDE=%SDK%\Include;%INCLUDE%
set LIB=%SDK%\Lib\win8\um\x86;%LIB%
如果是编译64位 请设置 :set MW_TARGET_ARCH=x64
所有用到的lib 都请用64的。
方法2 利用VS :