MATLAB Answers

How to print in cuda kernel when writing mex files

16 views (last 30 days)
Weixin Wang
Weixin Wang on 16 Mar 2021
Commented: Weixin Wang on 31 Mar 2021
I'm trying to write cude code and generate a mex file. For debugging purposes, I want to print something in a cuda kernel. When I call "printf", the compiler returns the following error:
Error calling a __host__ function("mexPrintf_800") from a __global__ function("mulImg_FTot") is not allowed pendulum_propagate D:\C++\test_VS\pendulum_propagate\pendulum_propagate\integrate.cu 73
Error identifier "mexPrintf_800" is undefined in device code pendulum_propagate D:\C++\test_VS\pendulum_propagate\pendulum_propagate\integrate.cu 73
Error MSB3721 The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64" -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include" --keep-dir x64\Release -maxrregcount=0 --machine 64 --compile -cudart static -DWIN32 -DWIN64 -DNDEBUG -D_CONSOLE -D_WINDLL -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /Fdx64\Release\vc142.pdb /FS /MD " -o x64\Release\integrate.cu.obj "D:\C++\test_VS\pendulum_propagate\pendulum_propagate\integrate.cu"" exited with code 1. pendulum_propagate C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.1.targets 785
So, is there a way to print in cuda kernel when writing mex files? Thanks in advance.

Accepted Answer

Joss Knight
Joss Knight on 18 Mar 2021
For convenience, printf is redirected by mex.h so that output will appear in the command window. You can use printf, but you should either put your kernels before the mex header file in your source file, or declare #undef printf. You can also compile all your device code independently and link it into your main mex file.
You will not see any output on Windows unless you launch MATLAB from a terminal window using matlab -wait -log.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by