{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "from pystencils.session import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Demo: Assignment collections and simplification\n", "\n", "\n", "## Assignment collections\n", "\n", "The assignment collection class helps to formulate and simplify assignments for numerical kernels. \n", "\n", "An AssignmentCollection is an ordered collection of assignments, together with an optional ordered collection of subexpressions, that are required to evaluate the main assignments. There are various simplification rules available that operate on AssignmentCollections." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by defining some stencil update rule. Here we also use the *pystencils* Field, note however that the assignment collection module works purely on the *sympy* level." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\left(a^{2} - c\\right) + {{f}_{N}^{0}} \\left(a^{2} + b\\right) + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} \\left(a^{2} - 2 c\\right)$$ $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\left(c^{2} - c\\right) + {{f}_{N}^{0}} \\left(b + c^{2}\\right) + {{f}_{S}^{0}} \\left(- a^{2} + c^{2}\\right) + {{f}_{W}^{0}} \\left(c^{2} - 2 c\\right)$$
" ], "text/plain": [ "Equation Collection for f_C^1,f_C^0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a,b,c = sp.symbols(\"a b c\")\n", "f = ps.fields(\"f(2) : [2D]\")\n", "\n", "a1 = ps.Assignment(f[0,0](1), (a**2 +b) * f[0,1] + \\\n", " (a**2 - c) * f[1,0] + \\\n", " (a**2 - 2*c) * f[-1,0] + \\\n", " (a**2) * f[0, -1])\n", "\n", "a2 = ps.Assignment(f[0,0](0), (c**2 +b) * f[0,1] + \\\n", " (c**2 - c) * f[1,0] + \\\n", " (c**2 - 2*c) * f[-1,0] + \\\n", " (c**2 - a**2) * f[0, -1])\n", "\n", "\n", "ac = ps.AssignmentCollection([a1, a2], subexpressions=[])\n", "ac" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*sympy* operations can be applied on an assignment collection: In this example we first expand the collection, then look for common subexpressions." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "expand_all = ps.simp.apply_to_all_assignments(sp.expand)\n", "expandedEc = expand_all(ac)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Subexpressions:
 $$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$ $$\\xi_{1} \\leftarrow a^{2}$$ $$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$ $$\\xi_{3} \\leftarrow c^{2}$$
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$ $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$
" ], "text/plain": [ "Equation Collection for f_C^1,f_C^0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ac_cse = ps.simp.sympy_cse(expandedEc)\n", "ac_cse" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Symbols occuring in assignment collections are classified into 3 categories:\n", "- free_symbols: symbols that occur in right-hand-sides but never on left-hand-sides\n", "- bound_symbols: symbols that occur on left-hand-sides\n", "- defined_symbols: symbols that occur on left-hand-sides of a main assignment" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAM0AAAAcBAMAAAAw8CbQAAAAMFBMVEX///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+5RS8WZSSNU0BrgdDd3TuUEO399NWLNEUJUZQV/xgtOh2/rGDHEYZ//J5cvBe1lv01RLPbqk3vuiNDG93HVyu+iGuf0GaZWX+4e+uzWiKiHq29ywo8yiAPY0+eQ5rOPjocPTIRXSkCGMirt4wy+oKMZ2v6ZaCtcak5IhKgscGI/qMPuwt4ycgwfgndFHGKJI0UpyJ6n/qHcpnxln+aqEB5/iwOLrUVO8pTqcMaojYwc3nuovAuGvggBGcqouA/v0F3waeG/NScEJoSXz3GojmHf5Cx7UB087qdoTHNUBqiPqCols4HSnRuvDz8SUVgvfXtxvRsnMp3n50L6pdG6fSX7XAs748vfrnFWHa8GuxpvzWDdrfXV1Vu7zG7VtdX/+0GMO/gXjcIRAhBP5DAAAAAASUVORK5CYII=\n", "text/latex": [ "$$\\left\\{{{f}_{E}^{0}}, {{f}_{N}^{0}}, {{f}_{S}^{0}}, {{f}_{W}^{0}}, a, b, c\\right\\}$$" ], "text/plain": [ "set([f_E__0, f_N__0, f_S__0, f_W__0, a, b, c])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ac_cse.free_symbols" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAALwAAAAcBAMAAAApe8h2AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInbvRDKJ3asQu82ZVGZbSvgjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADmklEQVRIDaVVQWhcVRQ9fzrzf3+SmQwuuqhixhGCSq2hm1KwdHbiKoNuggnkd7KwlUpjsVpbqEMLltoshkaUiJABQSO4CBVcShow2KwiLiwFydCFllLoWNpKMW167n3//f+mi2mbPHj33nPPfee/9/777wNOGxx0gISjj+BNwUKUr3cIjJ3pgBsDXjkeF1Rwr1Piz074dGisovUvGgfMVrG3U2FT8uG5BtW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\\xi_{0}, \\xi_{1}, \\xi_{2}, \\xi_{3}\\right\\}$$" ], "text/plain": [ "set([f_C__0, f_C__1, ξ₀, ξ₁, ξ₂, ξ₃])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ac_cse.bound_symbols" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAE0AAAAcBAMAAADW9S/RAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInbvRDKJ3asQu82ZVGZbSvgjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACAUlEQVQ4EYVSPWsUURQ9G3dmHONMtg+4o0JAEBJtrITpglWCf2A3WvhBAouICSlkiWChWwwxJBqEXbCJpAkpBFPIRiSKVUq7LFYhVZAEIhrIuffN7L5RwQfvnnPuPfPm3jcDWGtoyBJCnbU/EirDalDP5Z0vNUsXLqTCi/HTypMG4nM+meTF2CCa47ie0hTUh5fvRRYPs1qlhMcZN2h82BDl7me1SoIHGTeY+m6Lckc053/c5HlXTV3i68sl0x9g+05PHbO/g57v6c72v3zNG3FuXv9gIZ03f94O0Ne27i+QtgPTk/3eO0xPTibd97o1oPD5KJKE5du91uhahIRPZtrdxLAwr676h8ZeGIh6vDIO+M9UF7q3nZbLrGUrfAiMGq1thytb86W0yLmAvca984LLbRTfCYG2vQ7/jSoGmWswwvII0VlneFFlwJk6UI55gghZKxyXtx62yMvbDGdrDDgVAY+IN0XI4hf0qjwqItd5zfdtxuiTn+8V/F8E+DxrOCFy2/c3lsBriQGYllKRD82qZLDu+T4w0Db5oER0W8Bvoy1fUP9g2il0aGH5Wz+J9HuOu3ee97bGgVrGM8/K7BLDV+4qd+rb5/T8J8Gf3p8gdLi/bzJ4HSxK3vgCGVOX27iVZFzx7tYlxVWNz/PFnFOFe0WhOPd3yc44c/87yHKfAFzmbSKAO3ifAAAAAElFTkSuQmCC\n", "text/latex": [ "$$\\left\\{{{f}_{C}^{0}}, {{f}_{C}^{1}}\\right\\}$$" ], "text/plain": [ "set([f_C__0, f_C__1])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ac_cse.defined_symbols" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assignment collections can be splitted up, and merged together. For splitting, a list of symbols that occur on the left-hand-side in the main assignments has to be passed. The returned assignment collection only contains these main assignments together with all necessary subexpressions." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Subexpressions:
 $$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$ $$\\xi_{1} \\leftarrow a^{2}$$ $$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$
" ], "text/plain": [ "Equation Collection for f_C^1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ac_f0 = ac_cse.new_filtered([f(0)])\n", "ac_f1 = ac_cse.new_filtered([f(1)])\n", "ac_f1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note here that $\\xi_4$ is no longer part of the subexpressions, since it is not used in the main assignment of $f_C^1$.\n", "\n", "If we merge both collections together, we end up with the original collection." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Subexpressions:
 $$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$ $$\\xi_{1} \\leftarrow a^{2}$$ $$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$ $$\\xi_{3} \\leftarrow c^{2}$$
Main Assignments:
 $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$ $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$
" ], "text/plain": [ "Equation Collection for f_C^0,f_C^1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ac_f0.new_merged(ac_f1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also a method that inserts all subexpressions into the main assignments. This is the inverse operation of common subexpression elimination." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Main Assignments:
 $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} c^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b + {{f}_{N}^{0}} c^{2} - {{f}_{S}^{0}} a^{2} + {{f}_{S}^{0}} c^{2} + {{f}_{W}^{0}} c^{2} - 2 {{f}_{W}^{0}} c$$
" ], "text/plain": [ "Equation Collection for f_C^0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "assert sp.simplify(ac_f0.new_without_subexpressions().main_assignments.rhs - a2.rhs) == 0\n", "ac_f0.new_without_subexpressions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To evaluate an assignment collection, use the lambdify method. It is very similar to *sympy*s lambdify function." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAL4AAAAcBAMAAAAtjhhLAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInb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w//jB/8bpejnu/gt8A/WXaysoSbxjYAAAAASUVORK5CYII=\n", "text/latex": [ "$$\\left \\{ {{f}_{C}^{0}} : 75, \\quad {{f}_{C}^{1}} : -17\\right \\}$$" ], "text/plain": [ "{f_C__0: 75, f_C__1: -17}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evalFct = ac_cse.lambdify([f[0,1], f[1,0]], # new parameters of returned function\n", " fixed_symbols={a:1, b:2, c:3, f[0,-1]: 4, f[-1,0]: 5}) # fix values of other symbols\n", "evalFct(2,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "lambdify is rather slow for evaluation. The intended way to evaluate an assignment collection is *pystencils* i.e. create a fast kernel, that applies the update at every site of a structured grid. The collection can be directly passed to the create_kernel function." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "func = ps.create_kernel(ac_cse).compile()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simplification Strategies\n", "\n", "In above examples, we already applied simplification rules to assignment collections. Simplification rules are functions that take, as a single argument, an assignment collection and return an modified/simplified copy of it. The SimplificationStrategy class holds a list of simplification rules and can apply all of them in the specified order. Additionally it provides useful printing and reporting functions. \n", "\n", "We start by creating a simplification strategy, consisting of the expand and CSE simplifications we have already applied above:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "strategy = ps.simp.SimplificationStrategy()\n", "strategy.add(ps.simp.apply_to_all_assignments(sp.expand))\n", "strategy.add(ps.simp.sympy_cse)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This strategy can be applied to any assignment collection:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Subexpressions:
 $$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$ $$\\xi_{1} \\leftarrow a^{2}$$ $$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$ $$\\xi_{3} \\leftarrow c^{2}$$
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$ $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$
" ], "text/plain": [ "Equation Collection for f_C^1,f_C^0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strategy(ac)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The strategy can also print the simplification results at each stage. \n", "The report contains information about the number of operations after each simplification as well as the runtime of each simplification routine." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
OriginalTerm- 13 19 0 32
expand0.11 ms 13 26 0 39
sympy_cse3.25 ms 11 14 0 25
" ], "text/plain": [ ".Report at 0x7f9be404fda0>" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strategy.create_simplification_report(ac)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The strategy can also print the full collection after each simplification..." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
##### Initial Version
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\left(a^{2} - c\\right) + {{f}_{N}^{0}} \\left(a^{2} + b\\right) + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} \\left(a^{2} - 2 c\\right)$$ $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\left(c^{2} - c\\right) + {{f}_{N}^{0}} \\left(b + c^{2}\\right) + {{f}_{S}^{0}} \\left(- a^{2} + c^{2}\\right) + {{f}_{W}^{0}} \\left(c^{2} - 2 c\\right)$$
##### expand
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} a^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} a^{2} + {{f}_{N}^{0}} b + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} a^{2} - 2 {{f}_{W}^{0}} c$$ $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} c^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b + {{f}_{N}^{0}} c^{2} - {{f}_{S}^{0}} a^{2} + {{f}_{S}^{0}} c^{2} + {{f}_{W}^{0}} c^{2} - 2 {{f}_{W}^{0}} c$$
##### sympy_cse
Subexpressions:
 $$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$ $$\\xi_{1} \\leftarrow a^{2}$$ $$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$ $$\\xi_{3} \\leftarrow c^{2}$$
Main Assignments:
 $${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$ $${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$
" ], "text/plain": [ ".IntermediateResults at 0x7f9bad688dd8>" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strategy.show_intermediate_results(ac)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... or only specific assignments for better readability" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
##### Initial Version
$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\left(a^{2} - c\\right) + {{f}_{N}^{0}} \\left(a^{2} + b\\right) + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} \\left(a^{2} - 2 c\\right)$$
##### expand
$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} a^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} a^{2} + {{f}_{N}^{0}} b + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} a^{2} - 2 {{f}_{W}^{0}} c$$
##### sympy_cse
$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$
" ], "text/plain": [ ".IntermediateResults at 0x7f9bad688b00>" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strategy.show_intermediate_results(ac, symbols=[f(1)])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }