Jelajahi Sumber

Implemented device selection for gaussian

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.

The argument parsing is done on top of the pre-existing routine.
Andrea Gussoni 8 tahun lalu
induk
melakukan
c17feafd29

+ 8 - 4
opencl/gaussian/clutils.cpp

@@ -238,13 +238,17 @@ cl_context cl_init(char devicePreference)
 
     return context;
 }
-cl_context cl_init_context(int platform, int dev,int quiet) {
+cl_context cl_init_context(int platform_id, int device_id, int quiet, int use_gpu) {
     int printInfo=1;
-    if (platform >= 0 && dev >= 0) printInfo = 0;
+    if (platform_id >= 0 && device_id >= 0) printInfo = 0;
 	cl_int status;
 	// Used to iterate through the platforms and devices, respectively
 	cl_uint numPlatforms;
 	cl_uint numDevices;
+  
+  // Prepare the device type on the basis of what received as argument, in this
+  // way we can specialize the code
+  cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
 
 	// These will hold the platform and device we select (can potentially be
 	// multiple, but we're just doing one for now)
@@ -321,8 +325,8 @@ cl_context cl_init_context(int platform, int dev,int quiet) {
 	if (printInfo) printf("Enter Platform and Device No (Seperated by Space) \n");
 	if (printInfo) scanf("%d %d", &platform_touse, &device_touse);
 	else {
-	  platform_touse = platform;
-	  device_touse = dev;
+	  platform_touse = platform_id;
+	  device_touse = device_id;
 	}
 	if (!quiet) printf("Using Platform %d \t Device No %d \n",platform_touse, device_touse);
 

+ 1 - 1
opencl/gaussian/clutils.h

@@ -72,7 +72,7 @@ typedef double cl_time;
 cl_context cl_init(char devicePreference='\0');
 
 // Creates a context given a platform and a device
-cl_context cl_init_context(int platform,int dev,int quiet=0);
+cl_context cl_init_context(int platform,int dev, int quiet=0, int use_gpu= 0);
 
 // Releases resources used by clutils
 void    cl_cleanup();

+ 8 - 3
opencl/gaussian/gaussianElim.cpp

@@ -77,11 +77,11 @@ int main(int argc, char *argv[]) {
     
     // args
     char filename[200];
-    int quiet=1,timing=0,platform=-1,device=-1;
+    int quiet=1,timing=0,platform=-1,device=-1,use_gpu=-1;
     
     // parse command line
     if (parseCommandline(argc, argv, filename,
-			 &quiet, &timing, &platform, &device, &size)) {
+			 &quiet, &timing, &platform, &device, &size, &use_gpu)) {
     printUsage();
     return 0;
     }
@@ -403,7 +403,7 @@ float eventTime(cl_event event,cl_command_queue command_queue){
 
  // Ke Wang add a function to generate input internally
 int parseCommandline(int argc, char *argv[], char* filename,
-                     int *q, int *t, int *p, int *d, int *size){
+                     int *q, int *t, int *p, int *d, int *size, int *use_gpu){
     int i;
     if (argc < 2) return 1; // error
     // strncpy(filename,argv[1],100);
@@ -440,6 +440,10 @@ int parseCommandline(int argc, char *argv[], char* filename,
               i++;
               *d = atoi(argv[i]);
               break;
+            case 'g': // device
+              i++;
+              *use_gpu = atoi(argv[i]);
+              break;  
         }
       }
     }
@@ -464,6 +468,7 @@ void printUsage(){
   printf("\n");
   printf("-p [int]     Choose the platform (must choose both platform and device)\n");
   printf("-d [int]     Choose the device (must choose both platform and device)\n");
+  printf("-g [int]     1 for gpu and 0 for cpu\n");
   printf("\n");
   printf("\n");
   printf("Notes: 1. The filename is required as the first parameter.\n");

+ 1 - 1
opencl/gaussian/gaussianElim.h

@@ -24,7 +24,7 @@ float *OpenClGaussianElimination(
 
 void printUsage();
 int parseCommandline(int argc, char *argv[], char* filename,
-                     int *q, int *t, int *p, int *d, int *size);
+                     int *q, int *t, int *p, int *d, int *size, int *use_gpu);
                      
 void InitPerRun(int size,float *m);
 void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int timing);

+ 1 - 1
opencl/gaussian/run-cpu

@@ -1 +1 @@
-./gaussian -f ../../data/gaussian/matrix1024.txt -p 1 -d 0
+./gaussian -f ../../data/gaussian/matrix1024.txt -p 1 -d 0 -g 0

+ 1 - 0
opencl/gaussian/run-gpu

@@ -0,0 +1 @@
+./gaussian -f ../../data/gaussian/matrix1024.txt -p 0 -d 0 -g 1