Jelajahi Sumber

Implemented device selection for nn

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 since the previous implementation was reasonable.
Andrea Gussoni 8 tahun lalu
induk
melakukan
ae64cd4da3

+ 8 - 23
opencl/nn/clutils.cpp

@@ -238,9 +238,9 @@ cl_context cl_init(char devicePreference)
 
 
     return context;
     return context;
 }
 }
-cl_context cl_init_context(int platform, int dev,int quiet) {
-    int printInfo=1;
-    if (platform >= 0 && dev >= 0) printInfo = 0;
+cl_context cl_init_context(int platform_id, int device_id, int quiet, int use_gpu) {
+  int printInfo=1;
+  if (platform_id >= 0 && device_id >= 0) printInfo = 0;
 	cl_int status;
 	cl_int status;
 	// Used to iterate through the platforms and devices, respectively
 	// Used to iterate through the platforms and devices, respectively
 	cl_uint numPlatforms;
 	cl_uint numPlatforms;
@@ -321,8 +321,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) printf("Enter Platform and Device No (Seperated by Space) \n");
 	if (printInfo) scanf("%d %d", &platform_touse, &device_touse);
 	if (printInfo) scanf("%d %d", &platform_touse, &device_touse);
 	else {
 	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);
 	if (!quiet) printf("Using Platform %d \t Device No %d \n",platform_touse, device_touse);
 
 
@@ -342,23 +342,8 @@ cl_context cl_init_context(int platform, int dev,int quiet) {
 					devices, NULL);
 					devices, NULL);
 	if(cl_errChk(status,"Error in Getting Devices\n",true)) exit(1);
 	if(cl_errChk(status,"Error in Getting Devices\n",true)) exit(1);
 
 
-
-	//!Check if Device requested is a CPU or a GPU
-	cl_device_type dtype;
-	device = devices[device_touse];
-	status = clGetDeviceInfo(devices[device_touse],
-					CL_DEVICE_TYPE,
-					sizeof(dtype),
-					(void *)&dtype,
-					NULL);
-	if(cl_errChk(status,"Error in Getting Device Info\n",true)) exit(1);
-	if(dtype == CL_DEVICE_TYPE_GPU) {
-	  if (!quiet) printf("Creating GPU Context\n\n");
-	}
-	else if (dtype == CL_DEVICE_TYPE_CPU) {
-      if (!quiet) printf("Creating CPU Context\n\n");
-	}
-	else perror("This Context Type Not Supported\n");
+  // Selector for CPU/GPU on the basis of the use_gpu parameter
+	cl_device_type device_type= use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
 
 
 	cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM,
 	cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM,
 		(cl_context_properties)(platforms[platform_touse]), 0};
 		(cl_context_properties)(platforms[platform_touse]), 0};
@@ -366,7 +351,7 @@ cl_context cl_init_context(int platform, int dev,int quiet) {
 	cl_context_properties *cprops = cps;
 	cl_context_properties *cprops = cps;
 
 
 	context = clCreateContextFromType(
 	context = clCreateContextFromType(
-					cprops, (cl_device_type)dtype,
+					cprops, device_type,
 					NULL, NULL, &status);
 					NULL, NULL, &status);
 	if(cl_errChk(status, "creating Context",true)) {
 	if(cl_errChk(status, "creating Context",true)) {
 		exit(1);
 		exit(1);

+ 1 - 1
opencl/nn/clutils.h

@@ -72,7 +72,7 @@ typedef double cl_time;
 cl_context cl_init(char devicePreference='\0');
 cl_context cl_init(char devicePreference='\0');
 
 
 // Creates a context given a platform and a device
 // 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
 // Releases resources used by clutils
 void    cl_cleanup();
 void    cl_cleanup();

+ 9 - 4
opencl/nn/nearestNeighbor.cpp

@@ -13,12 +13,12 @@ int main(int argc, char *argv[]) {
   int i;
   int i;
   // args
   // args
   char filename[100];
   char filename[100];
-  int resultsCount=10,quiet=0,timing=0,platform=-1,device=-1;
+  int resultsCount=10,quiet=0,timing=0,platform=-1,device=-1,use_gpu=-1;
   float lat=0.0,lng=0.0;
   float lat=0.0,lng=0.0;
   
   
   // parse command line
   // parse command line
   if (parseCommandline(argc, argv, filename,&resultsCount,&lat,&lng,
   if (parseCommandline(argc, argv, filename,&resultsCount,&lat,&lng,
-                     &quiet, &timing, &platform, &device)) {
+                     &quiet, &timing, &platform, &device, &use_gpu)) {
     printUsage();
     printUsage();
     return 0;
     return 0;
   }
   }
@@ -36,7 +36,7 @@ int main(int argc, char *argv[]) {
 
 
   if (resultsCount > numRecords) resultsCount = numRecords;
   if (resultsCount > numRecords) resultsCount = numRecords;
 
 
-  context = cl_init_context(platform,device,quiet);
+  context = cl_init_context(platform,device,quiet,use_gpu);
   
   
   recordDistances = OpenClFindNearestNeighbors(context,numRecords,locations,lat,lng,timing);
   recordDistances = OpenClFindNearestNeighbors(context,numRecords,locations,lat,lng,timing);
 
 
@@ -259,7 +259,7 @@ void findLowest(std::vector<Record> &records,float *distances,int numRecords,int
 }
 }
 
 
 int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,float *lng,
 int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,float *lng,
-                     int *q, int *t, int *p, int *d){
+                     int *q, int *t, int *p, int *d, int *g){
     int i;
     int i;
     if (argc < 2) return 1; // error
     if (argc < 2) return 1; // error
     strncpy(filename,argv[1],100);
     strncpy(filename,argv[1],100);
@@ -299,6 +299,10 @@ int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,fl
               i++;
               i++;
               *d = atoi(argv[i]);
               *d = atoi(argv[i]);
               break;
               break;
+            case 'g': // device
+              i++;
+              *g = atoi(argv[i]);
+              break;              
         }
         }
       }
       }
     }
     }
@@ -326,6 +330,7 @@ void printUsage(){
   printf("\n");
   printf("\n");
   printf("-p [int]     Choose the platform (must choose both platform and device)\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("-d [int]     Choose the device (must choose both platform and device)\n");
+  printf("-g [int]     Choose 1 for GPU, 0 for CPU\n");
   printf("\n");
   printf("\n");
   printf("\n");
   printf("\n");
   printf("Notes: 1. The filename is required as the first parameter.\n");
   printf("Notes: 1. The filename is required as the first parameter.\n");

+ 1 - 1
opencl/nn/nearestNeighbor.h

@@ -46,5 +46,5 @@ int loadData(char *filename,std::vector<Record> &records,std::vector<LatLong> &l
 void findLowest(std::vector<Record> &records,float *distances,int numRecords,int topN);
 void findLowest(std::vector<Record> &records,float *distances,int numRecords,int topN);
 void printUsage();
 void printUsage();
 int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,float *lng,
 int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,float *lng,
-                     int *q, int *t, int *p, int *d);
+                     int *q, int *t, int *p, int *d, int *g);
 #endif
 #endif

+ 1 - 1
opencl/nn/run-cpu

@@ -1 +1 @@
-./nn filelist.txt -r 10 -lat 30 -lng 90 -p 1 -d 0
+./nn filelist.txt -r 10 -lat 30 -lng 90 -p 1 -d 0 -g 0

+ 1 - 0
opencl/nn/run-gpu

@@ -0,0 +1 @@
+./nn filelist.txt -r 10 -lat 30 -lng 90 -p 0 -d 0 -g 1