Skip to content

Commit 267ffc3

Browse files
jstacclaude
andcommitted
Complete numpy_vs_numba_vs_jax lecture with timing comparisons
Added comprehensive comparisons between NumPy, Numba, and JAX for both vectorized and sequential operations: - Added Numba simple loop and parallel versions for vectorized example - Demonstrated nested prange parallelization and its limitations - Added detailed discussion of parallelization overhead and contention issues - Implemented sequential operation (quadratic map) in both Numba and JAX - Used JAX lax.scan with @partial(jax.jit, static_argnums) for cleaner code - Added timing code with separate runs to show compile vs cached performance - Included educational discussion without specific numbers (machine-independent) - Added explanation of reduction problem challenges with shared variable updates - Fixed spelling error: "implict" → "implicit" - Added missing punctuation All code examples tested and verified to run successfully. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c0f5f33 commit 267ffc3

File tree

2 files changed

+264
-261
lines changed

2 files changed

+264
-261
lines changed

lectures/numpy.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,10 @@ Note that the change to `b` has not affected `a`.
919919

920920

921921

922-
## Additional Functionality
922+
## Additional Features
923+
924+
Let's look at some other useful features of NumPy.
923925

924-
Let's look at some other useful things we can do with NumPy.
925926

926927
### Universal Functions
927928

@@ -1081,6 +1082,42 @@ We'll cover the SciPy versions in more detail {doc}`soon <scipy>`.
10811082
For a comprehensive list of what's available in NumPy see [this documentation](https://numpy.org/doc/stable/reference/routines.html).
10821083

10831084

1085+
### Implicit Multithreading
1086+
1087+
[Previously](need_for_speed) we discussed the concept of parallelization via multithreading.
1088+
1089+
NumPy tries to implement multithreading in much of its compiled code.
1090+
1091+
Let's look at an example to see this in action.
1092+
1093+
The next piece of code computes the eigenvalues of a large number of randomly
1094+
generated matrices.
1095+
1096+
It takes a few seconds to run.
1097+
1098+
```{code-cell} python3
1099+
n = 20
1100+
m = 1000
1101+
for i in range(n):
1102+
X = np.random.randn(m, m)
1103+
λ = np.linalg.eigvals(X)
1104+
```
1105+
1106+
Now, let's look at the output of the htop system monitor on our machine while
1107+
this code is running:
1108+
1109+
```{figure} /_static/lecture_specific/parallelization/htop_parallel_npmat.png
1110+
:scale: 80
1111+
```
1112+
1113+
We can see that 4 of the 8 CPUs are running at full speed.
1114+
1115+
This is because NumPy's `eigvals` routine neatly splits up the tasks and
1116+
distributes them to different threads.
1117+
1118+
1119+
1120+
10841121

10851122
## Exercises
10861123

0 commit comments

Comments
 (0)