Bladeren bron

Implemented device selection for srad

We now use parameters as the way to select OpenCL platform, device and
type instead of having the hardcoded in the code. In this way we can
easily change the type of the benchmark without recompiling it.

Since the parameters parsing routine was quite artisanal and ad-hoc I
opted for reimplementing it adding name to paramters(e.g. -p for
platform...).
Andrea Gussoni 8 jaren geleden
bovenliggende
commit
c10e5ef29a

+ 26 - 20
opencl/srad/kernel/kernel_gpu_opencl_wrapper.c

@@ -42,20 +42,23 @@
 //========================================================================================================================================================================================================200
 
 void 
-kernel_gpu_opencl_wrapper(	fp* image,											// input image
-							int Nr,												// IMAGE nbr of rows
-							int Nc,												// IMAGE nbr of cols
-							long Ne,											// IMAGE nbr of elem
-							int niter,											// nbr of iterations
-							fp lambda,											// update step size
-							long NeROI,											// ROI nbr of elements
-							int* iN,
-							int* iS,
-							int* jE,
-							int* jW,
-							int iter,											// primary loop
-							int mem_size_i,
-							int mem_size_j)
+kernel_gpu_opencl_wrapper(fp* image,											// input image
+							            int Nr,												// IMAGE nbr of rows
+							            int Nc,												// IMAGE nbr of cols
+							            long Ne,											// IMAGE nbr of elem
+							            int niter,											// nbr of iterations
+							            fp lambda,											// update step size
+							            long NeROI,											// ROI nbr of elements
+							            int* iN,
+							            int* iS,
+							            int* jE,
+							            int* jW,
+							            int iter,											// primary loop
+							            int mem_size_i,
+							            int mem_size_j,
+                          int platform_idx,
+                          int device_idx,
+                          int use_gpu)
 {
 
 	//======================================================================================================================================================150
@@ -89,8 +92,8 @@ kernel_gpu_opencl_wrapper(	fp* image,											// input image
 	if (error != CL_SUCCESS) 
 		fatal_CL(error, __LINE__);
 
-	// Select the 1st platform
-	cl_platform_id platform = platforms[1];
+	// Select the platform using the parameter
+	cl_platform_id platform = platforms[platform_idx];
 
 	// Get the name of the selected platform and print it (if there are multiple platforms, choose the first one)
 	char pbuf[100];
@@ -112,10 +115,13 @@ kernel_gpu_opencl_wrapper(	fp* image,											// input image
 													(cl_context_properties) platform, 
 													0};
 
-	// Create context for selected platform being GPU
+  // 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 context for selected platform being GPU/CPU
 	cl_context context;
 	context = clCreateContextFromType(	context_properties, 
-										CL_DEVICE_TYPE_ALL, 
+										device_type, 
 										NULL, 
 										NULL, 
 										&error);
@@ -146,9 +152,9 @@ kernel_gpu_opencl_wrapper(	fp* image,											// input image
 	if (error != CL_SUCCESS) 
 		fatal_CL(error, __LINE__);
 
-	// Select the first device (previousely selected for the context) (if there are multiple devices, choose the first one)
+	// Select the device in accordance to the paramter
 	cl_device_id device;
-	device = devices[0];
+	device = devices[device_idx];
 
 	// Get the name of the selected device (previousely selected for the context) and print it
 	error = clGetDeviceInfo(device, 

+ 17 - 14
opencl/srad/kernel/kernel_gpu_opencl_wrapper.h

@@ -7,20 +7,23 @@ extern "C" {
 //========================================================================================================================================================================================================200
 
 void 
-kernel_gpu_opencl_wrapper(	fp* image,											// input image
-							int Nr,												// IMAGE nbr of rows
-							int Nc,												// IMAGE nbr of cols
-							long Ne,											// IMAGE nbr of elem
-							int niter,											// nbr of iterations
-							fp lambda,											// update step size
-							long NeROI,											// ROI nbr of elements
-							int* iN,
-							int* iS,
-							int* jE,
-							int* jW,
-							int iter,											// primary loop
-							int mem_size_i,
-							int mem_size_j);
+kernel_gpu_opencl_wrapper(fp* image,											// input image
+							            int Nr,												// IMAGE nbr of rows
+							            int Nc,												// IMAGE nbr of cols
+							            long Ne,											// IMAGE nbr of elem
+							            int niter,											// nbr of iterations
+							            fp lambda,											// update step size
+							            long NeROI,											// ROI nbr of elements
+							            int* iN,
+							            int* iS,
+							            int* jE,
+							            int* jW,
+							            int iter,											// primary loop
+							            int mem_size_i,
+							            int mem_size_j,
+                          int platform_idx,
+                          int device_idx,
+                          int use_gpu);
 
 //========================================================================================================================================================================================================200
 //	END

+ 58 - 23
opencl/srad/main.c

@@ -103,15 +103,47 @@ main(	int argc,
 	//	INPUT ARGUMENTS
 	//======================================================================================================================================================150
 
-	if(argc != 5){
-		printf("ERROR: wrong number of arguments\n");
-		return 0;
-	}
-	else{
-		niter = atoi(argv[1]);
-		lambda = atof(argv[2]);
-		Nr = atoi(argv[3]);						// it is 502 in the original image
-		Nc = atoi(argv[4]);						// it is 458 in the original image
+  // Rewritten parameters parsing for selecting platform and device and old
+  // parameters
+    
+  // Additional variables for platform and device selection
+  int platform_idx = 0;
+  int device_idx = 0;
+  int use_gpu = 0;
+  
+  int opt;
+  extern char *optarg;
+	while ((opt = getopt(argc, argv, "i:l:r:c:p:d:g:")) != -1 ) {
+		switch(opt){
+			case 'i':
+			   niter = atoi(optarg);
+			   break;
+			case 'l':
+			   lambda = atoi(optarg);
+			   break;
+      case 'r':
+			   Nr = atoi(optarg);
+         break;
+      case 'c':
+			   Nc = atoi(optarg);
+         break;
+      case 'p':
+			   platform_idx = atoi(optarg);
+			   break;
+      case 'd':
+  		   device_idx = atoi(optarg);
+  		   break;
+      case 'g':
+         use_gpu = atoi(optarg);
+         break;            
+      case ':':
+			   fprintf(stderr, "missing argument\n");
+			   break;
+      default:
+			   fprintf(stderr, "Usage: %s - <iteration> -l <lambda> -r <rows> -c <columns> -p <platform> -d <device> -g <use_gpu>\n",
+                 argv[0]);
+			   exit(EXIT_FAILURE);
+		}
 	}
 
 	time1 = get_time();
@@ -201,20 +233,23 @@ main(	int argc,
 	// 	KERNEL
 	//======================================================================================================================================================150
 
-	kernel_gpu_opencl_wrapper(	image,											// input image
-								Nr,												// IMAGE nbr of rows
-								Nc,												// IMAGE nbr of cols
-								Ne,												// IMAGE nbr of elem
-								niter,											// nbr of iterations
-								lambda,											// update step size
-								NeROI,											// ROI nbr of elements
-								iN,
-								iS,
-								jE,
-								jW,
-								iter,											// primary loop
-								mem_size_i,
-								mem_size_j);
+	kernel_gpu_opencl_wrapper(image,											// input image
+								            Nr,												// IMAGE nbr of rows
+								            Nc,												// IMAGE nbr of cols
+								            Ne,												// IMAGE nbr of elem
+								            niter,											// nbr of iterations
+								            lambda,											// update step size
+								            NeROI,											// ROI nbr of elements
+								            iN,
+								            iS,
+								            jE,
+								            jW,
+								            iter,											// primary loop
+								            mem_size_i,
+								            mem_size_j,
+                            platform_idx,
+                            device_idx,
+                            use_gpu);
 
 	time4 = get_time();
 

+ 1 - 1
opencl/srad/run-cpu

@@ -1 +1 @@
-./srad 300 0.5 502 458
+./srad -i 300 -l 0.5 -r 502 -c 458 -p 1 -d 0 -g 0

+ 1 - 0
opencl/srad/run-gpu

@@ -0,0 +1 @@
+./srad -i 300 -l 0.5 -r 502 -c 458 -p 0 -d 0 -g 1