-
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.
Merge branch 'gt4py-workshop' of https://github.com/gridtools/gt4py i…
…nto gt4py-workshop
- Loading branch information
Showing
9 changed files
with
1,685 additions
and
545 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,139 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2ead8b70", | ||
"metadata": {}, | ||
"source": [ | ||
"# 1. Simple Addition" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e7b501e0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"import gt4py.next as gtx" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "06113a1f", | ||
"metadata": {}, | ||
"source": [ | ||
"Next we implement the stencil and a numpy reference version, in order to verify them against each other." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "4c9f3427", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"I = gtx.Dimension(\"I\")\n", | ||
"J = gtx.Dimension(\"J\")\n", | ||
"size = 10" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8272b87c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def addition_numpy(a: np.array, b: np.array) -> np.array:\n", | ||
" c = a + b\n", | ||
" return c" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "05f2d09d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def addition ... # TODO fix this cell" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "db95ed00", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def test_addition(backend=None):\n", | ||
" domain = gtx.domain({I: size, J: size})\n", | ||
"\n", | ||
" a_data = np.fromfunction(lambda xx, yy: xx, domain.shape, dtype=float)\n", | ||
" a = gtx.as_field(domain, a_data, allocator=backend)\n", | ||
" b_data = np.fromfunction(lambda xx, yy: yy, domain.shape, dtype=float)\n", | ||
" b = gtx.as_field(domain, b_data, allocator=backend)\n", | ||
"\n", | ||
" c_numpy = addition_numpy(a.asnumpy(), b.asnumpy())\n", | ||
"\n", | ||
" c = gtx.zeros(domain, allocator=backend)\n", | ||
"\n", | ||
" addition(a, b, out=c, offset_provider={})\n", | ||
"\n", | ||
" assert np.allclose(c.asnumpy(), c_numpy)\n", | ||
"\n", | ||
" print(\"Result:\")\n", | ||
" print(c)\n", | ||
" print(c.asnumpy())\n", | ||
" \n", | ||
" # Plots\n", | ||
" fig, ax = plt.subplot_mosaic([\n", | ||
" ['a', 'b', 'c']\n", | ||
" ])\n", | ||
" ax['a'].imshow(a.asnumpy())\n", | ||
" ax['b'].imshow(b.asnumpy())\n", | ||
" ax['c'].imshow(c.asnumpy())\n", | ||
"\n", | ||
" print(\"\\nTest successful!\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "08502c34", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"test_addition()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"jupytext": { | ||
"formats": "ipynb,md:myst" | ||
}, | ||
"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.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.