Commit 5eee3518e0e by Pierre Augier

Add fxfy.ipynb

parent bfd747581ce
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from transonic import jit\n",
"\n",
"\n",
"def fxfy(ft, fn, theta):\n",
" sin_theta = np.sin(theta)\n",
" cos_theta = np.cos(theta)\n",
" fx = cos_theta * ft - sin_theta * fn\n",
" fy = sin_theta * ft + cos_theta * fn\n",
" return fx, fy\n",
"\n",
"\n",
"def fxfy_loops(ft, fn, theta):\n",
" n0 = theta.size\n",
" fx = np.empty_like(ft)\n",
" fy = np.empty_like(fn)\n",
" for index in range(n0):\n",
" sin_theta = np.sin(theta[index])\n",
" cos_theta = np.cos(theta[index])\n",
" fx[index] = cos_theta * ft[index] - sin_theta * fn[index]\n",
" fy[index] = sin_theta * ft[index] + cos_theta * fn[index]\n",
" return fx, fy\n",
"\n",
"\n",
"fxfy_pythran = jit(backend=\"pythran\", native=True, xsimd=True)(fxfy)\n",
"fxfy_numba = jit(backend=\"numba\")(fxfy)\n",
"fxfy_loops_pythran = jit(backend=\"pythran\", native=True, xsimd=True)(fxfy_loops)\n",
"fxfy_loops_numba = jit(backend=\"numba\")(fxfy_loops)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32mINFO \u001b[0m \u001b[34mSchedule pythranization of file /home/pierre/.transonic/pythran/__jit__/__ipython__6ca57c6e93778ae96e3e4b88a3e893ab/fxfy.py\u001b[0m\n",
"\u001b[32mINFO \u001b[0m \u001b[34mSchedule pythranization of file /home/pierre/.transonic/pythran/__jit__/__ipython__6ca57c6e93778ae96e3e4b88a3e893ab/fxfy_loops.py\u001b[0m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"transonic 0.4.1\n",
"pythran 0.9.3post1\n",
"numba 0.46.0\n",
"\n",
"compile extension\n",
"compile extension\n"
]
}
],
"source": [
"import numba\n",
"import pythran\n",
"from transonic import __version__, wait_for_all_extensions\n",
"from transonic.util import timeit\n",
"\n",
"theta = np.linspace(0, 2 * np.pi, 10000)\n",
"ft = 2.5 * theta\n",
"fv = 1.5 * theta\n",
"out = fxfy(ft, fv, theta)\n",
"out_loops = fxfy_loops(ft, fv, theta)\n",
"assert np.allclose(out, out_loops)\n",
"\n",
"print(\n",
" f\"transonic {__version__}\\n\"\n",
" f\"pythran {pythran.__version__}\\n\"\n",
" f\"numba {numba.__version__}\\n\"\n",
")\n",
"\n",
"loc = locals()\n",
"\n",
"def bench(call, norm=None):\n",
" ret = result = timeit(call, globals=loc)\n",
" if norm is None:\n",
" norm = result\n",
" result /= norm\n",
" print(f\"{call.split('(')[0]:33s}: {result:.3f} * norm\")\n",
" return ret\n",
"\n",
"# warmup\n",
"fxfy_pythran(ft, fv, theta)\n",
"fxfy_loops_pythran(ft, fv, theta)\n",
"fxfy_numba(ft, fv, theta)\n",
"fxfy_loops_numba(ft, fv, theta)\n",
"\n",
"wait_for_all_extensions()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fxfy : 1.000 * norm\n",
"norm = 2.68e-04 s\n",
"fxfy_numba : 0.956 * norm\n",
"fxfy_loops_numba : 0.917 * norm\n",
"fxfy_pythran : 0.217 * norm\n",
"fxfy_loops_pythran : 0.918 * norm\n"
]
}
],
"source": [
"norm = bench(\"fxfy(ft, fv, theta)\")\n",
"print(f\"norm = {norm:.2e} s\")\n",
"\n",
"for backend in (\"numba\", \"pythran\"):\n",
" bench(f\"fxfy_{backend}(ft, fv, theta)\", norm=norm)\n",
" bench(f\"fxfy_loops_{backend}(ft, fv, theta)\", norm=norm)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
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