Skip to content

[numba.md] numba_ex3: run the solution at the n the exercise asks for - #601

Merged
mmcky merged 4 commits into
mainfrom
numba-ex3-large-n
Aug 12, 2026
Merged

[numba.md] numba_ex3: run the solution at the n the exercise asks for#601
mmcky merged 4 commits into
mainfrom
numba-ex3-large-n

Conversation

@mmcky

@mmcky mmcky commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Closes #579.

The exercise statement says to use a substantial sample size "such as n = 100_000_000", but the solution reused the shared n = 1_000_000 arrays. Benchmarked in the quantecon env (numba 0.62.1): at 10⁶ the jitted kernel runs in ~0.4 ms, so both timed cells in the published solution display 0.00 seconds — the parallelization the exercise teaches is invisible. Resolution chosen by @mmcky over the two options in the issue: make the solution actually do what the statement asks.

Changes

  • The solution draws its own u_big/v_big at n = 100_000_000 (with a note that the arrays occupy ~1.6 GB), leaving the shared 10⁶ arrays untouched — speed_ex1's pure-Python comparison would take minutes at 10⁸, so bumping the shared setup was not an option.
  • Adds a timed call of calculate_pi (the serial jitted function from speed_ex1) on the same points, so the serial-vs-parallel comparison the prose describes is actually visible in the built lecture instead of asking the reader to flip parallel=True themselves.
  • Prose updated to match, keeping the local-results caveat and extending it with why small samples can make the parallel version slower.

Validation

Ran the exact new cell sequence (shared setup → speed_ex1 compile → new solution) locally: parallel-with-compile 0.33 s, parallel 14 ms, serial 51 ms — a 3.6× visible speedup, π ≈ 3.14156 from all calls. Memory peaks at ~1.6 GB, comfortably inside every CI runner including the 7 GB macOS host; measured CI cost is a few seconds of extra execution.

🤖 Generated with Claude Code

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>
Copilot AI review requested due to automatic review settings August 4, 2026 00:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the numba_ex3 solution in the Numba lecture so it actually runs the Monte Carlo simulation at the substantial sample size (n = 100_000_000) requested by the exercise, making the intended serial-vs-parallel speed comparison visible in rendered outputs.

Changes:

  • Draws fresh large arrays (u_big, v_big) at n = 100_000_000 for numba_ex3, with a memory-usage note.
  • Adds a timed run of the serial jitted calculate_pi on the same large inputs to show the parallelization speedup directly.
  • Updates surrounding prose to explain why small n can hide (or reverse) parallel gains.

Comment thread lectures/numba.md
@mmcky mmcky added the in-work label Aug 4, 2026
@mmcky

mmcky commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author
  • get code and comparisons running first
  • edit prose to be QuantEcon style

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

@github-actions
github-actions Bot temporarily deployed to pull request August 4, 2026 00:51 Inactive
mmcky and others added 3 commits August 12, 2026 12:08
The `u_big`/`v_big` pair added for numba_ex3 stayed alive for the rest of
the notebook. Because `numba_ex_draw_speed` rebinds `u_draws`/`v_draws` to
a second pair of 1e8-element arrays, peak memory reached ~3.2 GB rather
than ~1.6 GB. That is comfortable on the g4dn.2xlarge CI runner but not on
the smaller machines the accompanying memory note is written for.

Delete the arrays after their last use, and fix "Lets" -> "Let's" plus the
trailing whitespace introduced alongside it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The Netlify preview of 6246b07 reports 0.1448s serial against 0.0353s
parallel at n=100_000_000, a 4.1x gain, but the solution claimed "around
3x on our workstation". Drop the multiple rather than restate it: these
lectures re-execute every build on a shared runner, so a pinned figure
drifts, and line 765 was the only hardcoded speedup in the file — the rest
describes gains qualitatively and lets the printed timings carry the fact.

The exercise preamble still warned "you should not expect huge gains
here", which was written for the old small-n setup and now contradicts the
near-linear scaling the solution demonstrates. Reframe it around giving
each thread enough work, which is why the exercise asks for a large n.

Prose only, so the execution cache for the code cells is unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mmcky mmcky added ready and removed in-work labels Aug 12, 2026
@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

@jstac here is a suggested edit to close #579.

Preview: https://6a7bd8835efc3a7f30eebe8b--epic-agnesi-957267.netlify.app/numba#exercises

@jstac

jstac commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Thanks @mmcky ! please merge when ready.

@mmcky
mmcky merged commit 7fce1f2 into main Aug 12, 2026
4 checks passed
@mmcky
mmcky deleted the numba-ex3-large-n branch August 12, 2026 03:37
@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

✅ Translation sync completed (fr)

Target repo: QuantEcon/lecture-python-programming.fr
Translation PR: QuantEcon/lecture-python-programming.fr#31
Files synced (1):

  • lectures/numba.md

@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

✅ Translation sync completed (zh-cn)

Target repo: QuantEcon/lecture-python-programming.zh-cn
Translation PR: QuantEcon/lecture-python-programming.zh-cn#90
Files synced (1):

  • lectures/numba.md

@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

✅ Translation sync completed (fa)

Target repo: QuantEcon/lecture-python-programming.fa
Translation PR: QuantEcon/lecture-python-programming.fa#151
Files synced (1):

  • lectures/numba.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[numba.md] numba_ex3 asks for n = 100_000_000 but the shared setup and solution use n = 1_000_000

3 participants