|
| 1 | +--- |
| 2 | +title: Run PRIK in a Notebook |
| 3 | +description: Compile Fortran and C cells and reshape the generated API without leaving the notebook |
| 4 | +audience: users |
| 5 | +prerequisites: installation, IPython and Jupyter notebooks |
| 6 | +related: ../guide/notebooks.md, ../guide/c/pointers-arrays-and-strings.md, pythonic-blas.md |
| 7 | +status: maintained |
| 8 | +publication: reviewed |
| 9 | +--- |
| 10 | + |
| 11 | +# Run PRIK in a Notebook |
| 12 | + |
| 13 | +This tutorial compiles Fortran and C in notebook cells, calls them from Python, |
| 14 | +and then reshapes the generated API by editing its semantic contract — all in |
| 15 | +one session. |
| 16 | + |
| 17 | +<p class="prik-notebook-actions"> |
| 18 | +<a class="prik-primary-cta" href="https://colab.research.google.com/github/PyNumLab/prik/blob/main/examples/notebooks/quickstart.ipynb">▶ Run it in Colab</a> |
| 19 | +<a class="prik-secondary-cta" href="../../../examples/notebooks/quickstart.ipynb" download="quickstart.ipynb">⬇ Download the notebook</a> |
| 20 | +</p> |
| 21 | + |
| 22 | +The notebook runs top to bottom and builds real extension modules, so it needs a |
| 23 | +compiler. In Colab the first cell installs one. |
| 24 | + |
| 25 | +## 1. Load the extension |
| 26 | + |
| 27 | +```ipython |
| 28 | +%load_ext prik.jupyter |
| 29 | +``` |
| 30 | + |
| 31 | +## 2. Compile a Fortran cell |
| 32 | + |
| 33 | +`%%fortran` compiles the cell and publishes what it declares. A Fortran module |
| 34 | +becomes a notebook name: |
| 35 | + |
| 36 | +```ipython |
| 37 | +%%fortran |
| 38 | +module geometry |
| 39 | +contains |
| 40 | + real(8) function circle_area(radius) |
| 41 | + real(8), intent(in) :: radius |
| 42 | + circle_area = 3.141592653589793d0 * radius**2 |
| 43 | + end function |
| 44 | +end module |
| 45 | +``` |
| 46 | + |
| 47 | +```python |
| 48 | +area = geometry.circle_area(np.float64(2.0)) |
| 49 | +assert np.isclose(area, np.pi * 4) |
| 50 | +print(f"✅ circle_area(2.0) = {area} (expected {np.pi * 4})") |
| 51 | +``` |
| 52 | + |
| 53 | +```text |
| 54 | +✅ circle_area(2.0) = 12.566370614359172 (expected 12.566370614359172) |
| 55 | +``` |
| 56 | + |
| 57 | +Every result the notebook claims is asserted, so a ✅ means the cell really did |
| 58 | +that rather than the page saying so. |
| 59 | + |
| 60 | +## 3. Compile a C cell |
| 61 | + |
| 62 | +`%%c` publishes C functions directly. This one doubles an array in place and |
| 63 | +takes the element count the way C usually does: |
| 64 | + |
| 65 | +```ipython |
| 66 | +%%c |
| 67 | +#include <stddef.h> |
| 68 | +
|
| 69 | +void scale(size_t count, double *values) { |
| 70 | + for (size_t index = 0; index < count; ++index) { |
| 71 | + values[index] *= 2.0; |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +`double *values` becomes runtime-rank storage, so it accepts a NumPy array of |
| 77 | +any rank and writes through it. The count still has to be passed by hand, |
| 78 | +though NumPy already knows it: |
| 79 | + |
| 80 | +```python |
| 81 | +values = np.array([1.0, 2.0, 3.0]) |
| 82 | +scale(np.uintp(values.size), values) |
| 83 | +assert np.allclose(values, [2.0, 4.0, 6.0]) |
| 84 | +print(f"✅ scale(count, values) doubled in place: {values} (expected [2. 4. 6.])") |
| 85 | +``` |
| 86 | + |
| 87 | +```text |
| 88 | +✅ scale(count, values) doubled in place: [2. 4. 6.] (expected [2. 4. 6.]) |
| 89 | +``` |
| 90 | + |
| 91 | +## 4. Reshape the API with a contract |
| 92 | + |
| 93 | +`--pyi` compiles nothing. It keeps the source and hands back the semantic |
| 94 | +contract it derived, as an editable cell: |
| 95 | + |
| 96 | +```ipython |
| 97 | +%%c --pyi |
| 98 | +#include <stddef.h> |
| 99 | +
|
| 100 | +void scale(size_t count, double *values) { |
| 101 | + for (size_t index = 0; index < count; ++index) { |
| 102 | + values[index] *= 2.0; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +Jupyter and Colab insert the contract below the cell you just ran: |
| 108 | + |
| 109 | +```ipython |
| 110 | +%%pyi |
| 111 | +
|
| 112 | +# prik: source-sha256=<generated digest> |
| 113 | +
|
| 114 | +from prik.contracts import Float64, UInt64 |
| 115 | +
|
| 116 | +def scale( |
| 117 | + count: UInt64, |
| 118 | + values: Float64[...] |
| 119 | +) -> None: ... |
| 120 | +``` |
| 121 | + |
| 122 | +Edit it so the count comes from the array. `Arg(0).size` supplies it, and |
| 123 | +`Float64[:]` pins the rank to one. Keep the `# prik:` line, then run the cell: |
| 124 | + |
| 125 | +```ipython |
| 126 | +%%pyi |
| 127 | +
|
| 128 | +# prik: source-sha256=<generated digest> |
| 129 | +
|
| 130 | +from prik.contracts import Arg, Float64, native_call |
| 131 | +
|
| 132 | +@native_call([Arg(0).size, Arg(0)]) |
| 133 | +def scale(values: Float64[:]) -> None: ... |
| 134 | +``` |
| 135 | + |
| 136 | +Same C code, same compiler; only the Python API changed — `count` is gone: |
| 137 | + |
| 138 | +```python |
| 139 | +values = np.array([1.0, 2.0, 3.0]) |
| 140 | +scale(values) |
| 141 | +assert np.allclose(values, [2.0, 4.0, 6.0]) |
| 142 | +print(f"✅ scale(values) doubled in place: {values} (expected [2. 4. 6.])") |
| 143 | +``` |
| 144 | + |
| 145 | +```text |
| 146 | +✅ scale(values) doubled in place: [2. 4. 6.] (expected [2. 4. 6.]) |
| 147 | +``` |
| 148 | + |
| 149 | +## Where to go next |
| 150 | + |
| 151 | +- [IPython and Jupyter Notebooks](../guide/notebooks.md) covers every magic, |
| 152 | + its options, and the cell cache. |
| 153 | +- [C Pointers, Arrays, and Strings](../guide/c/pointers-arrays-and-strings.md) |
| 154 | + explains what `Float64[...]` accepts and how to narrow it. |
| 155 | +- [Design a Pythonic BLAS API](pythonic-blas.md) applies the same contract |
| 156 | + editing to a real library. |
0 commit comments