_RPTF for debugging kernels

In the spirit of giving back for all the help I’ve gotten from this forum as a newbie to CUDA, I wanted to recommend the use of _RPTF for debugging, especially for kernels. To my surprise I did a search on both the computing forums for “_RPTF” and didn’t get a single hit. This is a standard C reporting function that appears to have full support in nvcc and I’ve found it very useful and wanted to make sure that other newbies knew of it’s existence.

I like _RPTF because you don’t have to remove the statements when you do a Release build, and the output can be directed to the Visual Studio output window. I have found that to be a very useful feature as it then appears with any C++ exceptions that occur showing you where to look for the problem.

To use _RPTF, add the following to your .cu files just prior to where you include your kernels.cu code (if you use another system, then just make sure that the kernel code sees it);

#define _CRTDBG_MAP_ALLOC

#include <stdlib.h>

#include <crtdbg.h>

#ifdef _DEBUG

#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)

#define new DEBUG_NEW

#endif

You will also need to add the above to host code, and before you can use _RPTF you will need to set the reporting mode:

#ifdef _DEBUG

	_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

#endif

That’s all there is to setting it up. There are 5 functions: _RPTF[0-4] depending upon the number of variables. The function uses printf syntax so it’s very simple to use.

_RPTF2(_CRT_ERROR, " MyKernelFunction: myFloatArray[%d] = %g\n",ndx,myFloatArray[ndx]);

I use _CRT_ERROR but there are other choices for output, the main thing is that I want the output to go to my Visual Studio output window which requires “_CRTDBG_MODE_DEBUG”. When I build without _DEBUG, the statements are ignored by both the VS and nvcc compilers, so I leave them in which let’s me switch back and forth from Debug(with emulation) and Release builds with a minimum of grief. Obviously running with _DEBUG but not using emulation is going to cause complaints for these in kernel code but I usually use emulation with _DEBUG and with Release I output to a debug file from non-kernel code to test async calls, streams, etc.

Hope this is useful,

  • Richard

Is crtdbg.h a windows only header? I cannot find it on my system.

On linux, I use an equivalent “report” macro ( http://code.google.com/p/hydrazine/source/…ntation/debug.h )

It’s listed as part of the standard C run-time libraries but only the debug versions so you should see it if you’re building /Mtd.

  • R