Errors with compiling CUDA-enabled Matlab mex files What's the latest news on nvmex?

Hi. I’m running Windows XP 32-bit with CUDA 2.2 and Matlab 2008a.

As per http://forums.nvidia.com/index.php?showtopic=66961 (from about 3 months ago), I downloaded the Matlab_CUDA nvmex package and updated it with the 2008a-specific replacements provided therein.

I attempt to compile the provided Szeta.cu application like so. First, I compile the cuFFT interface with no problems:

>> mex fft2_cuda.c -IC:\CUDA\include -LC:\CUDA\lib -lcudart -lcufft

>> mex fft2_cuda_sp_dp.c -IC:\CUDA\include -LC:\CUDA\lib -lcudart -lcufft

>> mex ifft2_cuda.c -IC:\CUDA\include -LC:\CUDA\lib -lcudart -lcufft

But Szeta.cu fails to build, with the following errors:

>> nvmex -f nvmexopts.bat Szeta.cu -IC:\cuda\include -LC:\cuda\lib -lcudart

Szeta.cu 

tmpxft_00001484_00000000-3_Szeta.cudafe1.gpu 

tmpxft_00001484_00000000-8_Szeta.cudafe2.gpu 

tmpxft_00001484_00000000-3_Szeta.cudafe1.cpp 

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

??? Error using ==> nvmex at 206

Unable to complete successfully.

It appears that the linker fails.

In verbose mode:

>> nvmex -v -f nvmexopts.bat Szeta.cu -IC:\cuda\include -LC:\cuda\lib -lcudart

 This is mex, Copyright 1984-2007 The MathWorks, Inc. 

		-> Options file specified on command line 

---------------------------------------------------------------- 

->	Options file		   = nvmexopts.bat 

	  MATLAB				 = C:\PROGRA~1\MATLAB\R2008a 

->	COMPILER			   = nvcc 

->	Compiler flags: 

	 COMPFLAGS		   =  -c -Xcompiler "/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD" 

	 OPTIMFLAGS		  = -Xcompiler "/O2 /Oy- /DNDEBUG" 

	 DEBUGFLAGS		  = -Xcompiler "/Zi /Fd"Szeta.mexw32.pdb"" 

	 arguments		   =  -IC:\cuda\include 

	 Name switch		 =  

->	Pre-linking commands   =  

->	LINKER				 = link 

->	Link directives: 

	 LINKFLAGS		   = /dll /export:mexFunction /MAP /LIBPATH:"C:\PROGRA~1\MATLAB\R2008a\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\DOCUME~1\fasiha\LOCALS~1\Temp\mex_ImWOwD\templib.x /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib 

	 LINKDEBUGFLAGS	  = /DEBUG /PDB:"Szeta.mexw32.pdb" 

	 LINKFLAGSPOST	   =  

	 Name directive	  = /out:"Szeta.mexw32" 

	 File link directive =  

	 Lib. link directive =  

	 Rsp file indicator  = @ 

->	Resource Compiler	  = rc /fo "mexversion.res" 

->	Resource Linker		=  

---------------------------------------------------------------- 

--> nvcc  -IC:\cuda\include  -c -Xcompiler "/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD"  -IC:\PROGRA~1\MATLAB\R2008a\extern\include -IC:\PROGRA~1\MATLAB\R2008a\simulink\include -Xcompiler "/O2 /Oy- /DNDEBUG" -DMX_COMPAT_32 C:\home\fasiha\Matlab_CUDA-1.1a\Matlab_CUDA_1.1\Szeta.cu 

Szeta.cu 

tmpxft_000010e0_00000000-3_Szeta.cudafe1.gpu 

tmpxft_000010e0_00000000-8_Szeta.cudafe2.gpu 

tmpxft_000010e0_00000000-3_Szeta.cudafe1.cpp 

tmpxft_000010e0_00000000-13_Szeta.ii 

	Contents of C:\DOCUME~1\fasiha\LOCALS~1\Temp\mex_ImWOwD\mex_tmp.rsp: 

  Szeta.obj 

--> link /out:"Szeta.mexw32" /dll /export:mexFunction /MAP /LIBPATH:"C:\PROGRA~1\MATLAB\R2008a\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\DOCUME~1\fasiha\LOCALS~1\Temp\mex_ImWOwD\templib.x /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  @C:\DOCUME~1\fasiha\LOCALS~1\Temp\mex_ImWOwD\mex_tmp.rsp  C:\cuda\lib\cudart.lib  

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

??? Error using ==> nvmex at 206

Unable to complete successfully.

The verbose output indicates that nvcc’s compilation is fine, the linker steps are the issue. I cannot discern any directory error with the link command, and the exact same issue happens with my custom CUDA-enabled mex file, so I turn to you for assistance. Thanks,

Ahmed

I think you are missing cufft.

Try:
nvmex -f nvmexopts.bat Szeta.cu -IC:\cuda\include -LC:\cuda\lib -lcudart -lcufft

You can also try to use a two steps process:

  1. use nvcc to produce the object file ( something similar to this, but with Windows syntax)
    !nvcc -c Szeta.cu -Xcompiler -fPIC -I /usr/local/matlab/extern/include
  2. use the regular mex to do the linking ( again this is for Linux):
    mex Szeta.o -L /usr/local/cuda/lib64 -lcudart -lcufft

You are absolutely right, 1) I was missing -lcufft, and 2) when I try your two-step procedure, it (and my custom CUDA-mex) compiles fine. !!!

Specifically:

nvmex -c -v -f nvmexopts.bat Szeta.cu -IC:\cuda\include -LC:\cuda\lib -lcudart -lcufft

mex Szeta.obj  -IC:\cuda\include -LC:\cuda\lib -lcufft -lcudart

A big thanks to you and CLH!!!

If nvmex is working on your system, you should be able to compile and link with a single command:
nvmex -f nvmexopts.bat Szeta.cu -IC:\cuda\include -LC:\cuda\lib -lcudart -lcufft

The alternative is for Matlab versions where the nvmex perl script generates errors.

I agree, this command you suggest fails to link, producing an apparently identical error as in my initial post. Therefore, I use the alternative two-step compile-link step using nvmex and mex to get my custom mexfiles working. (And they work extremely well! :)

I want to thank you for your sharing this two-step compiling trick because it also helps me solve the problem on my machine.

I want to thank you for your sharing this two-step compiling trick because it also helps me solve the problem on my machine.