void device_opencl_info(vector<DeviceInfo>& devices) { vector<cl_device_id> device_ids; cl_uint num_devices = 0; vector<cl_platform_id> platform_ids; cl_uint num_platforms = 0; /* get devices */ if(clGetPlatformIDs(0, NULL, &num_platforms) != CL_SUCCESS || num_platforms == 0) return; platform_ids.resize(num_platforms); if(clGetPlatformIDs(num_platforms, &platform_ids[0], NULL) != CL_SUCCESS) return; if(clGetDeviceIDs(platform_ids[0], opencl_device_type(), 0, NULL, &num_devices) != CL_SUCCESS || num_devices == 0) return; device_ids.resize(num_devices); if(clGetDeviceIDs(platform_ids[0], opencl_device_type(), num_devices, &device_ids[0], NULL) != CL_SUCCESS) return; /* add devices */ for(int num = 0; num < num_devices; num++) { cl_device_id device_id = device_ids[num]; char name[1024] = "\0"; if(clGetDeviceInfo(device_id, CL_DEVICE_NAME, sizeof(name), &name, NULL) != CL_SUCCESS) continue; DeviceInfo info; info.type = DEVICE_OPENCL; info.description = string(name); info.id = string_printf("OPENCL_%d", num); info.num = num; /* we don't know if it's used for display, but assume it is */ info.display_device = true; info.advanced_shading = false; info.pack_images = true; devices.push_back(info); } }
cl_device_id opencl_choose_device (cl_device_type dev_type) { #define MAX_PLATFORMS 10 #define MAX_DEVICES 10 cl_platform_id platforms[MAX_PLATFORMS]; cl_uint platforms_n = 0; cl_device_id devices[MAX_DEVICES]; cl_uint devices_n = 0; // devices per platform CL_ERR (clGetPlatformIDs (MAX_PLATFORMS, platforms, &platforms_n)); if (platforms_n < 1) { printf ("Error: no platforms found while looking for a platform.\n"); exit (EXIT_FAILURE); } cl_uint p; for (p = 0; p < platforms_n; ++p) { print_platform_info (platforms[p]); CL_ERR (clGetDeviceIDs (platforms[p], CL_DEVICE_TYPE_ALL, MAX_DEVICES, devices, &devices_n)); //CL_ERR (clGetDeviceIDs (platforms[p], dev_type, MAX_DEVICES, devices, &devices_n)); cl_uint d; for (d = 0; d < devices_n; ++d) { print_device_info (devices[d]); if (dev_type == opencl_device_type (devices[d])) { printf ("Choose device %i\n", devices[d]); return devices[d]; } } } printf ("No requested type of device found in the system\n"); exit (EXIT_FAILURE); }
OpenCLDevice(DeviceInfo& info, Stats &stats, bool background_) : Device(stats) { background = background_; cpPlatform = NULL; cxContext = NULL; cqCommandQueue = NULL; cpProgram = NULL; ckPathTraceKernel = NULL; ckFilmConvertKernel = NULL; null_mem = 0; device_initialized = false; /* setup platform */ cl_uint num_platforms; ciErr = clGetPlatformIDs(0, NULL, &num_platforms); if(opencl_error(ciErr)) return; if(num_platforms == 0) { opencl_error("OpenCL: no platforms found."); return; } ciErr = clGetPlatformIDs(1, &cpPlatform, NULL); if(opencl_error(ciErr)) return; char name[256]; clGetPlatformInfo(cpPlatform, CL_PLATFORM_NAME, sizeof(name), &name, NULL); platform_name = name; /* get devices */ vector<cl_device_id> device_ids; cl_uint num_devices; if(opencl_error(clGetDeviceIDs(cpPlatform, opencl_device_type(), 0, NULL, &num_devices))) return; if(info.num > num_devices) { if(num_devices == 0) opencl_error("OpenCL: no devices found."); else opencl_error("OpenCL: specified device not found."); return; } device_ids.resize(num_devices); if(opencl_error(clGetDeviceIDs(cpPlatform, opencl_device_type(), num_devices, &device_ids[0], NULL))) return; cdDevice = device_ids[info.num]; /* create context */ cxContext = clCreateContext(0, 1, &cdDevice, NULL, NULL, &ciErr); if(opencl_error(ciErr)) return; cqCommandQueue = clCreateCommandQueue(cxContext, cdDevice, 0, &ciErr); if(opencl_error(ciErr)) return; null_mem = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, 1, NULL, &ciErr); device_initialized = true; }