From a195f013d0be5c51bf82c8fcfbfde9e505548ee4 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Fri, 12 Jun 2015 12:04:07 -0500 Subject: [PATCH] Build IPython integration --- .gitignore | 1 + .../fortran/IPython Integration Demo.ipynb | 190 ++++++++++++++++++ loopy/ipython_ext.py | 28 +++ 3 files changed, 219 insertions(+) create mode 100644 examples/fortran/IPython Integration Demo.ipynb create mode 100644 loopy/ipython_ext.py diff --git a/.gitignore b/.gitignore index 322e3a7ca..c58651162 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ distribute*tar.gz core .coverage htmlcov +.ipynb_checkpoints diff --git a/examples/fortran/IPython Integration Demo.ipynb b/examples/fortran/IPython Integration Demo.ipynb new file mode 100644 index 000000000..1d4e8c328 --- /dev/null +++ b/examples/fortran/IPython Integration Demo.ipynb @@ -0,0 +1,190 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:5882f90e7a77d4dfd6033f1e8d0d1ff65e244204f12c1bfdf9e7c5abe5e4e11a" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Loopy IPython Integration Demo" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load_ext loopy.ipython_ext" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Without transform code" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%%fortran_kernel\n", + "\n", + "subroutine fill(out, a, n)\n", + " implicit none\n", + "\n", + " real*8 a, out(n)\n", + " integer n, i\n", + "\n", + " do i = 1, n\n", + " out(i) = a\n", + " end do\n", + "end" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print(fill)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "---------------------------------------------------------------------------\n", + "KERNEL: fill\n", + "---------------------------------------------------------------------------\n", + "ARGUMENTS:\n", + "a: ValueArg, type: float64\n", + "n: ValueArg, type: int32\n", + "out: GlobalArg, type: float64, shape: (n), dim_tags: (N0:stride:1)\n", + "---------------------------------------------------------------------------\n", + "DOMAINS:\n", + "[n] -> { [i] : i >= 0 and i <= -1 + n }\n", + "---------------------------------------------------------------------------\n", + "INAME IMPLEMENTATION TAGS:\n", + "i: None\n", + "---------------------------------------------------------------------------\n", + "INSTRUCTIONS:\n", + "[i] out[i] <- a # insn0\n", + "---------------------------------------------------------------------------\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## With transform code" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "split_amount = 128" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%%tfortran_kernel\n", + "\n", + "subroutine tr_fill(out, a, n)\n", + " implicit none\n", + "\n", + " real*8 a, out(n)\n", + " integer n, i\n", + "\n", + " do i = 1, n\n", + " out(i) = a\n", + " end do\n", + "end\n", + "\n", + "!$loopy begin\n", + "!\n", + "! tr_fill, = lp.parse_fortran(SOURCE)\n", + "! tr_fill = lp.split_iname(tr_fill, \"i\", split_amount,\n", + "! outer_tag=\"g.0\", inner_tag=\"l.0\")\n", + "! RESULT = [tr_fill]\n", + "!\n", + "!$loopy end" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print(tr_fill)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "---------------------------------------------------------------------------\n", + "KERNEL: tr_fill\n", + "---------------------------------------------------------------------------\n", + "ARGUMENTS:\n", + "a: ValueArg, type: float64\n", + "n: ValueArg, type: int32\n", + "out: GlobalArg, type: float64, shape: (n), dim_tags: (N0:stride:1)\n", + "---------------------------------------------------------------------------\n", + "DOMAINS:\n", + "[n] -> { [i_outer, i_inner] : i_inner >= -128i_outer and i_inner <= -1 + n - 128i_outer and i_inner >= 0 and i_inner <= 127 }\n", + "---------------------------------------------------------------------------\n", + "INAME IMPLEMENTATION TAGS:\n", + "i_inner: l.0\n", + "i_outer: g.0\n", + "---------------------------------------------------------------------------\n", + "INSTRUCTIONS:\n", + "[i_inner,i_outer] out[i_inner + i_outer*128] <- a # insn0\n", + "---------------------------------------------------------------------------\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/loopy/ipython_ext.py b/loopy/ipython_ext.py new file mode 100644 index 000000000..b1f9faaf2 --- /dev/null +++ b/loopy/ipython_ext.py @@ -0,0 +1,28 @@ +from __future__ import division + +from IPython.core.magic import (magics_class, Magics, cell_magic) + +import loopy as lp + + +@magics_class +class LoopyMagics(Magics): + @cell_magic + def fortran_kernel(self, line, cell): + result = lp.parse_fortran(cell.encode()) + + for knl in result: + self.shell.user_ns[knl.name] = knl + + @cell_magic + def tfortran_kernel(self, line, cell): + result = lp.parse_transformed_fortran( + cell.encode(), + transform_code_context=self.shell.user_ns) + + for knl in result: + self.shell.user_ns[knl.name] = knl + + +def load_ipython_extension(ip): + ip.register_magics(LoopyMagics) -- GitLab