-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: cartesian with next compatibility (#1202)
Add an example illustrating using gt4py.cartesian and gt4py.next computations next to each other using gt4py.next storages. Refactor GTFieldInterface and cleanup GTDimsInterface for next.
- Loading branch information
Showing
8 changed files
with
239 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"GT4Py - GridTools for Python\n", | ||
"\n", | ||
"Copyright (c) 2014-2023, ETH Zurich\n", | ||
"All rights reserved.\n", | ||
"\n", | ||
"This file is part the GT4Py project and the GridTools framework.\n", | ||
"GT4Py is free software: you can redistribute it and/or modify it under\n", | ||
"the terms of the GNU General Public License as published by the\n", | ||
"Free Software Foundation, either version 3 of the License, or any later\n", | ||
"version. See the LICENSE.txt file at the top-level directory of this\n", | ||
"distribution for a copy of the license or check <https://www.gnu.org/licenses/>.\n", | ||
"\n", | ||
"SPDX-License-Identifier: GPL-3.0-or-later" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Demonstrates gt4py.cartesian with gt4py.next compatibility" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"\n", | ||
"nx = 32\n", | ||
"ny = 32\n", | ||
"nz = 1\n", | ||
"dtype = np.float64" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Storages\n", | ||
"--\n", | ||
"\n", | ||
"We create fields using the gt4py.next constructors. These fields are compatible with gt4py.cartesian when we use \"I\", \"J\", \"K\" as the dimension names." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<link href=\"https://fonts.googleapis.com/icon?family=Material+Icons\" rel=\"stylesheet\"><script src=\"https://spcl.github.io/dace/webclient2/dist/sdfv.js\"></script>\n", | ||
"<link href=\"https://spcl.github.io/dace/webclient2/sdfv.css\" rel=\"stylesheet\">\n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"import gt4py.next as gtx\n", | ||
"\n", | ||
"allocator = gtx.itir_embedded # should match the executor\n", | ||
"# allocator = gtx.gtfn_cpu\n", | ||
"# allocator = gtx.gtfn_gpu\n", | ||
"\n", | ||
"# Note: for gt4py.next, names don't matter, for gt4py.cartesian they have to be \"I\", \"J\", \"K\"\n", | ||
"I = gtx.Dimension(\"I\")\n", | ||
"J = gtx.Dimension(\"J\")\n", | ||
"K = gtx.Dimension(\"K\", kind=gtx.DimensionKind.VERTICAL)\n", | ||
"\n", | ||
"domain = gtx.domain({I: nx, J: ny, K: nz})\n", | ||
"\n", | ||
"inp = gtx.as_field(domain, np.fromfunction(lambda x, y, z: x**2+y**2, shape=(nx, ny, nz)), dtype, allocator=allocator)\n", | ||
"out_cartesian = gtx.zeros(domain, dtype, allocator=allocator)\n", | ||
"out_next = gtx.zeros(domain, dtype, allocator=allocator)" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"gt4py.cartesian\n", | ||
"--" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import gt4py.cartesian.gtscript as gtscript\n", | ||
"\n", | ||
"cartesian_backend = \"numpy\"\n", | ||
"# cartesian_backend = \"gt:cpu_ifirst\"\n", | ||
"# cartesian_backend = \"gt:gpu\"\n", | ||
"\n", | ||
"@gtscript.stencil(backend=cartesian_backend)\n", | ||
"def lap_cartesian(\n", | ||
" inp: gtscript.Field[dtype],\n", | ||
" out: gtscript.Field[dtype],\n", | ||
"):\n", | ||
" with computation(PARALLEL), interval(...):\n", | ||
" out = -4.0 * inp[0, 0, 0] + inp[-1, 0, 0] + inp[1, 0, 0] + inp[0, -1, 0] + inp[0, 1, 0]\n", | ||
"\n", | ||
"lap_cartesian(inp=inp, out=out_cartesian, origin=(1, 1, 0), domain=(nx-2, ny-2, nz))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from gt4py.next import Field\n", | ||
"\n", | ||
"next_backend = gtx.itir_embedded\n", | ||
"# next_backend = gtx.gtfn_cpu\n", | ||
"# next_backend = gtx.gtfn_gpu\n", | ||
"\n", | ||
"Ioff = gtx.FieldOffset(\"I\", source=I, target=(I,))\n", | ||
"Joff = gtx.FieldOffset(\"J\", source=J, target=(J,))\n", | ||
"\n", | ||
"@gtx.field_operator\n", | ||
"def lap_next(inp: Field[[I, J, K], dtype]) -> Field[[I, J, K], dtype]:\n", | ||
" return -4.0 * inp + inp(Ioff[-1]) + inp(Ioff[1]) + inp(Joff[-1]) + inp(Joff[1])\n", | ||
"\n", | ||
"@gtx.program(backend=next_backend)\n", | ||
"def lap_next_program(inp: Field[[I, J, K], dtype], out: Field[[I, J, K], dtype]):\n", | ||
" lap_next(inp, out=out[1:-1, 1:-1, :])\n", | ||
"\n", | ||
"lap_next_program(inp, out_next, offset_provider={\"Ioff\": I, \"Joff\": J})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"assert np.allclose(out_cartesian.asnumpy(), out_next.asnumpy())" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.10.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.