Преглед изворни кода

Implemented device selection for lud

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.

I reused the available argument passing routine and simply adapted the rest of
the code.
Andrea Gussoni пре 8 година
родитељ
комит
a06391e685
3 измењених фајлова са 41 додато и 31 уклоњено
  1. 39 30
      opencl/lud/ocl/lud.cpp
  2. 1 1
      opencl/lud/ocl/run-cpu
  3. 1 0
      opencl/lud/ocl/run-gpu

+ 39 - 30
opencl/lud/ocl/lud.cpp

@@ -50,7 +50,7 @@ static cl_device_type   device_type;
 static cl_device_id   * device_list;
 static cl_int           num_devices;
 
-static int initialize(int use_gpu)
+static int initialize(int platform_id, int device_id, int use_gpu)
 {
 	cl_int result;
 	size_t size;
@@ -63,12 +63,12 @@ static int initialize(int use_gpu)
 	clGetPlatformIDs(0, NULL, &platformCount);
 	platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
 	if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
-	cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[1], 0};
+	cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
 	device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
 	context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
 	if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
 
-	// get the list of GPUs
+	// get the list of GPUs/CPUs
 	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
 	num_devices = (int) (size / sizeof(cl_device_id));
 	printf("num_devices = %d\n", num_devices);
@@ -79,8 +79,8 @@ static int initialize(int use_gpu)
 	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
 	if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
 
-	// create command queue for the first device
-	cmd_queue = clCreateCommandQueue( context, device_list[0], 0, NULL );
+	// create command queue for the device passed as parameter
+	cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
 	if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
 	return 0;
 }
@@ -123,33 +123,44 @@ main ( int argc, char *argv[] )
 	const char *input_file = NULL;
 	float *m, *mm;
 	stopwatch sw;
+  
+  // Variables to store information on platform and device to use_gpu
+  int platform_id = 0;
+  int device_id = 0;
+  int use_gpu = 0;
 	
-	while ((opt = getopt_long(argc, argv, "::vs:i:", 
+	while ((opt = getopt_long(argc, argv, "::vs:i:p:d:g:", 
                             long_options, &option_index)) != -1 ) {
 		switch(opt){
 			case 'i':
-			input_file = optarg;
-			break;
+			   input_file = optarg;
+			   break;
 			case 'v':
-			do_verify = 1;
-			break;
-        case 's':
-			matrix_dim = atoi(optarg);
-			printf("Generate input matrix internally, size =%d\n", matrix_dim);
-			// fprintf(stderr, "Currently not supported, use -i instead\n");
-			// fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file]\n", argv[0]);
-			// exit(EXIT_FAILURE);
-			break;
-        case '?':
-			fprintf(stderr, "invalid option\n");
-			break;
-        case ':':
-			fprintf(stderr, "missing argument\n");
-			break;
-        default:
-			fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file]\n",
-                  argv[0]);
-			exit(EXIT_FAILURE);
+			   do_verify = 1;
+			   break;
+      case 's':
+			   matrix_dim = atoi(optarg);
+			   printf("Generate input matrix internally, size =%d\n", matrix_dim);
+			   break;
+      case 'p':
+			   platform_id = atoi(optarg);
+			   break;
+      case 'd':
+  		   device_id = atoi(optarg);
+  		   break;
+      case 'g':
+         use_gpu = atoi(optarg);
+         break;            
+      case '?':
+			   fprintf(stderr, "invalid option\n");
+			   break;
+      case ':':
+			   fprintf(stderr, "missing argument\n");
+			   break;
+      default:
+			   fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file] -p platform -d device -g use_gpu\n",
+                 argv[0]);
+			   exit(EXIT_FAILURE);
 		}
 	}
   
@@ -201,10 +212,8 @@ main ( int argc, char *argv[] )
 	fread(source + strlen(source), sourcesize, 1, fp);
 	fclose(fp);
 
-	// Use 1: GPU  0: CPU
-	int use_gpu = 0;
 	// OpenCL initialization
-	if(initialize(use_gpu)) return -1;
+	if(initialize(platform_id, device_id, use_gpu)) return -1;
 	// compile kernel
 	cl_int err = 0;
 	const char * slist[2] = { source, 0 };

+ 1 - 1
opencl/lud/ocl/run-cpu

@@ -1 +1 @@
-./lud -s 2048
+./lud -s 2048 -p 1 -d 0 -g 0

+ 1 - 0
opencl/lud/ocl/run-gpu

@@ -0,0 +1 @@
+./lud -s 2048 -p 0 -d 0 -g 1