|
@@ -214,7 +214,7 @@ float *dilate_OpenCL(int max_gicov_m, int max_gicov_n, int strel_m, int strel_n)
|
|
|
|
|
|
|
|
|
// Chooses the most appropriate GPU on which to execute
|
|
|
-void select_device() {
|
|
|
+void select_device(int platform_id, int device_id, int use_gpu) {
|
|
|
cl_int error;
|
|
|
|
|
|
// Determine the number of platforms
|
|
@@ -231,39 +231,37 @@ void select_device() {
|
|
|
// Get the list of platforms
|
|
|
cl_platform_id *platform_ids = (cl_platform_id *) malloc(sizeof(cl_platform_id) * num_platforms);
|
|
|
error = clGetPlatformIDs(num_platforms, platform_ids, NULL);
|
|
|
+ check_error(error, __FILE__, __LINE__);
|
|
|
+
|
|
|
+ // Selector for the device type in accordance to what passed as parameter
|
|
|
+ cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
|
|
|
+
|
|
|
+ // Create an OpenCL context
|
|
|
+ cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform_ids[platform_id], 0};
|
|
|
+ context = clCreateContextFromType(ctxprop, device_type, NULL, NULL, &error);
|
|
|
+ // If this platform has no GPU, try the next one
|
|
|
check_error(error, __FILE__, __LINE__);
|
|
|
|
|
|
- // Iterate through all available platforms, choosing the first one that has a GPU
|
|
|
- int i;
|
|
|
- for (i = 0; i < num_platforms; i++) {
|
|
|
+ // Get the list of devices (GPUs or CPUs)
|
|
|
+ size_t size;
|
|
|
+ error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &size);
|
|
|
+ check_error(error, __FILE__, __LINE__);
|
|
|
+ cl_device_id *device_list = (cl_device_id *) malloc(size);
|
|
|
+ error = clGetContextInfo(context, CL_CONTEXT_DEVICES, size, device_list, NULL);
|
|
|
+ check_error(error, __FILE__, __LINE__);
|
|
|
+
|
|
|
+ // Create a command queue for the device passed as parameter
|
|
|
+ device = device_list[device_id];
|
|
|
+ command_queue = clCreateCommandQueue(context, device, 0, &error);
|
|
|
+ check_error(error, __FILE__, __LINE__);
|
|
|
+
|
|
|
+ // Print the device name
|
|
|
+ char cBuffer[1024];
|
|
|
+ clGetDeviceInfo(device_list[0], CL_DEVICE_NAME, sizeof(cBuffer), &cBuffer, NULL);
|
|
|
+ printf("Running on: %s\n", cBuffer);
|
|
|
+
|
|
|
+ return;
|
|
|
|
|
|
- // Create an OpenCL context
|
|
|
- cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform_ids[i], 0};
|
|
|
- context = clCreateContextFromType(ctxprop, CL_DEVICE_TYPE_GPU, NULL, NULL, &error);
|
|
|
- // If this platform has no GPU, try the next one
|
|
|
- if (error == CL_DEVICE_NOT_FOUND) continue;
|
|
|
- check_error(error, __FILE__, __LINE__);
|
|
|
-
|
|
|
- // Get the list of devices (GPUs)
|
|
|
- size_t size;
|
|
|
- error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &size);
|
|
|
- check_error(error, __FILE__, __LINE__);
|
|
|
- cl_device_id *device_list = (cl_device_id *) malloc(size);
|
|
|
- error = clGetContextInfo(context, CL_CONTEXT_DEVICES, size, device_list, NULL);
|
|
|
- check_error(error, __FILE__, __LINE__);
|
|
|
-
|
|
|
- // Create a command queue for the first device
|
|
|
- device = device_list[0];
|
|
|
- command_queue = clCreateCommandQueue(context, device, 0, &error);
|
|
|
- check_error(error, __FILE__, __LINE__);
|
|
|
-
|
|
|
- // Print the device name
|
|
|
- char cBuffer[1024];
|
|
|
- clGetDeviceInfo(device_list[0], CL_DEVICE_NAME, sizeof(cBuffer), &cBuffer, NULL);
|
|
|
- printf("Running on: %s\n", cBuffer);
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
// If we reach here, no platform has a GPU
|
|
|
printf("Error: None of the platforms has a GPU\n");
|