| 260 | } |
| 261 | |
| 262 | bool createContext(cl_context* context) |
| 263 | { |
| 264 | cl_int errorNumber = 0; |
| 265 | cl_uint numberOfPlatforms = 0; |
| 266 | cl_platform_id firstPlatformID = 0; |
| 267 | |
| 268 | /* Retrieve a single platform ID. */ |
| 269 | if (!checkSuccess(clGetPlatformIDs(1, &firstPlatformID, &numberOfPlatforms))) |
| 270 | { |
| 271 | cerr << "Retrieving OpenCL platforms failed. " << __FILE__ << ":"<< __LINE__ << endl; |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (numberOfPlatforms <= 0) |
| 276 | { |
| 277 | cerr << "No OpenCL platforms found. " << __FILE__ << ":"<< __LINE__ << endl; |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | cl_uint deviceCount; |
| 282 | // get all devices |
| 283 | clGetDeviceIDs(firstPlatformID, CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount); |
| 284 | |
| 285 | struct {cl_device_type type; const char* name; cl_uint dcount; } devices[] = |
| 286 | { |
| 287 | { CL_DEVICE_TYPE_CPU, "CL_DEVICE_TYPE_CPU", 0 }, |
| 288 | { CL_DEVICE_TYPE_GPU, "CL_DEVICE_TYPE_GPU", 0 }, |
| 289 | { CL_DEVICE_TYPE_ACCELERATOR, "CL_DEVICE_TYPE_ACCELERATOR", 0 } |
| 290 | }; |
| 291 | |
| 292 | const int NUM_OF_DEVICE_TYPES = sizeof(devices)/sizeof(devices[0]); |
| 293 | |
| 294 | printf("Number of devices available of each type:\n"); |
| 295 | for(int i = 0; i < NUM_OF_DEVICE_TYPES; ++i) |
| 296 | { |
| 297 | errorNumber = clGetDeviceIDs(firstPlatformID, devices[i].type, 0, 0, &devices[i].dcount); |
| 298 | if(CL_DEVICE_NOT_FOUND == errorNumber) |
| 299 | { |
| 300 | devices[i].dcount = 0; |
| 301 | errorNumber = CL_SUCCESS; |
| 302 | } |
| 303 | printf("\t%s: %d\n", devices[i].name, devices[i].dcount); |
| 304 | } |
| 305 | |
| 306 | /* Get a context with a device from the platform found above. */ |
| 307 | cl_context_properties contextProperties [] = {CL_CONTEXT_PLATFORM, (cl_context_properties)firstPlatformID, 0}; |
| 308 | if(devices[NUM_OF_DEVICE_TYPES-1].dcount > 0) //choose Accelerator as first preference if present |
| 309 | *context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_ACCELERATOR, NULL, NULL, &errorNumber); |
| 310 | else if(devices[NUM_OF_DEVICE_TYPES-2].dcount > 0) //choose GPU as second preference if present |
| 311 | *context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU, NULL, NULL, &errorNumber); |
| 312 | else //choose CPU as last preference |
| 313 | *context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_CPU, NULL, NULL, &errorNumber); |
| 314 | if (!checkSuccess(errorNumber)) |
| 315 | { |
| 316 | cerr << "Creating an OpenCL context failed. " << __FILE__ << ":"<< __LINE__ << endl; |
| 317 | return false; |
| 318 | } |
| 319 | |