Printf within a Kernel

Hi all

CUDA 3.1 specification says that there is possible to use printf inside a kernel such that each thread prints the specified string.

I wonder if there is the same feature in OpenCL.

Thank you.

With a native kernel, you can.
it’s a real c function executed on device that can run a native kernel (I guess only CPU).

What’s a “native” kernel?

Thank you for your reply

see also clEnqueueNativeKernel

I’ll look it.

Thank you very much.

I use printf on my CPU when debugging my kernels. I also find assert(…) to be a useful function.

Here are some useful macros that you can use in your kernel…

#define DEBUG

#ifdef DEBUG
#define assert(x) \
		if (! (x)) \
		{ \
			printf("Assert(%s) failed in %s:%d", #x, __FUNCTION__, __LINE__); \
			exit(-1); \
	#define assert(X)
	#define printf(fmt, ...)

Just comment out the #define DEBUG when you run this on a device which will not support printf or you want to not have your asserts run.

Hope this helps! I’m not sure how portable the FUNCTION and LINE macros are, but they work for me:).

printf() is not part of standard OpenCL C; don’t be surprised if it fails elsewhere. FUNCTION doesn’t appear in the standard either, although LINE is (section 6.9).

BTW, variadic macros like “printf(fmt,…)” are also not standard either (section 6.8 ).