Debugging CUDA from Matlab How to debug from Matlab?

Matlab is easily reprogrammable but sometimes slow. CUDA is theoretically fast for some applications but is driving me mad lately. I was programming the GPU via OpenGL through the Psichophysics Toolbox from Matlab when I finally realized that there appeared mysterious errors that depended on the computer you were running the software on (available functional code at my web page: CUDA + Matlab).
Now I am using CUDA through MEX files from Matlab but I have reached a dead end.

Does someone know how to compile in debug mode from Matlab? Is it a matter of correctly modifying the nvmex_helper.m file (like adding -deviceemu somewhere)? Is it really possible or the only solution will be to convert all my code into C?

Are you compiling on Windows or Linux?
It should be possible to add the -deviceemu in the compile stage and all the proper libraries during the link stage.

I am using Windows and this is the command I use (which is taken from the nvidia examples):

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

The nvmexopts.bat is apparently already prepared for debugging:

[i]rem ********************************************************************
rem Compiler parameters
rem ********************************************************************
set COMPILER=nvcc
set COMPFLAGS=-c -Xcompiler “/c /Zp8 /GR /W3 /EHsc- /Zc:wchar_t- /DMATLAB_MEX_FILE /nologo”
set OPTIMFLAGS=-Xcompiler “/MD /O2 /Oy- /DNDEBUG”
set DEBUGFLAGS=-Xcompiler “/MD /Zi /Fd”%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb"
set NAME_OBJECT=-o

rem ********************************************************************
rem Linker parameters
rem ********************************************************************
set LIBLOC=%MATLAB%\extern\lib\win32\microsoft
set LINKER=link
rem set LINKER=nvcc
set LINKFLAGS=/dll /export:%ENTRYPOINT% /MAP /LIBPATH:"%LIBLOC%" libmx.lib libmex.lib libmat.lib /implib:%LIB_NAME%.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
set LINKOPTIMFLAGS=
set LINKDEBUGFLAGS=/DEBUG /PDB:"%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb"
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=/out:"%OUTDIR%%MEX_NAME%%MEX_EXT%"
set RSP_FILE_INDICATOR=@
[/i]

I have made several tial-and-error attempts to put -deviceemu somewhere, but it doesn’t seem to work. Where exactly do I tell the compiler to debug?

Try these steps:

1)Copy the nvmexopts.bat to nvmexoptsemul.bat.

2)Edit nvmexoptsemul.bat, adding the -deviceemu to the COMPFLAGS line (before or after -c)

3)Invoke the nvmex with:
nvmex -f nvmexoptsemul.bat CUDA_test.cu -IC:\cuda\include -LC:\cuda\lib -lcufftemu -lcudart

Ok, cool, thanks for the tip, it seems to be the correct modification as it doesn’t complain.
However, no printf in global functions appears in the Matlab command line, what am I doing wrong?

And there is yet another question, can I run the code now in debug mode?

Have you tried mxPrintf?

My suggestion is to debug the cuda kernel outside of Matlab. Once you get the expected behavior, wrap the kernel in a mex.

Hi, I’m trying to debug a cuda kernel in a matlab mexfile too. I’ve done the first two steps and when I try to compile the mexfile with:

nvmex -g -f  nvmexoptsemul.bat cuda_test.cu -IC:\cuda\include -LC:\cuda\lib -lcufftemu -lcudart

I get the following errors:

target arch: win64

cuda_test.cu

c:\program files\cuda\include\common_functions.h(56): warning: dllexport/dllimport conflict with “clock”

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\time.h(174): here; dllimport/dllexport dropped

c:\program files\cuda\include\common_functions.h(56): warning: dllexport/dllimport conflict with “clock”

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\time.h(174): here; dllimport/dllexport dropped

tmpxft_000013f0_00000000-3_cuda_test.cudafe1.cpp

tmpxft_000013f0_00000000-7_cuda_test.ii

C:/Documents and Settings/fecker/Desktop/Cuda stuff/cuda lib (mex files)/src/cuda_test.cu(173) : warning C4267: ‘=’: Konvertierung von ‘size_t’ nach ‘int’, Datenverlust m”glich

C:/Documents and Settings/fecker/Desktop/Cuda stuff/cuda lib (mex files)/src/cuda_test.cu(174) : warning C4267: ‘=’: Konvertierung von ‘size_t’ nach ‘int’, Datenverlust m”glich

c:\program files\cuda\include\math_functions.h(96) : error C2201: ‘__cuda_sinf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(101) : error C2201: ‘__cuda_cosf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(111) : error C2201: ‘__cuda_tanf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(116) : error C2201: ‘__cuda_sqrtf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(146) : error C2201: ‘__cuda_log10f’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(151) : error C2201: ‘__cuda_logf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(159) : error C2201: ‘__cuda_floor’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(161) : error C2201: ‘__cuda_floorf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(166) : error C2201: ‘__cuda_expf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(171) : error C2201: ‘__cuda_coshf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(176) : error C2201: ‘__cuda_sinhf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(181) : error C2201: ‘__cuda_tanhf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(199) : error C2201: ‘__cuda_ldexp’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(224) : error C2201: ‘__cuda_frexp’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(264) : error C2201: ‘__cuda_ceil’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(266) : error C2201: ‘__cuda_ceilf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(281) : error C2201: ‘__cuda_atan2f’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(286) : error C2201: ‘__cuda_atanf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(291) : error C2201: ‘__cuda_asinf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(296) : error C2201: ‘__cuda_acosf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(299) : error C2201: ‘__cuda_hypot’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(311) : error C2201: ‘__cuda_powf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(314) : error C2201: ‘__cuda_modf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(316) : error C2201: ‘__cuda_modff’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(321) : error C2201: ‘__cuda_fmodf’: Externe Bindung erforderlich, um Export/Import zu erm”glichen

c:\program files\cuda\include\math_functions.h(1129) : error C2491: ‘__cuda_ceilf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1134) : error C2491: ‘__cuda_floorf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1139) : error C2491: ‘__cuda_sqrtf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1781) : error C2491: ‘__cuda_sinf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1792) : error C2491: ‘__cuda_cosf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1820) : error C2491: ‘__cuda_tanf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1854) : error C2491: ‘__cuda_expf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1876) : error C2491: ‘__cuda_coshf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1889) : error C2491: ‘__cuda_sinhf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1914) : error C2491: ‘__cuda_tanhf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1938) : error C2491: ‘__cuda_atan2f’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(1975) : error C2491: ‘__cuda_atanf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2011) : error C2491: ‘__cuda_asinf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2028) : error C2491: ‘__cuda_acosf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2045) : error C2491: ‘__cuda_logf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2056) : error C2491: ‘__cuda_log10f’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2405) : error C2491: ‘__cuda_modff’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2427) : error C2491: ‘__cuda_fmodf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions.h(2988) : error C2491: ‘__cuda_powf’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions_dbl_ptx1.h(91) : error C2491: ‘__cuda_ceil’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions_dbl_ptx1.h(101) : error C2491: ‘__cuda_floor’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions_dbl_ptx1.h(226) : error C2491: ‘__cuda_hypot’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions_dbl_ptx1.h(256) : error C2491: ‘__cuda_ldexp’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions_dbl_ptx1.h(271) : error C2491: ‘__cuda_frexp’: Definition von Funktion fr dllimport nicht zul„ssig

c:\program files\cuda\include\math_functions_dbl_ptx1.h(276) : error C2491: ‘__cuda_modf’: Definition von Funktion fr dllimport nicht zul„ssig

C:\PROGRA~1\MATLAB\R2008A\BIN\NVMEX.PL: Error: Compile of ‘cuda_test.cu’ failed.

Without the -deviceemu option the mexfile compiles succesfull.

Can it have something todo with the dllexport/dllimport conflict with “clock” and the dropped dllimport/dllexport?

I’m using Matlab r2008a, VS2008 and win xp64.

I’m using Matlab+CUDA too. I like different solution. I create a dll Microsoft VC++ project and than I can use standrad VC debuger which is fine.
In project You have to include #include “mex.h” only and do export of mexFunction().
Build project with debug (and if You need to debug kernel with deviceemu) settings and set breakpoints.
Than You can use “Attach to Process” (select matlab) and run the function from matlab - now You can play with VC debuger as usually.
If You need pritfs - use mexPrintf as was metioned above.

Hi Kywan,

you answered a 1,5 year old question :-). I re-invoked this thread because of a problem which I described in my post from Feb 19 2009 in this thread.

Problem is still not solved. :-(

Hi,

I am facing exactly the same as dan@tubs. Please someone suggest a solution. I am stuck without it.

THanks,

Aditi

Not quite sure what the problem is (German isn’t my strong suit and google translator isn’t that great either), but I can say that " the dllexport/dllimport conflict with “clock” and the dropped dllimport/dllexport?" is not related to the problem, as all code I compiled in Matlab ran fine even when that warning appeared.

I would try replacing all functions dependent on the math_functions.h, and removing all references to the header, then add it back later if you really need it.

That is what even I did (removing references to math_functions.h) to solve the problem at least for now. I was getting some other errors (which I do not remember now)…but adding “-c -deviceemu” to DEBUGFLAGS solved that problem for me.

Have you tried using [post=“0”]http://www.mathworks.com/matlabcentral/fileexchange/25314-cuda-mex[/post] ? Rather like nvmex, cuda_mex is similar but sets up the environment using the mex environment. I have tried using deviceemu succesfully in a simple example included in this tool, with Visual C++ 2008 and Windows XP, Matlab 2009b.

[codebox]cuda_mex -g COMPFLAGS="$COMPFLAGS -deviceemu" yourcudacode.cu[/codebox]

Hi ,

.I would like to debug cuda from within matlab…But I am working in linux … can anyone suggest where in the nvopts.sh do i make the above changes ?

sisutata

You can debug Matlab mexfiles including CUDA codes using Visual Studio and NVIDIA Nsight for Visual Studio by the procedure reported in https://devtalk.nvidia.com/default/topic/611272/cuda-programming-and-performance/integrate-cu-cpp-and-mex64-file-under-matlab/ and http://www.orangeowlsolutions.com/archives/564.