问题
Apple provides an OpenCL "Hello World" example, which can be downloaded as a .zip
file from the following page:
https://developer.apple.com/library/mac/samplecode/OpenCL_Hello_World_Example/Introduction/Intro.html
I downloaded it, opened the project in Xcode, and clicked Run
. The build succeeded, but I got the following error message: Error: Failed to create a device group!
I would appreciate any advice on how to get a simple OpenCL example running on my Mac. In case it is diagnostically relevant: I'm running Mac OS 10.7.5 on an early 2011 MacBook Pro, and I have Xcode version 4.2 installed.
回答1:
Hooray, I worked it out myself. The hello.c
file that Apple provides contains the following lines of code:
114 // Connect to a compute device
115 //
116 int gpu = 1;
117 err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
118 if (err != CL_SUCCESS)
119 {
120 printf("Error: Failed to create a device group!\n");
121 return EXIT_FAILURE;
122 }
The code is trying to get the id of a GPU device that supports OpenCL. The problem is that my machine (MacBook Pro, Early 2011) doesn't have a GPU that supports OpenCL. If the CL_DEVICE_TYPE_CPU
flag is set instead, then the CPU is found, and this does support OpenCL. If the file is modified as follows:
116 int gpu = 0;
then I get the output: Computed '1024/1024' correct values!
回答2:
You can modify the code so that if there is no supported GPU then it fall back to 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);
来源:https://stackoverflow.com/questions/18616599/how-to-run-apples-opencl-hello-world-example-in-xcode