-
Notifications
You must be signed in to change notification settings - Fork 52
Cartesian vs Next example #1202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
647efdc
Cartesian vs Next example
havogt 2393a29
Merge remote-tracking branch 'upstream/main' into cartesian_vs_next_e…
havogt ee54718
make gtx.Fields compatible with gt4py.cartesian
havogt 4281e70
add note about dimension names
havogt c670872
cleanup
havogt 90ac591
cleanup
havogt 78610d6
Update src/gt4py/next/common.py
havogt 3e3f288
address review comments
havogt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.