Skip to content

docs(nowcast): bound fit concurrency instead of leaving cores idle - #241

Closed
seabbs-bot wants to merge 3 commits into
mainfrom
fix/177-nowcast-thread-usage
Closed

seabbs-bot wants to merge 3 commits into
mainfrom
fix/177-nowcast-thread-usage

Conversation

@seabbs-bot

@seabbs-bot seabbs-bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

This is entirely from an agent so do not review until I have pinged for review as I will do a first pass

Summary

Closes #177 ("Nowcast docs are not using multiple cores correctly").

  • CI threading is already correct. .github/workflows/document.yaml
    passes julia_num_threads: 'auto' into the reusable documentation.yml
    workflow (PR fix(ci): pass julia_num_threads to docs workflow #194), which sets it as a job-level env: var. The
    julia-actions/julia-docdeploy step's "Build the documentation" step runs
    include("docs/make.jl") as a subprocess of that same job shell, so it
    inherits JULIA_NUM_THREADS=auto (confirmed by reading both the
    package's and the reusable workflow's YAML directly). CI was not the
    remaining problem.
  • Local task docs / task docs-fast has no thread count at all.
    Taskfile.yml is stamped "MANAGED by EpiAwarePackageTools.scaffold — do
    not edit by hand," and the scaffolded template
    (EpiAware/EpiAwarePackageTools.jl templates/Taskfile.yml) has the
    same gap, so this isn't fixable in this repo. Filed
    Scaffolded docs/docs-fast Taskfile tasks don't set a thread count, so local docs builds sample MCMC chains serially EpiAwarePackageTools.jl#380 upstream instead of patching it
    here.
  • The remaining, in-scope problem is on the nowcast page itself.
    MCMCThreads() caps a single fit at min(nchains, Threads.nthreads()) =
    2 threads (nchains is 2 throughout this page, for the cross-chain
    R-hat diagnostic). The page fits four independent models — naive,
    right-truncate, report-triangle, complete — one after another, so any
    threads beyond 2 sat idle for the whole page regardless of how many the
    build process actually had.

The fix

docs/src/case-studies/realtime-nowcast.md now runs the four fits through
asyncmap(nuts_fit, (...); ntasks = Threads.nthreads() ÷ 2) instead of
four sequential sample() calls. Each fit still asks MCMCThreads() for
two threads (one per chain), so nthreads() ÷ 2 is the largest number of
fits that can run at once without asking for more threads than exist:

  • 1-2 threads → ntasks = 1: fits run one at a time, in the same
    order as before. Identical to current main.
  • 4 threads → ntasks = 2: two fits run at once, each still getting
    its full two-chain allocation. No oversubscription.
  • More threads scale the same way, capping at 4 concurrent fits (there
    are only four).

What I measured vs. reasoned

I do not have a trustworthy CI-scale measurement of the real page, and I'm
not presenting one. Here's exactly what is and isn't backed by a number:

  • Measured, real dependency: AbstractMCMC's
    sample(..., MCMCThreads(), ...) warns and falls back to serial chains
    on a single thread (read from the installed
    AbstractMCMC/*/src/sample.jl, all versions in this repo's manifests);
    it does not error. A single-threaded build looks fine and is just slow,
    which is why this went unnoticed.

  • Measured, synthetic, wall time only: a stand-in for the four fits —
    each spawning two ~2s busy-CPU tasks, matching MCMCThreads()'s own
    two-tasks-per-fit shape, but with no Turing/Mooncake compilation, no AD,
    no BLAS — run at -t 1, -t 2, and -t 4 on this machine when it
    briefly had free capacity:

    threads serial bounded (asyncmap) unbounded spawn (not shipped)
    1 16.3s 16.3s 16.3s
    2 8.2s 8.3s 8.1s
    4 8.2s 4.2s 4.1s

    This is the basis for the ntasks bound, not a claim about the real
    page: at 2 threads (plausibly what a small GitHub-hosted runner has),
    the bounded version is indistinguishable from today's sequential order,
    as designed. At 4 threads it halves wall time, matching the theoretical
    minimum (total work / threads). An earlier draft that spawned all four
    fits unconditionally showed no wall-time penalty even at 2 threads in
    this synthetic test — Julia's scheduler multiplexes pure CPU-bound tasks
    fairly — which is why I don't have a timing argument against it. The
    reason I dropped it anyway is memory, not speed:

  • Not measured, reasoned: peak memory. I have no RSS numbers for
    either version, synthetic or real. The synthetic test's busy loop
    allocates negligible persistent state, so it can't show a memory
    difference even in principle. The structural argument for the bound is:
    unconditional spawning holds all four posteriors' model state and
    sampler state live simultaneously regardless of thread count, while
    ntasks = nthreads() ÷ 2 caps how many are live at once to the same
    number the thread budget would support running concurrently anyway. On
    a memory-constrained CI runner — and this docs build has already timed
    out in CI at 45 minutes, so it is not a machine with headroom to spare —
    that seemed like the wrong thing to risk for a benefit I can't
    demonstrate at the thread counts CI is likely to have.

  • Not measured at all: real wall time or memory for the actual
    nowcast fits (Turing + Mooncake, real NUTS, real BLAS threading) at any
    thread count, on this host or on CI. This dev machine was shared with
    several other concurrent agents for most of this investigation
    (~/.claude/hooks/compute-budget.sh read "red," load 16-100 on 16
    cores, for the majority of the session) and isn't a trustworthy stand-in
    for an isolated 2-4-core CI runner even when idle. I did not attempt it
    and am not reporting a speedup for the real page — only for the
    synthetic scheduling model above. This PR's own CI run is the first
    trustworthy signal on the real thing.

  • Verified locally (not performance, correctness): the docs
    environment instantiates, task format and task test-quality pass,
    task test-fast passes (1134/1134), and the page still resolves the
    same naive_chain/rt_chain/tri_chain/complete_chain/*_post
    variable names the rest of the page (Recent R_t, plots, summary stats,
    prior-vs-posterior) depends on.

Scope: the other seven MCMCThreads(), 250, 2 call sites

catalyst-ode.md, delays-dayofweek.md, renewal-negbin.md,
split-observations.md, and sir-ode.md (twice) all use the same
sample(..., MCMCThreads(), 250, 2) pattern. I checked each: none of them
fit multiple independent models back-to-back the way nowcast does.
sir-ode.md's two calls are a base SIR fit and a separate stochastic
ascertainment model introduced later in the tutorial, built and discussed
sequentially rather than being parallel-fittable without restructuring the
narrative; the rest have exactly one fit each. So the specific defect this
PR fixes — independent fits serialised for no reason — doesn't recur on
those pages, and closing #177 doesn't imply they're covered by this
change.

They do all share the more basic property that a single fit's
MCMCThreads() call never uses more than min(nchains, nthreads()) = 2
threads, however many the build has. That's a real, separate question
(is 2 chains still right, should chain count track thread count, etc.)
that I haven't investigated here — it's not what #177 is about, and I
don't want to bundle an unreviewed change to seven other pages' sampling
behaviour into this PR. Happy to open a follow-up issue if that's wanted.

Notes

This was opened by a bot. Please ping @seabbs for any questions.

seabbs-bot and others added 3 commits August 4, 2026 17:46
The nowcast case study's four sample() calls (naive, right-truncate,
report-triangle, complete) are independent but ran strictly
sequentially, so MCMCThreads()'s min(nchains, nthreads()) = 2 threads
per call was the most the page ever used, even when more were free.

Restructure so the four fits are spawned with Threads.@Spawn and
fetched afterwards, sharing whatever thread pool is available. Not
yet verified at CI-realistic thread counts (2-4); if that turns out to
oversubscribe rather than help, drop this in favour of the chain-count
route.

Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
Spawning all four fits unconditionally (previous commit) asks for
eight threads (four fits x two chains each via MCMCThreads()) and
keeps four posteriors' sampler state live at once, regardless of how
many threads are actually available. On a CI runner with two or four
threads that oversubscribes rather than parallelising, and multiplies
peak memory for no gain.

Cap concurrent fits at Threads.nthreads() / 2 with asyncmap instead,
matching the two-threads-per-fit budget exactly. Verified the
scheduling behaviour with a synthetic two-chains-per-fit workload at
nthreads = 1, 2 and 4: at 1-2 threads the bounded version is
indistinguishable from the original sequential order (no regression,
no added memory pressure); at 4 threads it halves wall time by running
two fits at a time, matching the theoretical minimum. Real NUTS/Mooncake
timings at CI's actual thread count are not yet confirmed; that needs a
real CI run rather than this host, which is shared with other agents
and not a trustworthy stand-in for a dedicated runner.

Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
@codecov

codecov Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unit 92.62% <ø> (+0.49%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Try this Pull Request!

Option 1: Julia Package Manager

Open Julia and type:

import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableTuringIDModels.jl", rev="fix/177-nowcast-thread-usage")
using ComposableTuringIDModels

Option 2: Local Checkout

If you have the repo locally:

git checkout fix/177-nowcast-thread-usage
julia --project=. -e "using Pkg; Pkg.instantiate()"

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

📖 Documentation preview is ready!

View the docs for this PR at: https://EpiAware.github.io/ComposableTuringIDModels.jl/previews/PR241/

This preview will be updated automatically when you push new commits.

@seabbs seabbs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

NO

@seabbs seabbs closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nowcast docs are not using multiple cores correctly

2 participants