Results 1 to 7 of 7

Thread: Printf within a Kernel

  1. #1

    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.

  2. #2
    Senior Member
    Join Date
    Nov 2009
    Posts
    118

    Re: Printf within a Kernel

    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).

  3. #3

    Re: Printf within a Kernel

    Quote Originally Posted by matrem
    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

  4. #4
    Senior Member
    Join Date
    Nov 2009
    Posts
    118

    Re: Printf within a Kernel

    Quote Originally Posted by opencl 1.1 specification 3.2.2
    Native kernels are accessed through a host function pointer. Native kernels are queued for execution along with OpenCL kernels on a device and share memory objects with OpenCL kernels. For example, these native kernels could be functions defined in application code or exported from a library. Note that the ability to execute native kernels is an optional functionality within OpenCL and the semantics of native kernels are implementation-defined. The OpenCL API includes functions to query capabilities of a device(s) and determine if this capability is supported.
    see also clEnqueueNativeKernel

  5. #5

    Re: Printf within a Kernel

    I'll look it.

    Thank you very much.

  6. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    11

    Re: Printf within a Kernel

    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...

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

    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.

  7. #7
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Printf within a Kernel

    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 ).
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

Similar Threads

  1. using printf in the .cl file
    By elrachal in forum OpenCL
    Replies: 6
    Last Post: 03-22-2013, 10:49 AM
  2. Troubleshooting via printf?
    By The.Scotsman in forum OpenCL
    Replies: 1
    Last Post: 06-06-2012, 01:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •