Skip to content
Snippets Groups Projects
Commit a195f013 authored by Andreas Klöckner's avatar Andreas Klöckner
Browse files

Build IPython integration

parent 3026768f
No related branches found
No related tags found
No related merge requests found
......@@ -16,3 +16,4 @@ distribute*tar.gz
core
.coverage
htmlcov
.ipynb_checkpoints
{
"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
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment