Compiling CUDA in matlab.

Hi.

I have some problems compiling CUDA in matlab. I’m using a small scipt, which looks like this:

function compile(varargin)

!"%VS90COMNTOOLS%vsvars32.bat" & nvcc -c cudaKernel.cu

n=getenv('CUDA_LIB_PATH'); if n(1)=='"', n=n(2:end); end, if n(end)=='"', n=n(1:end-1);end

mex(['-L' n],'-lcudart','-lcufft','cudaKernel.obj',varargin{:});

When I run this I get the error:

>> compile cudaKernel.cpp

Setting environment for using Microsoft Visual Studio 2008 x86 tools. 

cudaKernel.cu 

cudaKernel.cu(180): warning: variable "dreal" was declared but never referenced 

cudaKernel.cu(180): warning: variable "dimagin" was declared but never referenced 

tmpxft_00000804_00000000-3_cudaKernel.cudafe1.gpu 

tmpxft_00000804_00000000-8_cudaKernel.cudafe2.gpu 

cudaKernel.cu(180): warning: variable "dreal" was declared but never referenced 

cudaKernel.cu(180): warning: variable "dimagin" was declared but never referenced 

tmpxft_00000804_00000000-3_cudaKernel.cudafe1.cpp 

tmpxft_00000804_00000000-13_cudaKernel.ii 

Microsoft (R) Incremental Linker Version 9.00.21022.08 

Copyright (C) Microsoft Corporation.  All rights reserved. 

cudaKernel.obj C:\DOCUME~1\MER\LOCALS~1\Temp\mex_dGc1FB\cudaKernel.obj  

MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __cdecl type_info::type_info(class type_info const &)" (??0type_info@@AEAA@AEBV0@@Z) already defined in LIBCMT.lib(typinfo.obj) 

MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __cdecl type_info::operator=(class type_info const &)" (??4type_info@@AEAAAEAV0@AEBV0@@Z) already defined in LIBCMT.lib(typinfo.obj) 

   Creating library C:\DOCUME~1\MER\LOCALS~1\Temp\mex_dGc1FB\templib.x and object C:\DOCUME~1\MER\LOCALS~1\Temp\mex_dGc1FB\templib.exp 

LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library 

cudaKernel.mexw64 : fatal error LNK1169: one or more multiply defined symbols found 

C:\PROGRA~1\MATLAB\R2008A\BIN\MEX.PL: Error: Link of 'cudaKernel.mexw64' failed. 

??? Error using ==> mex at 207

Unable to complete successfully.

Error in ==> compile at 4

mex(['-L' n],'-lcudart','-lcufft','cudaKernel.obj',varargin{:});

In my cudaKernel.cpp I have a declaration of the function in cudaKernel.cu I want to call, with the extern “C” flag. I have no idea why this won’t work. Maybe it has something with the fact that I can’t compile in cmd. Using nvcc yealds:

c:\cuda\include\host_config.h(115) : fatal error C1083: Cannot open include file: ‘crtdefs.h’: No such file or directory

I’m in the dark here, and would appreciate any help.

Btw. The nvmex function won’t work either. When using nvmex I get:

>> nvmex -f nvmexopt_9.bat cudaKernel.cu

abdelali target arch: win64 

C:\PROGRA~1\MATLAB\R2008A\BIN\NVMEX.PL: Error: Could not find specified options file 

	'nvmexopt_9.bat'. 

??? Error using ==> nvmex at 206

Unable to complete successfully.

>> nvmex -f nvmexopts_9.bat cudaKernel.cu

abdelali target arch: win64 

cudaKernel.cu 

Z:/tekst/MATLAB/ISAR_Tests/cudaKernel.cu(180): warning: variable "dreal" was declared but never referenced 

Z:/tekst/MATLAB/ISAR_Tests/cudaKernel.cu(180): warning: variable "dimagin" was declared but never referenced 

tmpxft_000009fc_00000000-3_cudaKernel.cudafe1.gpu 

tmpxft_000009fc_00000000-8_cudaKernel.cudafe2.gpu 

Z:/tekst/MATLAB/ISAR_Tests/cudaKernel.cu(180): warning: variable "dreal" was declared but never referenced 

Z:/tekst/MATLAB/ISAR_Tests/cudaKernel.cu(180): warning: variable "dimagin" was declared but never referenced 

tmpxft_000009fc_00000000-3_cudaKernel.cudafe1.cpp 

Microsoft (R) Incremental Linker Version 9.00.21022.08 

Copyright (C) Microsoft Corporation.  All rights reserved. 

cudaKernel.obj  

LINK : fatal error LNK1181: cannot open input file 'kernel32.lib' 

C:\PROGRA~1\MATLAB\R2008A\BIN\NVMEX.PL: Error: Link of 'cudaKernel.mexw64' failed. 

??? Error using ==> nvmex at 206

Unable to complete successfully.

Actually I don’t care which of them you know how to fix, ass long as one of them will work, I’ll be happy.

I’m getting closer now. When I’m trying to compile now (with nvmex), I get the errors:

tmpxft_00000874_00000000-3_cudaKernel.cudafe1.cpp 

Microsoft (R) Incremental Linker Version 9.00.21022.08 

Copyright (C) Microsoft Corporation.  All rights reserved. 

cudaKernel.obj  

   Creating library C:\DOCUME~1\MER\LOCALS~1\Temp\mex_hVxClX\templib.x and object C:\DOCUME~1\MER\LOCALS~1\Temp\mex_hVxClX\templib.exp 

cudaKernel.obj : error LNK2019: unresolved external symbol __cudaUnregisterFatBinary referenced in function __cudaUnregisterBinaryUtil 

cudaKernel.obj : error LNK2019: unresolved external symbol cudaLaunch referenced in function "enum cudaError __cdecl cudaLaunch<char>(char *)" (??$cudaLaunch@D@@YA?AW4cudaError@@PEAD@Z) 

cudaKernel.obj : error LNK2019: unresolved external symbol cudaSetupArgument referenced in function __device_stub__Z10zeroMatrixP6float2ii 

cudaKernel.obj : error LNK2019: unresolved external symbol __cudaRegisterFunction referenced in function __sti____cudaRegisterAll_45_tmpxft_00000874_00000000_6_cudaK

ernel_cpp1_ii_c9c3f677 

cudaKernel.obj : error LNK2019: unresolved external symbol __cudaRegisterFatBinary referenced in function __sti____cudaRegisterAll_45_tmpxft_00000874_00000000_6_cudaK

ernel_cpp1_ii_c9c3f677 

cudaKernel.obj : error LNK2019: unresolved external symbol cudaMemcpy referenced in function "int __cdecl cudaLoop(float *,float *,int,int)" (?cudaLoop@@YAHPEAM0HH@Z) 

cudaKernel.obj : error LNK2019: unresolved external symbol cudaConfigureCall referenced in function "int __cdecl cudaLoop(float *,float *,int,int)" (?cudaLoop@@YAHPEAM0HH@Z) 

cudaKernel.obj : error LNK2019: unresolved external symbol cudaMalloc referenced in function "int __cdecl cudaLoop(float *,float *,int,int)" (?cudaLoop@@YAHPEAM0HH@Z) 

cudaKernel.mexw64 : fatal error LNK1120: 8 unresolved externals 

C:\PROGRA~1\MATLAB\R2008A\BIN\NVMEX.PL: Error: Link of 'cudaKernel.mexw64' failed. 

??? Error using ==> nvmex at 206

Unable to complete successfully.

I’m compiling with -lcudart and -lcufft, but also get the warnings:

Warning: cudart specified with -l option not found on -L path

Warning: cufft specified with -l option not found on -L path

Any help will be appreciated.

I think I’m getting really close now. I now get the error:

>> compile

abdelali target arch: win64 

cudaKernel.cu 

tmpxft_00000fdc_00000000-3_cudaKernel.cudafe1.gpu 

tmpxft_00000fdc_00000000-8_cudaKernel.cudafe2.gpu 

tmpxft_00000fdc_00000000-3_cudaKernel.cudafe1.cpp 

'mt' is not recognized as an internal or external command, 

operable program or batch file.

And when I try to run it, I get:

??? Invalid MEX-file 'z:\tekst\MATLAB\ISAR_Tests\cudaKernel.mexw64': The specified module could not be found.

How did you get over the following error:

c:\cuda\include\host_config.h(115) : fatal error C1083: Cannot open include file: ‘crtdefs.h’: No such file or directory

I am facing this while trying to compile on 64-bit Win7.

I will appreciate your reply/help in this regard.

Thanks
Shadab

I get the same error as Shadab, on Win 7 64-bit.

I’m using the command:

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin\nvcc.exe” -D_MATLAB_ -I"C:\Program Files\MATLAB\R2010b\extern\include" -I"C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 3.2\C\common\inc" -arch sm_11 --ptxas-options=-v -O3 -Iprivate/depth_sensor/ --cuda “private/depth_sensor/pmls.cu” --output-file “C:\Users\OWOODF~1\AppData\Local\Temp\tpc44f628a_a571_4747_80c2_df38d2b8c177.cpp”

pmls.cu

The error is:

c:\program files\nvidia gpu computing toolkit\cuda\v3.2\include\host_config.h(96) : fatal error C1083: Cannot open include file: ‘crtdefs.h’: No such file or directory

Any help would be much appreciated.

I don’t remember how I solved my problems. But I have found another solution. Now I use CMAKE to compile, and it does everything for me. Just use CL/gcc, link to the matlab libraries and compile it as a shared library. Then rename the shared library from .so/.dll to what matlab needs.

Thanks for the suggestion. I might try it, but for now I solved my problem by using the 32-bit CUDA toolkit instead of the 64-bit one. I’m using MSVC 8.0 (2005).