Skip to content

Commit

Permalink
Merge pull request #48 from cancan101/ipython_build_options_2
Browse files Browse the repository at this point in the history
Added more magics to IPython
  • Loading branch information
inducer committed Jul 18, 2014
2 parents 269515e + 855c451 commit 7aad056
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
2 changes: 2 additions & 0 deletions doc/misc.rst
Expand Up @@ -39,6 +39,8 @@ and then use the ``%%cl_kernel`` 'cell-magic' command. See `this notebook

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"``.
There are also line magics: ``cl_load_edit_kernel`` which will load a file into the next cell (adding ``cl_kernel`` to the first line) and ``cl_kernel_from_file`` which will compile kernels from a file (as if you copy-and-pasted the contents of the file to a cell with ``cl_kernel``). Boths of these magics take options ``-f`` to specify the file and optionally ``-o`` for build options.

.. versionadded:: 2014.1

Guidelines
Expand Down
49 changes: 43 additions & 6 deletions pyopencl/ipython_ext.py
@@ -1,6 +1,6 @@
from __future__ import division

from IPython.core.magic import (magics_class, Magics, cell_magic)
from IPython.core.magic import (magics_class, Magics, cell_magic, line_magic)

import pyopencl as cl

Expand All @@ -13,8 +13,9 @@ def _try_to_utf8(text):

@magics_class
class PyOpenCLMagics(Magics):
@cell_magic
def cl_kernel(self, line, cell):
def _run_kernel(self, kernel, options):
kernel = _try_to_utf8(kernel)
options = _try_to_utf8(options).strip()
try:
ctx = self.shell.user_ns["cl_ctx"]
except KeyError:
Expand All @@ -33,13 +34,49 @@ def cl_kernel(self, line, cell):
raise RuntimeError("unable to locate cl context, which must be "
"present in namespace as 'cl_ctx' or 'ctx'")

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())
prg = cl.Program(ctx, kernel).build(options=options)

for knl in prg.all_kernels():
self.shell.user_ns[knl.function_name] = knl


@cell_magic
def cl_kernel(self, line, cell):
kernel = cell

opts, args = self.parse_options(line,'o:')
build_options = opts.get('o', '')

self._run_kernel(kernel, build_options)


def _load_kernel_and_options(self, line):
opts, args = self.parse_options(line,'o:f:')

build_options = opts.get('o')
kernel = self.shell.find_user_code(opts.get('f') or args)

return kernel, build_options


@line_magic
def cl_kernel_from_file(self, line):
kernel, build_options = self._load_kernel_and_options(line)
self._run_kernel(kernel, build_options)


@line_magic
def cl_load_edit_kernel(self, line):
kernel, build_options = self._load_kernel_and_options(line)
header = "%%cl_kernel"

if build_options:
header = "%s %s" % (header, build_options)

content = "%s\n\n%s" % (header, kernel)

self.shell.set_next_input(content)


def load_ipython_extension(ip):
ip.register_magics(PyOpenCLMagics)

0 comments on commit 7aad056

Please sign in to comment.