Skip to content

Commit

Permalink
Merge pull request #47 from cancan101/ipython_build_options
Browse files Browse the repository at this point in the history
Allow passing build options with IPython Magic
  • Loading branch information
inducer committed Jul 18, 2014
2 parents 9ff4fab + f332821 commit 269515e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/misc.rst
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/ipython-demo.ipynb
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions pyopencl/__init__.py
Expand Up @@ -176,6 +176,8 @@ def __getattr__(self, attr):
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
Expand Down
12 changes: 10 additions & 2 deletions pyopencl/ipython_ext.py
Expand Up @@ -5,6 +5,12 @@
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
Expand All @@ -23,11 +29,13 @@ def cl_kernel(self, line, cell):
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
Expand Down

0 comments on commit 269515e

Please sign in to comment.