Commit 82529d94083 by Pierre Augier

Update documentation with mybinder link

parent 7b2aa91910b
......@@ -43,6 +43,12 @@ accelerate modern Python-Numpy code with different accelerators (currently
Transonic run fine without any accelerators installed (of course without
speedup)!
.. |mybinder| image:: https://mybinder.org/badge_logo.svg
:target: https://mybinder.org/v2/gh/fluiddyn/transonic/master?urlpath=lab/tree/doc/ipynb/executed
:alt: mybinder
You can try Transonic online by clicking this button: |mybinder|.
.. warning ::
Transonic is still in an active development stage (see our `roadmap
......@@ -135,7 +141,7 @@ We start to have a good API to accelerate Python-Numpy code (functions, methods
and blocks of code). The default Transonic backend uses Pythran and works well.
`Here, we explain why Pythran is so great for Python users and why Transonic is
great for Pythran users
<https://transonic.readthedocs.io/en/latest/backends/pythran.html>`_ There are
<https://transonic.readthedocs.io/en/latest/backends/pythran.html>`_. There are
also (more experimental) backends for Cython and Numba.
.. note ::
......
......@@ -35,4 +35,4 @@ See also how to accelerate the same function in a Jupyter notebook with the
.. toctree::
:maxdepth: 1
../../ipynb/executed/fxfy
\ No newline at end of file
../../ipynb/executed/bench_fxfy
......@@ -134,7 +134,6 @@
],
"source": [
"norm = timeit_verbose(\"fxfy(ft, fv, theta)\", globals=loc)\n",
"print(f\"norm = {norm:.2e} s\")\n",
"\n",
"for backend in (\"numba\", \"pythran\"):\n",
" timeit_verbose(f\"fxfy_{backend}(ft, fv, theta)\", globals=loc, norm=norm)\n",
......
......@@ -6,7 +6,7 @@
"source": [
"## IPython + jit decorator\n",
"\n",
"We can define jit function in IPython (and Jupyter, which uses IPython). The only limitation is that the type variables have to be defined in the same cell as the function."
"Let's say that we have this kind of code:"
]
},
{
......@@ -15,8 +15,48 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"image = np.random.rand(1024, 1024)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def laplace(image):\n",
" \"\"\"Laplace operator in NumPy for 2D images.\"\"\"\n",
" laplacian = (\n",
" image[:-2, 1:-1] + image[2:, 1:-1] + image[1:-1, :-2] + image[1:-1, 2:]\n",
" - 4*image[1:-1, 1:-1]\n",
" )\n",
" thresh = np.abs(laplacian) > 0.05\n",
" return thresh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can define a jitted function in IPython (and Jupyter, which uses IPython). The only limitation is that everything needed for the function (even `np`) has to be (re)defined in the same cell!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"from transonic import jit\n",
"\n",
"@jit\n",
......@@ -31,56 +71,70 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We first call the function to launch the compilation (\"warmup the jit\"):"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"result = laplace_pythran(image)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the compilation is quit long, we explicitelly wait for the extension to benchmark the compiled version."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from transonic import wait_for_all_extensions\n",
"wait_for_all_extensions()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's benchmark the 2 functions:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Schedule pythranization of file /home/users/augier3pi/.transonic/pythran/__jit__/__ipython__47f8727e8bd9e9dff2a4415001759d2d/laplace_pythran.py\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"compile extension\n"
"laplace : 1.000 * norm\n",
"norm = 1.37e-02 s\n",
"laplace_pythran : 0.119 * norm\n",
"\n",
"It corresponds to a speedup of ~ 8.4!\n"
]
},
{
"data": {
"text/plain": [
"array([[False, True, True, ..., False, False, False],\n",
" [False, True, False, ..., False, False, False],\n",
" [ True, True, False, ..., False, False, False],\n",
" ...,\n",
" [False, False, False, ..., True, False, False],\n",
" [False, False, False, ..., True, False, False],\n",
" [False, False, False, ..., True, True, False]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from skimage.data import astronaut\n",
"from skimage.color import rgb2gray\n",
"from transonic.util import timeit_verbose\n",
"\n",
"image = astronaut()\n",
"image = rgb2gray(image)\n",
"namespace = {\"laplace\": laplace, \"laplace_pythran\": laplace_pythran, \"image\": image}\n",
"\n",
"laplace_pythran(image)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After a warmup period, the function `laplace_pythran` is going to be very efficient."
"norm = timeit_verbose(\"laplace(image)\", globals=namespace)\n",
"time_pythran = timeit_verbose(\"laplace_pythran(image)\", globals=namespace, norm=norm)\n",
"print(f\"\\nIt corresponds to a speedup of ~ {norm/time_pythran:.1f}!\")"
]
}
],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment