{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## What is Numba?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numba is a *just-in-time*, *type-specializing*, *function* compiler. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numba" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numba.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### just-in-time function compiler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "the main API:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numba.jit?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numba.njit?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "both are decorators that compile functions into native code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simple example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@numba.njit\n", "def plus1(x):\n", " return x + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plus1(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "look at the generated assembly:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "print(plus1.inspect_asm(plus1.signatures[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### type-specializing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "decorated functions remember their compiled signatures" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plus1.signatures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "new argument types triggers new compilation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "using `plus1()` with `float64`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plus1(1.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plus1.signatures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "using `plus1()` with NumPy array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "plus1(np.arange(10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plus1.signatures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inspect the LLVM controlflow graph each of call signature. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "plus1.inspect_cfg(plus1.signatures[0]).display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plus1.inspect_cfg(plus1.signatures[1]).display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "plus1.inspect_cfg(plus1.signatures[2]).display()" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 4 }