Bläddra i källkod

Implemented device selection for lavaMD

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 original benchmark had a really basic parsing structure, I opted for
implementing for scratch the parameter parsing taking advantage of the
getopt standard helper.
Andrea Gussoni 8 år sedan
förälder
incheckning
43d8ca402b

+ 22 - 16
opencl/lavaMD/kernel/kernel_gpu_opencl_wrapper.c

@@ -37,12 +37,15 @@ extern "C" {
 //========================================================================================================================================================================================================200
 
 void 
-kernel_gpu_opencl_wrapper(	par_str par_cpu,
-							dim_str dim_cpu,
-							box_str* box_cpu,
-							FOUR_VECTOR* rv_cpu,
-							fp* qv_cpu,
-							FOUR_VECTOR* fv_cpu)
+kernel_gpu_opencl_wrapper(par_str par_cpu,
+							            dim_str dim_cpu,
+							            box_str* box_cpu,
+							            FOUR_VECTOR* rv_cpu,
+							            fp* qv_cpu,
+							            FOUR_VECTOR* fv_cpu,
+                          int platform_id,
+                          int device_id,
+                          int use_gpu)
 {
 
 	//======================================================================================================================================================150
@@ -91,8 +94,8 @@ kernel_gpu_opencl_wrapper(	par_str par_cpu,
 	if (error != CL_SUCCESS) 
 		fatal_CL(error, __LINE__);
 
-	// Select the 1st platform
-	cl_platform_id platform = platforms[1];
+	// Select the platform in accordance to platform_id passed as parameter
+	cl_platform_id platform = platforms[platform_id];
 
 	// Get the name of the selected platform and print it (if there are multiple platforms, choose the first one)
 	char pbuf[100];
@@ -109,18 +112,21 @@ kernel_gpu_opencl_wrapper(	par_str par_cpu,
 	//	CREATE CONTEXT FOR THE PLATFORM
 	//====================================================================================================100
 
+  // 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 properties for selected platform
 	cl_context_properties context_properties[3] = {	CL_CONTEXT_PLATFORM, 
 													(cl_context_properties) platform, 
 													0};
 
-	// Create context for selected platform being GPU
+	// Create context for selected platform being CPU/GPU
 	cl_context context;
-	context = clCreateContextFromType(	context_properties, 
-										CL_DEVICE_TYPE_CPU, 
-										NULL, 
-										NULL, 
-										&error);
+	context = clCreateContextFromType(context_properties, 
+										                device_type, 
+										                NULL, 
+										                NULL, 
+										                &error);
 	if (error != CL_SUCCESS) 
 		fatal_CL(error, __LINE__);
 
@@ -148,9 +154,9 @@ kernel_gpu_opencl_wrapper(	par_str par_cpu,
 	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 passed as parameter
 	cl_device_id device;
-	device = devices[0];
+	device = devices[device_id];
 
 	// Get the name of the selected device (previousely selected for the context) and print it
 	error = clGetDeviceInfo(device, 

+ 9 - 6
opencl/lavaMD/kernel/kernel_gpu_opencl_wrapper.h

@@ -7,12 +7,15 @@ extern "C" {
 //========================================================================================================================================================================================================200
 
 void 
-kernel_gpu_opencl_wrapper(	par_str parms_cpu,
-							dim_str dim_cpu,
-							box_str* box_cpu,
-							FOUR_VECTOR* rv_cpu,
-							fp* qv_cpu,
-							FOUR_VECTOR* fv_cpu);
+kernel_gpu_opencl_wrapper(par_str parms_cpu,
+							            dim_str dim_cpu,
+							            box_str* box_cpu,
+							            FOUR_VECTOR* rv_cpu,
+							            fp* qv_cpu,
+							            FOUR_VECTOR* fv_cpu,
+                          int platform_id,
+                          int device_id,
+                          int use_gpu);
 
 //========================================================================================================================================================================================================200
 //	END KERNEL_GPU_OPENCL_WRAPPER HEADER

+ 52 - 11
opencl/lavaMD/main.c

@@ -61,12 +61,23 @@ extern "C" {
 
 #include "./kernel/kernel_gpu_opencl_wrapper.h"	// (in library path specified here)
 
+// Helper function to print usage
+void usage(char *argv0) {
+    char *help =
+    "\nUsage: %s [switches] \n\n"
+		"    -b               :number of boxes     [default=0]\n"
+    "    -p platform_id   :OCL platform to use [default=0]\n"
+    "    -d device_id     :OCL device to use   [default=0]\n"
+    "    -g use_gpu       :1 for GPU 0 for CPU [default=0]\n";
+    fprintf(stderr, help, argv0);
+    exit(-1);
+}
+
 //========================================================================================================================================================================================================200
 //	MAIN FUNCTION
 //========================================================================================================================================================================================================200
 
-int 
-main(	int argc, 
+int main(	int argc, 
 		char *argv [])
 {
 
@@ -114,8 +125,8 @@ main(	int argc,
 	dim_cpu.cores_arg = 1;
 	dim_cpu.boxes1d_arg = 1;
 
-	// go through arguments
-	if(argc==3){
+	/*// go through arguments
+	if(argc>=3){
 		for(dim_cpu.cur_arg=1; dim_cpu.cur_arg<argc; dim_cpu.cur_arg++){
 			// check if -boxes1d
 			if(strcmp(argv[dim_cpu.cur_arg], "-boxes1d")==0){
@@ -154,7 +165,34 @@ main(	int argc,
 	else{
 		printf("Provide boxes1d argument, example: -boxes1d 16");
 		return 0;
-	}
+	}*/
+  
+  // Arguments parsing for platform, device and type
+  
+  // Variables to store information on platform and device to use_gpu
+  int platform_id = 0;
+  int device_id = 0;
+  int use_gpu = 0;
+
+  /* obtain command line arguments and change appropriate options */
+  int opt;
+  extern char *optarg;
+  while ((opt=getopt(argc,argv,"b:p:d:g:"))!= EOF) {
+      switch (opt) {
+          case 'p': platform_id = atoi(optarg);
+                    break;
+          case 'd': device_id = atoi(optarg);
+                    break;
+          case 'g': use_gpu = atoi(optarg);
+                    break;      
+          case 'b': dim_cpu.boxes1d_arg = atoi(optarg);
+                    break;                                                        
+          case '?': usage(argv[0]);
+                    break;
+          default: usage(argv[0]);
+                    break;
+      }
+  }
 
 	time2 = get_time();
 
@@ -296,12 +334,15 @@ main(	int argc,
 	//	GPU_OPENCL
 	//====================================================================================================100
 
-	kernel_gpu_opencl_wrapper(	par_cpu,
-								dim_cpu,
-								box_cpu,
-								rv_cpu,
-								qv_cpu,
-								fv_cpu);
+	kernel_gpu_opencl_wrapper(par_cpu,
+								            dim_cpu,
+								            box_cpu,
+								            rv_cpu,
+								            qv_cpu,
+								            fv_cpu,
+                            platform_id,
+                            device_id,
+                            use_gpu);
 
 	time6 = get_time();
 

+ 1 - 1
opencl/lavaMD/run-cpu

@@ -1 +1 @@
-./lavaMD -boxes1d 16
+./lavaMD -b 16 -p 1 -d 0 -g 0

+ 1 - 0
opencl/lavaMD/run-gpu

@@ -0,0 +1 @@
+./lavaMD -b 16 -p 0 -d 0 -g 1