Matlab编译cuda的.cu文件

9 篇文章 0 订阅
2 篇文章 0 订阅

matlab函数,大体首先是用nvcc命令生成.o文件,然后用mex链接对应库文件,生成动态链接库(.mexw64等)。

测试环境:

1) Windows x64 + matlab + cuda 5.5 + vs2012

2) Ubuntu 12.04 amd64 server + matlab + gcc + cuda 5.5

windows用户需要根据自身编译器更改函数第10行,改为cl.exe所在文件夹。

如果第53行出错,请您在matlab中先mex -setup配置编译器。。。

使用方法e.g. 

nvmex('a.cu');


function nvmex(cuFileName)
%NVMEX Compiles and links a CUDA file for MATLAB usage
% NVMEX(FILENAME) will create a MEX-File (also with the name FILENAME) by
% invoking the CUDA compiler, nvcc, and then linking with the MEX
% function in MATLAB.


if ispc % Windows
	
	Host_Compiler_Location = '-ccbin "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\x86_amd64"';
	CUDA_INC_Location = ['"' getenv('CUDA_PATH')  '\include"'];
    CUDA_SAMPLES_Location =['"' getenv('NVCUDASAMPLES5_5_ROOT')  '\common\inc"'];
    PIC_Option = '';
    if ( strcmp(computer('arch'),'win32') ==1)
        machine_str = ' --machine 32 ';
        CUDA_LIB_Location = ['"' getenv('CUDA_PATH')  '\lib\Win32"'];
    elseif  ( strcmp(computer('arch'),'win64') ==1)
        machine_str = ' --machine 64 ';
        CUDA_LIB_Location = ['"' getenv('CUDA_PATH')  '\lib\x64"'];
    end
else % Mac and Linux (assuming gcc is on the path)
	
    CUDA_INC_Location = '/usr/local/cuda/include';
    CUDA_SAMPLES_Location = '/usr/local/cuda/samples/common/inc';
	Host_Compiler_Location = ' ';
	PIC_Option = ' --compiler-options -fPIC ';
    machine_str = [];
    if ( strcmp(computer('arch'),'win32') ==1)
        CUDA_LIB_Location = '/usr/local/cuda/lib';
    elseif  ( strcmp(computer('arch'),'win64') ==1)
        CUDA_LIB_Location = '/usr/local/cuda/lib64';
    end
end
% !!! End of things to modify !!!
[~, filename] = fileparts(cuFileName);
nvccCommandLine = [ ...
'nvcc --compile ' Host_Compiler_Location ' ' ...
'-o '  filename '.o ' ...
machine_str PIC_Option ...
' -I' '"' matlabroot '/extern/include "' ...
' -I' CUDA_INC_Location ' -I' CUDA_SAMPLES_Location ...
' "' cuFileName '" ' 
 ];
mexCommandLine = ['mex ' filename '.o'  ' -L' CUDA_LIB_Location  ' -lcudart'];
disp(nvccCommandLine);
warning off;
status = system(nvccCommandLine);
warning on;
if status < 0
	error 'Error invoking nvcc';
end
disp(mexCommandLine);
eval(mexCommandLine);
end


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值