How to print in cuda kernel when writing mex files

18 次查看(过去 30 天)
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.

采纳的回答

Joss Knight
Joss Knight 2021-3-18
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Troubleshooting in MATLAB Compiler SDK 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by