diff --git a/doc/misc.rst b/doc/misc.rst index f1fc8cc83cd29a508271066187526b59ca5aa7cb..3fda67b1e9afa7811be889e072b3ec17665e6e4f 100644 --- a/doc/misc.rst +++ b/doc/misc.rst @@ -31,7 +31,7 @@ PyOpenCL comes with IPython integration, which lets you seamlessly integrate PyOpenCL kernels into your IPython notebooks. Simply load the PyOpenCL IPython extension using:: - %load_ext pyopencl.ipython + %load_ext pyopencl.ipython_ext 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>`_ diff --git a/examples/ipython-demo.ipynb b/examples/ipython-demo.ipynb index 1635dcc31c9b3a229328f4fede2f913f6d9a1c1e..b0e8159c4d1f3579e79842dd96f0918350063cf6 100644 --- a/examples/ipython-demo.ipynb +++ b/examples/ipython-demo.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:85c637b863a4bbbd3fb91eca8682d36d9874a53a6db35b18f1c53bb53b3c6bdc" + "signature": "sha256:81f3deed7cdc26b0fc756b3ee1eb6e8f9b1be96304ddfc6ff484d223c2b8a942" }, "nbformat": 3, "nbformat_minor": 0, @@ -19,8 +19,17 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 2 + "outputs": [ + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "/usr/lib/python2.7/pkgutil.py:186: ImportWarning: Not importing directory '/usr/lib/python2.7/dist-packages/enthought': missing __init__.py\n", + " file, filename, etc = imp.find_module(subname, path)\n" + ] + } + ], + "prompt_number": 1 }, { "cell_type": "markdown", @@ -33,7 +42,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%load_ext pyopencl.ipython" + "%load_ext pyopencl.ipython_ext" ], "language": "python", "metadata": {}, @@ -62,8 +71,8 @@ "stream": "stdout", "text": [ "Choose platform:\n", - "[0] <pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7f244be8e500>\n", - "[1] <pyopencl.Platform 'Intel(R) OpenCL' at 0x3adcef0>\n" + "[0] <pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7fc14f1b0080>\n", + "[1] <pyopencl.Platform 'Intel(R) OpenCL' at 0x32aed00>\n" ] }, { @@ -162,7 +171,7 @@ "output_type": "pyout", "prompt_number": 8, "text": [ - "<pyopencl._cl.Event at 0x39dac20>" + "<pyopencl._cl.Event at 0x7fc14f3fdf30>" ] } ], diff --git a/pyopencl/ipython.py b/pyopencl/ipython.py index 309a6830f06d6904e9ed5123edcf14931b275e75..ca5d3cea34ba236e9e478de2d9733c2f6c5d2c39 100644 --- a/pyopencl/ipython.py +++ b/pyopencl/ipython.py @@ -4,6 +4,9 @@ from IPython.core.magic import (magics_class, Magics, cell_magic) import pyopencl as cl +from warnings import warn +warn("pyopencl.ipython is deprecated. Use pyopencl.ipython_ext instead.") + @magics_class class PyOpenCLMagics(Magics): diff --git a/pyopencl/ipython_ext.py b/pyopencl/ipython_ext.py new file mode 100644 index 0000000000000000000000000000000000000000..309a6830f06d6904e9ed5123edcf14931b275e75 --- /dev/null +++ b/pyopencl/ipython_ext.py @@ -0,0 +1,37 @@ +from __future__ import division + +from IPython.core.magic import (magics_class, Magics, cell_magic) + +import pyopencl as cl + + +@magics_class +class PyOpenCLMagics(Magics): + @cell_magic + def cl_kernel(self, line, cell): + 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: + 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() + + for knl in prg.all_kernels(): + self.shell.user_ns[knl.function_name] = knl + + +def load_ipython_extension(ip): + ip.register_magics(PyOpenCLMagics)