Browse Source

Implemented device selection for nw

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 parameter parsing was quite artisanal I opted for reimplementing it
taking adavantage of the getopt helper function of the C standard
library.
Andrea Gussoni 8 years ago
parent
commit
416b092239
3 changed files with 67 additions and 29 deletions
  1. 65 28
      opencl/nw/nw.c
  2. 1 1
      opencl/nw/run-cpu
  3. 1 0
      opencl/nw/run-gpu

+ 65 - 28
opencl/nw/nw.c

@@ -10,12 +10,13 @@
 
 #define LIMIT -999
 
+#include <iostream>
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
-#include <iostream>
+#include <string.h>
 #include <string>
 #include <sys/time.h>
+#include <unistd.h>
 
 #ifdef NV //NVIDIA
 	#include <oclUtils.h>
@@ -59,7 +60,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;
@@ -67,14 +68,13 @@ static int initialize(int use_gpu)
 	// modification to handle the case in which we have more than one OpenCL platform available on the system.
 	cl_uint platformCount;
 	
-
 	// create OpenCL context
 	clGetPlatformIDs(0, NULL, &platformCount);
 	
 	cl_platform_id *platforms_ids;
 	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; }
@@ -91,7 +91,7 @@ static int initialize(int use_gpu)
 	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 );
+	cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
 	if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
 	return 0;
 }
@@ -147,24 +147,62 @@ int main(int argc, char **argv){
 
   printf("WG size of kernel = %d \n", BLOCK_SIZE);
 
-    int max_rows, max_cols, penalty;
-	char * tempchar;
-	// the lengths of the two sequences should be able to divided by 16.
+  int max_rows, max_cols, penalty;
+	char * tempchar;	
+  
+  // Rewritten parameters parsing for selecting platform and device and old
+  // parameters
+  
+  // Variables to store information on platform and device to use_gpu
+  int platform_id = 0;
+  int device_id = 0;
+  int use_gpu = 0;
+  
+  // The lengths of the two sequences should be able to divided by 16.
 	// And at current stage  max_rows needs to equal max_cols
-	if (argc == 4)
-	{
-		max_rows = atoi(argv[1]);
-		max_cols = atoi(argv[1]);
-		penalty = atoi(argv[2]);
-		tempchar = argv[3];
-	}
-    else{
-	     usage(argc, argv);
-    }
-	
-	if(atoi(argv[1])%16!=0){
-	fprintf(stderr,"The dimension values must be a multiple of 16\n");
-	exit(1);
+  int opt;
+  extern char *optarg;
+	while ((opt = getopt(argc, argv, "r:c:x:t:p:d:g:")) != -1 ) {
+		switch(opt){
+			case 'r':
+			   max_rows = atoi(optarg);
+         max_cols = atoi(optarg);
+         if(max_rows%16!=0){
+       	   fprintf(stderr,"The dimension values must be a multiple of 16\n");
+       	   exit(1);
+       	 }
+			   break;
+			case 'c':
+         max_rows = atoi(optarg);
+			   max_cols = atoi(optarg);
+         if(max_cols%16!=0){
+       	   fprintf(stderr,"The dimension values must be a multiple of 16\n");
+       	   exit(1);
+       	 }
+			   break;
+      case 'x':
+			   penalty = atoi(optarg);
+         break;
+      case 't':
+			   tempchar = optarg;
+			   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, "missing argument\n");
+			   break;
+      default:
+			   fprintf(stderr, "Usage: %s -r/-c <max_rows/max_cols> -x <penalty> -p <platform> -d <device> -g <use_gpu>\n",
+                 argv[0]);
+			   exit(EXIT_FAILURE);
+		}
 	}
 
 	max_rows = max_rows + 1;
@@ -174,9 +212,9 @@ int main(int argc, char **argv){
 	int *input_itemsets;
 	int *output_itemsets;
 	
-	reference = (int *)malloc( max_rows * max_cols * sizeof(int) );
-    input_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) );
-	output_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) );
+  reference = (int *)malloc( max_rows * max_cols * sizeof(int) );
+  input_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) );
+  output_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) );
 	
 	srand(7);
 	
@@ -230,9 +268,8 @@ int main(int argc, char **argv){
 	size_t local_work[3] = { (workgroupsize>0)?workgroupsize:1, 1, 1 };
 	size_t global_work[3] = { nworkitems, 1, 1 }; //nworkitems = no. of GPU threads
 	
-	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;

+ 1 - 1
opencl/nw/run-cpu

@@ -1 +1 @@
-./nw 6400 10 ./nw.cl
+./nw -r 6400 -x 10 -t ./nw.cl -p 1 -d 0 -g 0

+ 1 - 0
opencl/nw/run-gpu

@@ -0,0 +1 @@
+./nw -r 6400 -x 10 -t ./nw.cl -p 0 -d 0 -g 1