您可以修改代码,以便如果没有支持的 GPU 则回退到 CPU
// Connect to a compute device
//
int gpu = 1;
err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
if (err != CL_SUCCESS)
{
printf("Failed to create a device group for gpu, so it will fall back to use cpu!\n");
err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
}
// Get some information about the returned device
cl_char vendor_name[1024] = {0};
cl_char device_name[1024] = {0};
size_t returned_size = 0;
err = clGetDeviceInfo(device_id, CL_DEVICE_VENDOR, sizeof(vendor_name),
vendor_name, &returned_size);
err |= clGetDeviceInfo(device_id, CL_DEVICE_NAME, sizeof(device_name),
device_name, &returned_size);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
printf("Connecting to %s %s...\n", vendor_name, device_name);