Skip to content

Commit 57107b2

Browse files
mmckyclaude
andcommitted
[numba.md] numba_ex3: run the solution at the n the exercise asks for
The statement says to use a substantial sample size such as n = 100_000_000, but the solution reused the shared 10^6 arrays --- at that size both timed cells display as 0.00 seconds, demonstrating nothing. The solution now draws its own 10^8 points (with a memory note), times the parallel version, and compares against speed_ex1's serial jitted function on the same arrays so the multithreading gain is visible on the page. The shared 10^6 arrays are unchanged: speed_ex1's pure-Python comparison would take minutes at 10^8. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 969028b commit 57107b2

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

lectures/numba.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,27 +723,50 @@ def calculate_pi_parallel(u_draws, v_draws):
723723
return area_estimate * 4 # dividing by radius**2
724724
```
725725

726-
Now let's see how fast it runs:
726+
As the exercise statement suggests, parallelization pays off when each thread
727+
has a substantial amount of work to do, so we draw a fresh, much larger set of
728+
points rather than reusing the arrays from above.
729+
730+
```{note}
731+
The two arrays below occupy about 1.6 GB of memory — reduce `n` if your
732+
machine is short on RAM.
733+
```
734+
735+
```{code-cell} ipython3
736+
n = 100_000_000
737+
rng = np.random.default_rng()
738+
u_big = rng.uniform(size=n)
739+
v_big = rng.uniform(size=n)
740+
```
741+
742+
Now let's see how fast it runs (the second call measures runtime without
743+
compilation time):
727744

728745
```{code-cell} ipython3
729746
with qe.Timer():
730-
calculate_pi_parallel(u_draws, v_draws)
747+
calculate_pi_parallel(u_big, v_big)
731748
```
732749

733750
```{code-cell} ipython3
734751
with qe.Timer():
735-
calculate_pi_parallel(u_draws, v_draws)
752+
calculate_pi_parallel(u_big, v_big)
736753
```
737754

738-
By switching parallelization on and off (selecting `True` or
739-
`False` in the `@jit` annotation), we can test the speed gain that
740-
multithreading provides on top of JIT compilation.
755+
For comparison, here is the serial jitted version from {ref}`speed_ex1` on the
756+
same points:
757+
758+
```{code-cell} ipython3
759+
with qe.Timer():
760+
calculate_pi(u_big, v_big)
761+
```
741762

742-
On our workstation, we find that parallelization provides a modest but
743-
worthwhile speed gain here.
763+
Comparing the last two timings, multithreading provides a substantial speed
764+
gain on top of JIT compilation — around 3x on our workstation.
744765

745766
(If you are executing locally, you will get different results, depending mainly
746-
on the number of CPUs on your machine.)
767+
on the number of CPUs on your machine — and at small sample sizes the
768+
parallel version can even be slower, because the gains cannot cover the cost of
769+
distributing work across threads.)
747770

748771
Notice that we drew all of the random points *before* the loop and passed them in
749772
as arrays, so the parallel loop only *reads* from memory.

0 commit comments

Comments
 (0)