Subject: create_some_context() can fail if multiple ICDs installed

From: "Rebecca N. Palmer" <[email-masked]>
Date: Mon, 21 Apr 2014 22:57:57 +0100

Source: pyopencl
Version: 2013.2-1
Tags: patch

In non-interactive mode, create_some_context currently always
chooses the first device of the first platform.  This fails if
this has no devices (eg. it is the wrong one for the available
hardware), and will be slow if if is the CPU.

This patch makes it search through the platforms for a device,
preferring a GPU if available.  As discussed earlier, this will allow 
making OpenCL just work (at least for hardware that has a free ICD) by 
defaulting to installing all ICDs.

This requires the "Make get_devices() return an empty list instead of 
erroring out" fix you just sent upstream.
Description: Don't fail if the first platform has no devices

In non-interactive mode, create_some_context currently always
chooses the first device of the first platform.  This fails if
this has no devices (eg. it is the wrong one for the available
hardware), and will be slow if if is the CPU.

This patch makes it search through the platforms for a device,
preferring a GPU if available.

Author: Rebecca Palmer
Forwarded: no

--- pyopencl-2013.2.orig/pyopencl/__init__.py
+++ pyopencl-2013.2/pyopencl/__init__.py
@@ -776,9 +776,22 @@ def create_some_context(interactive=True
             for i, pf in enumerate(platforms):
                 cc_print("[%d] %s" % (i, pf))
 
-        answer = get_input("Choice [0]:")
+        answer = get_input("Choice:")
         if not answer:
-            platform = platforms[0]
+            for pf in platforms:
+                devices = pf.get_devices(device_type=device_type.GPU)
+                if devices:
+                    platform = pf
+                    default_device = devices[0]
+                    break
+            if not devices: # No GPUs found
+                for pf in platforms:
+                    devices = pf.get_devices()
+                    if devices:
+                        platform = pf
+                        default_device = devices[0]
+                        break
+
         else:
             platform = None
             try:
@@ -828,9 +841,9 @@ def create_some_context(interactive=True
             for i, dev in enumerate(devices):
                 cc_print("[%d] %s" % (i, dev))
 
-        answer = get_input("Choice, comma-separated [0]:")
+        answer = get_input("Choice, comma-separated:")
         if not answer:
-            devices = [devices[0]]
+            devices = [default_device]
         else:
             devices = [parse_device(i) for i in answer.split(",")]