error LNK2019: unresolved external symbol when trying to MEX a C++ file containing CUDA

7 次查看(过去 30 天)
I’m trying to mex a C++ file that uses CUDA. I have included the folder containing cublas.h etc when calling mex and I’ve linked to the library in my CUDA toolbox folder.
mex -v NIPALS_gpu.cpp -I'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include' -L'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64'
but when I run it, it produces the error:
Error using mex
Creating library NIPALS_gpu.lib and object NIPALS_gpu.exp
NIPALS_gpu.obj : error LNK2019: unresolved external symbol cublasSetVector referenced in function "void __cdecl cublasSnormalize(int,int,float *,float
*,float *)" (?cublasSnormalize@@YAXHHPEAM00@Z)
.
And about 10 other similar LNK2019 errors.
Does anyone have experience in mexing CUDA and know what the error is about? Do I need to include some additional file that I’ve missed? The file I’m trying to mex ( NIPALS_gpu.cpp) is not something I’ve written, its available online and likely does not contain any errors.

回答(1 个)

Joss Knight
Joss Knight 2016-10-3
mex on its own does not automatically link in the CUDA libraries, so you will have to do that manually using -lcublas or similar. Your -L switch has added a library path but hasn't actually added any libraries.
Easier still is to use the MEX wrapper mexcuda which both automatically links in the CUDA libraries, and links in the GPU mex interface so that you can pass MATLAB gpuArray objects into your mex function. (Obviously, this is only available with Parallel Computing Toolbox.)
  2 个评论
Bob Khosh
Bob Khosh 2018-9-15
I do use "mexcuda" but i still get the following error:
Error using mex
Creating library mexMatrixMultiplication.lib and object
mexMatrixMultiplication.exp
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasCreate_v2 referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasDestroy_v2 referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasSetMatrix referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasGetMatrix referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasDgemm_v2 referenced in function
mexFunction
mexMatrixMultiplication.mexw64 : fatal error LNK1120: 5
unresolved externals
Error in mexcuda (line 157)
[varargout{1:nargout}] = mex(mexArguments{:});
Bowen Jia
Bowen Jia 2020-2-8
编辑:Bowen Jia 2020-2-8
Hi, Joss. I met the same question as Bob, is there anyway now to resolve this?
okay, I got this. Just like Joss said, add the "-lcublas" like ''mexcuda matrixmultiplacaiton.cu -lcublas''

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by