OpenCl in Action: Chapter 2

If you are reading OpenCL in Action and get stuck on section 2.3.3 (titled “Code example: testing device extensions”), it’s because there are several bugs in the code that will manifest themselves at runtime.

Line 30 contains the following line:

1
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, NULL, &num_devices);

The issue is that the third parameter indicates that this function call should read one device into the fourth parameter, which is NULL. As this call is only trying to retrieve the number of devices available, the simple fix is to replace it with:

1
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);

If you run the code now however, you’ll run into a segmentation fault. Using gdb (or your debugger of choice) and stepping through the code, you’ll find that the segfault happens on line 50:

1
2
clGetDeviceInfo(devices[i], CL_DEVICE_ADDRESS_BITS,
                sizeof(ext_data), &addr_data, NULL);

The clear bug here is that the third argument is the size of ext_data which is a 4096 character array. It should be a cl_uint (the type of addr_data). Replacing this code with:

1
2
clGetDeviceInfo(devices[i], CL_DEVICE_ADDRESS_BITS,
                sizeof(addr_data), &addr_data, NULL);

will fix the problem. I will be posting fixes like this to this blog and committing the changes to this repository.

I may also end up porting all the sample code to use the C++ bindings.

Comments