Skip to content

Commit

Permalink
Merge branch 'gt4py-workshop' of https://github.com/gridtools/gt4py i…
Browse files Browse the repository at this point in the history
…nto gt4py-workshop
  • Loading branch information
nfarabullini committed Nov 23, 2023
2 parents b88f8e3 + 56e9d72 commit a3f74c2
Show file tree
Hide file tree
Showing 9 changed files with 1,685 additions and 545 deletions.
139 changes: 139 additions & 0 deletions docs/user/next/exercises/1_simple_addition.ipynb
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
}
Loading

0 comments on commit a3f74c2

Please sign in to comment.