瀏覽代碼

Implemented device selection for streamcluster

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.

Reused part of the available routine to parse command line arguments,
the parsing is actually split into two different phases.

Also the initialization of the context was always relying on the first
platform available, extended to actually support more platforms, and
changed the old way to create an OCL context that always took into
consideration only the firsr platform available ignoring the parameter.
Andrea Gussoni 8 年之前
父節點
當前提交
b037e5b08d
共有 4 個文件被更改,包括 38 次插入27 次删除
  1. 35 25
      opencl/streamcluster/CLHelper.h
  2. 1 1
      opencl/streamcluster/run-cpu
  3. 1 0
      opencl/streamcluster/run-gpu
  4. 1 1
      opencl/streamcluster/streamcluster.cpp

+ 35 - 25
opencl/streamcluster/CLHelper.h

@@ -216,7 +216,9 @@ string FileToString(const string fileName){
 	@date:		24/03/2011
 ------------------------------------------------------------*/
 char device_type[3];
-int device_id = 0;
+static int platform_idx;
+static int device_idx = 0;
+
 void _clCmdParams(int argc, char* argv[]){
 	for (int i = 0; i < argc; ++i){
 		switch (argv[i][1]){
@@ -229,15 +231,24 @@ void _clCmdParams(int argc, char* argv[]){
 					throw;
 				}
 				break;
-			  case 'd':	 //--d stands for device id
-				if (++i < argc){
-					sscanf(argv[i], "%d", &device_id);
-				}
-				else{
-					std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
-					throw;
-				}
-				break;
+		  case 'p':	 //--p stands for platform id
+  			if (++i < argc){
+  				sscanf(argv[i], "%d", &platform_idx);
+  			}
+  			else{
+  				std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  				throw;
+  			}
+  			break;
+      case 'd':	 //--d stands for device id
+  			if (++i < argc){
+  				sscanf(argv[i], "%d", &device_idx);
+  			}
+  			else{
+  				std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  				throw;
+  			}
+  			break;
 			default:
 				;
 		}
@@ -260,7 +271,7 @@ void _clCmdParams(int argc, char* argv[]){
 		get the number of devices and devices have no relationship with context
 	@date:		24/03/2011
 ------------------------------------------------------------*/
-void _clInit(string device_type, int device_id)throw(string){
+void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
 
 #ifdef PROFILE_
 	TE = 0;
@@ -310,8 +321,9 @@ void _clInit(string device_type, int device_id)throw(string){
     if (resultCL != CL_SUCCESS)
         throw (string("InitCL()::Error: Getting platform ids (clGetPlatformIDs)"));
 
-    // Select the target platform. Default: first platform 
-    targetPlatform = allPlatforms[1];
+    // Select the target platform.
+    printf("DEBUG %d \n", platform_idx);
+    targetPlatform = allPlatforms[platform_idx];
     for (int i = 0; i < numPlatforms; i++)
     {
         char pbuff[128];
@@ -401,10 +413,10 @@ void _clInit(string device_type, int device_id)throw(string){
 	   	 	throw(string("exception in _clInit -> clGetDeviceIDs -> ALL -> 2"));   		   	
    	   	}
    	}
-   if(device_id!=0){
-   	if(device_id>(deviceListSize-1))
+   if(device_idx!=0){
+   	if(device_idx>(deviceListSize-1))
    		throw(string("Invalidate device id"));
-   	DEVICE_ID_INUSED = device_id;
+   	DEVICE_ID_INUSED = device_idx;
    }
      
    	_clGetDeviceProperties(DEVICE_ID_INUSED, &prop);
@@ -419,16 +431,14 @@ void _clInit(string device_type, int device_id)throw(string){
 #ifdef	DEV_INFO
 	std::cout<<"--cambine: before creating context"<<std::endl;
 #endif
+
+    cl_device_type device_type_cl = (device_type == "gpu") ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
     cl_context_properties cprops[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)targetPlatform, 0 };
-    oclHandles.context = clCreateContext(0, 
-                                        deviceListSize, 
-                                        oclHandles.devices, 
-                                        NULL,
-										NULL,
-                                        &resultCL);
-
-    if ((resultCL != CL_SUCCESS) || (oclHandles.context == NULL))
-        throw (string("InitCL()::Error: Creating Context (clCreateContextFromType)"));
+    oclHandles.context = clCreateContextFromType(cprops, device_type_cl, NULL, NULL, NULL);
+    if (!oclHandles.context) {
+        printf("ERROR: clCreateContextFromType failed\n");
+        exit(1);
+    }
 #ifdef	DEV_INFO
 	std::cout<<"--cambine: create OCL context successfully!"<<std::endl;
 #endif

+ 1 - 1
opencl/streamcluster/run-cpu

@@ -1 +1 @@
-./streamcluster 10 20 16 65536 65536 1000 none output.txt 4 -t cpu -d 0
+./streamcluster 10 20 16 65536 65536 1000 none output.txt 4 -p 1 -d 0 -t cpu

+ 1 - 0
opencl/streamcluster/run-gpu

@@ -0,0 +1 @@
+./streamcluster 10 20 16 65536 65536 1000 none output.txt 4 -p 0 -d 0 -t gpu

+ 1 - 1
opencl/streamcluster/streamcluster.cpp

@@ -902,7 +902,7 @@ int main(int argc, char **argv)
   nproc = atoi(argv[9]);
   _clCmdParams(argc, argv);
   try{
-	  _clInit(device_type, device_id);
+	  _clInit(platform_idx, device_idx, device_type);
    }
    catch(std::string msg){
    	std::cout<<"exception caught in main function->"<<msg<<std::endl;