From 3987009cf4c357f67945ff7c282d80693dda3b0e Mon Sep 17 00:00:00 2001
From: Oliver Bock <oliver.bock@aei.mpg.de>
Date: Wed, 9 Mar 2011 17:30:21 +0100
Subject: [PATCH] Added crude feature to pass device ID as second command line
 argument (using lots of redundant code)

---
 example/main.cpp | 92 +++++++++++++++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 36 deletions(-)

diff --git a/example/main.cpp b/example/main.cpp
index d48267c..844673f 100644
--- a/example/main.cpp
+++ b/example/main.cpp
@@ -369,55 +369,75 @@ int main (int argc, char * const argv[]) {
     } 
      
     device_id = NULL; 
-     
-    unsigned int i; 
-    for(i = 0; i < num_devices; i++) 
-    { 
+    unsigned int i = 0;
+
+    if (argc == 3) {
         cl_bool available; 
-        err = clGetDeviceInfo(device_ids[i], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &available, NULL); 
+        err = clGetDeviceInfo(device_ids[atoi(argv[2])], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &available, NULL); 
         if(err) 
         { 
-             printf("ERROR: Cannot check device availability of device # %d\n", i); 
+            printf("ERROR: Cannot check device availability of device # %d\n", atoi(argv[2])); 
         } 
-         
-        if(available) 
-        { 
-            device_id = device_ids[i]; 
-            char name[200]; 
-            err = clGetDeviceInfo(device_ids[i], CL_DEVICE_NAME, sizeof(name), name, NULL); 
-            if(err == CL_SUCCESS) 
-            { 
-                 printf("INFO: Using device %s...\n", name); 
-            } 
-            else 
-            { 
-                 printf("INFO: Using device # %d...\n", i); 
-            } 
 
-            break; 
+        if(available)
+        {
+            device_id = device_ids[atoi(argv[2])];
+        }
+        else
+        { 
+            printf("INFO: Device # %d not available for compute\n", atoi(argv[2])); 
+            return -1;
         } 
-        else 
+    }
+    else { 
+        for(i = 0; i < num_devices; i++) 
         { 
-            char name[200]; 
-            err = clGetDeviceInfo(device_ids[i], CL_DEVICE_NAME, sizeof(name), name, NULL); 
-            if(err == CL_SUCCESS) 
+            cl_bool available; 
+            err = clGetDeviceInfo(device_ids[i], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &available, NULL); 
+            if(err) 
             { 
-                 printf("INFO: Device %s not available for compute\n", name); 
+                printf("ERROR: Cannot check device availability of device # %d\n", i); 
             } 
-            else 
+         
+            if(available) 
             { 
-                 printf("INFO: Device # %d not available for compute\n", i); 
+                device_id = device_ids[i]; 
+                break; 
             } 
-        } 
+            else 
+            { 
+                char name[200]; 
+                err = clGetDeviceInfo(device_ids[i], CL_DEVICE_NAME, sizeof(name), name, NULL); 
+                if(err == CL_SUCCESS) 
+                { 
+                    printf("INFO: Device %s not available for compute\n", name); 
+                } 
+                else 
+                { 
+                    printf("INFO: Device # %d not available for compute\n", i); 
+                } 
+            }
+        }  
+    
+        if(!device_id) 
+        { 
+            log_error((char*)"None of the devices available for compute ... aborting test\n"); 
+            //test_finish(); 
+            return -1; 
+        }
     } 
-     
-    if(!device_id) 
+
+    char name[200]; 
+    err = clGetDeviceInfo(device_id, CL_DEVICE_NAME, sizeof(name), name, NULL); 
+    if(err == CL_SUCCESS) 
     { 
-        log_error((char*)"None of the devices available for compute ... aborting test\n"); 
-        //test_finish(); 
-        return -1; 
+        printf("INFO: Using device %s...\n", name); 
     } 
-     
+    else 
+    { 
+        printf("INFO: Using device # %d...\n", i); 
+    } 
+ 
     context = clCreateContext(0, 1, &device_id, NULL, NULL, &err); 
     if(!context || err)  
     { 
@@ -458,7 +478,7 @@ int main (int argc, char * const argv[]) {
         return -1; 
     } 
      
-    if(argc == 2) { // arguments are supplied in a file with arguments for a single run are all on the same line 
+    if(argc >= 2) { // arguments are supplied in a file with arguments for a single run are all on the same line 
         paramFile = fopen(argv[1], "r"); 
         if(!paramFile) { 
             log_error((char*)"Cannot open the parameter file\n"); 
-- 
GitLab