Using printf() to Debug Kernels

The simplest and most rudimentary approach to debugging of algorithms is to verify key data values throughout the execution of the program. For application developers, this is a tried and trusted way of actually identifying problems within the execution of a program. Because part of the algorithm are now running on an FPGA, even this way of debugging requires additional support. Towards this end, the SDAccel™ development environment supports OpenCL™ printf() built-in function within the kernels in all development flows: Software Emulation, Hardware Emulation, and running kernel in actual hardware.

IMPORTANT!: printf() is not supported in C/C++ kernels.

The following is a kernel example of using printf() and the output when the kernel is executed with global size of 8:

__kernel __attribute__ ((reqd_work_group_size(1, 1, 1)))
void hello_world(__global int *a)
{
    int idx = get_global_id(0);

     printf("Hello world from work item %d\n", idx);
     a[idx] = idx;
}

Output is as follows:

Hello world from work item 0
Hello world from work item 1
Hello world from work item 2
Hello world from work item 3
Hello world from work item 4
Hello world from work item 5
Hello world from work item 6
Hello world from work item 7
IMPORTANT!: printf() messages are buffered in the global memory and unloaded when kernel execution is completed. If printf() is used in multiple kernels, there is no guarantee that the order of the messages from each kernel will display on the host terminal.