/usr/lib/python2.7/dist-packages/pyopencl/ipython_ext.py is in python-pyopencl 2015.1-2build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | from __future__ import division
from IPython.core.magic import (magics_class, Magics, cell_magic, line_magic)
import pyopencl as cl
import sys
def _try_to_utf8(text):
if isinstance(text, unicode):
return text.encode("utf8")
return text
@magics_class
class PyOpenCLMagics(Magics):
def _run_kernel(self, kernel, options):
if sys.version_info < (3,):
kernel = _try_to_utf8(kernel)
options = _try_to_utf8(options).strip()
try:
ctx = self.shell.user_ns["cl_ctx"]
except KeyError:
ctx = None
if not isinstance(ctx, cl.Context):
ctx = None
if ctx is None:
try:
ctx = self.shell.user_ns["ctx"]
except KeyError:
ctx = 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, kernel).build(options=options.split())
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 -o "%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)
|