diff --git a/doc/source/index.rst b/doc/source/index.rst index d14399cc48bb00f1b829be878cf83ae36a0b6db0..f1f1747fff99dd9e3a83d8f17f85db8f02082b27 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -36,7 +36,7 @@ Here's an example, to give you an impression:: a = numpy.random.rand(50000).astype(numpy.float32) b = numpy.random.rand(50000).astype(numpy.float32) - ctx = cl.Context() + ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) mf = cl.mem_flags diff --git a/doc/source/misc.rst b/doc/source/misc.rst index d13c8b4f789b7adeead9559aae412cb06f88c161..48345c8be1bd09c91d620d3cf23453a78d016b89 100644 --- a/doc/source/misc.rst +++ b/doc/source/misc.rst @@ -78,6 +78,7 @@ Version 0.91.5 :attr:`pyopencl.ImageFormat.dtype_size`, :attr:`pyopencl.ImageFormat.itemsize`. * Add missing :func:`pyopencl.enqueue_copy_buffer`. +* Add :func:`pyopencl.create_some_context`. Version 0.91.4 -------------- diff --git a/doc/source/reference.rst b/doc/source/reference.rst index fb00c2431f1887ded73d9c5249b319267cc16f38..93de58bd4b7af02d3b0a4d02f9348679bdc49221 100644 --- a/doc/source/reference.rst +++ b/doc/source/reference.rst @@ -103,6 +103,14 @@ Platforms, Devices and Contexts If neither is specified, a context with a *dev_type* of :attr:`device_type.DEFAULT` is created. + .. note:: + + Calling the constructor with no arguments will fail for recent + CL drivers that support the OpenCL ICD. If you want similar, + just-give-me-a-context-already behavior, we recommend + :func:`create_some_context`. See, e.g. this + `explanation by AMD `_. + .. versionchanged:: 0.91.2 Constructor arguments *dev_type* added. @@ -118,6 +126,15 @@ Platforms, Devices and Contexts |comparable| +.. function:: create_some_context(interactive=True) + + Create a :class:`Context` 'somehow'. + + If multiple choices for platform and/or device exist, *interactive* + is True, and *sys.stdin.isatty()* is also True, + then the user is queried about which device should be chosen. + Otherwise, a device is chosen in an implementation-defined manner. + Command Queues and Events ------------------------- diff --git a/examples/demo.py b/examples/demo.py index fb1499a7e82574353df119c5b4f310fe3627c83d..71f146eb87581e145f909f872e5d9c07a02b46b6 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -5,7 +5,7 @@ import numpy.linalg as la a = numpy.random.rand(50000).astype(numpy.float32) b = numpy.random.rand(50000).astype(numpy.float32) -ctx = cl.Context() +ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) mf = cl.mem_flags diff --git a/examples/demo_meta_codepy.py b/examples/demo_meta_codepy.py index c2838da00e182ec3d060b2f1f8c880f76f523725..8c2a827560e77e72d0dad71eb4d406cb1107fe29 100644 --- a/examples/demo_meta_codepy.py +++ b/examples/demo_meta_codepy.py @@ -8,7 +8,7 @@ macroblock_count = 33 dtype = numpy.float32 total_size = local_size*thread_strides*macroblock_count -ctx = cl.Context() +ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) a = numpy.random.randn(total_size).astype(dtype) diff --git a/examples/matrix-multiply.py b/examples/matrix-multiply.py index 24c76fd8bfee07d73e386f0c736f6db05e863be9..3dff0ff1a353392c5e295b3e3a5881b618a970f6 100644 --- a/examples/matrix-multiply.py +++ b/examples/matrix-multiply.py @@ -116,8 +116,7 @@ import numpy block_size = 16 - -ctx = cl.Context(dev_type=cl.device_type.ALL) +ctx = cl.create_some_context() for dev in ctx.devices: assert dev.local_mem_size > 0 @@ -161,7 +160,8 @@ mf = cl.mem_flags kernel_params = {"block_size": block_size, "w_a":a_width, "h_a":a_height, "w_b":a_height} -prg = cl.Program(ctx, kernel_code % kernel_params).build() +prg = cl.Program(ctx, kernel_code % kernel_params, + ).build(options="-cl-mad-enable -cl-fast-relaxed-math") kernel = prg.matrixMul #def __call__(self, queue, tgt, src, shape): diff --git a/examples/narray.py b/examples/narray.py index a91a4597c9b1d172c7acf760711fdbf8c18f242c..667f6717411e80e76670a951af55298b70b1ad47 100644 --- a/examples/narray.py +++ b/examples/narray.py @@ -3,7 +3,7 @@ import pyopencl as cl import numpy as np demo_r = np.empty( (500,5), dtype=np.uint32) -ctx = cl.Context(dev_type=cl.device_type.ALL) +ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) mf = cl.mem_flags diff --git a/examples/transpose.py b/examples/transpose.py index 83e8b48039cdc1b7c1feaf1de7276c69087fe6b5..ad86de3b7112d107173d78e6d52222b70e38e469 100644 --- a/examples/transpose.py +++ b/examples/transpose.py @@ -120,7 +120,7 @@ def transpose_using_cl(ctx, queue, cpu_src, cls): def check_transpose(): for cls in [NaiveTranspose, SillyTranspose, TransposeWithLocal]: print "checking", cls.__name__ - ctx = cl.Context(dev_type=cl.device_type.ALL) + ctx = cl.create_some_context() for dev in ctx.devices: assert dev.local_mem_size > 0 @@ -143,7 +143,7 @@ def check_transpose(): def benchmark_transpose(): - ctx = cl.Context(dev_type=cl.device_type.ALL) + ctx = cl.create_some_context() for dev in ctx.devices: assert dev.local_mem_size > 0 diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py index 7a736c956550abc4264bde8d4378e11b449a9902..04377a06d1e4c98eb90d90eed3c13fddc7723645 100644 --- a/pyopencl/__init__.py +++ b/pyopencl/__init__.py @@ -227,6 +227,56 @@ _add_functionality() +# convenience ----------------------------------------------------------------- +def create_some_context(interactive=True): + try: + import sys + if not sys.stdin.isatty(): + interactive = False + except: + interactive = False + + platforms = get_platforms() + + if not platforms: + raise Error("no platforms found") + elif len(platforms) == 1 or not interactive: + platform = platforms[0] + else: + print "Choose platform from these choices:" + for i, pf in enumerate(platforms): + print "[%d] %s" % (i, pf) + + answer = raw_input("Choice [0]:") + if not answer: + choice = 0 + else: + choice = int(answer) + + platform = platforms[choice] + + devices = platform.get_devices() + + if not devices: + raise Error("no devices found") + elif len(devices) == 17 or not interactive: + pass + else: + print "Choose device(s) from these choices:" + for i, dev in enumerate(devices): + print "[%d] %s" % (i, dev) + + answer = raw_input("Choice, comma-separated [0]:") + if not answer: + devices = [devices[0]] + else: + devices = [devices[int(i)] for i in answer.split(",")] + + return Context(devices) + + + + # backward compatibility ------------------------------------------------------ def create_context_from_type(dev_type, properties=None): from warnings import warn