diff --git a/doc/misc.rst b/doc/misc.rst index 3fda67b1e9afa7811be889e072b3ec17665e6e4f..8acd53e1710ec846ce42033fb114cb3dd717b608 100644 --- a/doc/misc.rst +++ b/doc/misc.rst @@ -37,6 +37,8 @@ and then use the ``%%cl_kernel`` 'cell-magic' command. See `this notebook <http://nbviewer.ipython.org/urls/raw.githubusercontent.com/pyopencl/pyopencl/master/examples/ipython-demo.ipynb>`_ (which ships with PyOpenCL) for a demonstration. +You can pass build options to be used for building the program executable by using the ``-o`` flag on the first line of the cell (next to the ``%%cl_kernel`` directive). For example: `%%cl_kernel -o "-cl-fast-relaxed-math"``. + .. versionadded:: 2014.1 Guidelines diff --git a/examples/ipython-demo.ipynb b/examples/ipython-demo.ipynb index b0e8159c4d1f3579e79842dd96f0918350063cf6..bf646511d64a0e1b94e1ce8695eb2f9b4cd6c944 100644 --- a/examples/ipython-demo.ipynb +++ b/examples/ipython-demo.ipynb @@ -106,7 +106,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%%cl_kernel\n", + "%%cl_kernel -o \"-cl-fast-relaxed-math\"\n", "\n", "__kernel void sum_vector(__global const float *a,\n", "__global const float *b, __global float *c)\n", diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py index 41fa6a8927a3ccd0176b4a24b6587a60cdabc45b..dd5283fd7d6509ef7dd3db6db2d755137d91a669 100644 --- a/pyopencl/__init__.py +++ b/pyopencl/__init__.py @@ -178,6 +178,8 @@ class Program(object): def build(self, options=[], devices=None, cache_dir=None): if isinstance(options, str): options = [options] + elif isinstance(options, unicode): + options = [options.encode("utf8")] options = (options + _DEFAULT_BUILD_OPTIONS diff --git a/pyopencl/ipython_ext.py b/pyopencl/ipython_ext.py index 309a6830f06d6904e9ed5123edcf14931b275e75..2d7d42d7c78106490d556bbd5bfb67b8d80decdf 100644 --- a/pyopencl/ipython_ext.py +++ b/pyopencl/ipython_ext.py @@ -5,6 +5,12 @@ from IPython.core.magic import (magics_class, Magics, cell_magic) import pyopencl as cl +def _try_to_utf8(text): + if isinstance(text, unicode): + return text.encode("utf8") + return text + + @magics_class class PyOpenCLMagics(Magics): @cell_magic @@ -23,11 +29,13 @@ class PyOpenCLMagics(Magics): except KeyError: ctx = None - if ctx is None: + if ctx is None or not isinstance(ctx, cl.Context): raise RuntimeError("unable to locate cl context, which must be " "present in namespace as 'cl_ctx' or 'ctx'") - prg = cl.Program(ctx, cell.encode("utf8")).build() + opts, args = self.parse_options(line,'o:') + build_options = opts.get('o', '') + prg = cl.Program(ctx, _try_to_utf8(cell)).build(options=_try_to_utf8(build_options).strip()) for knl in prg.all_kernels(): self.shell.user_ns[knl.function_name] = knl