Skip to content

Count the workers as they change when computing the overhead - #517

Merged
basnijholt merged 2 commits into
python-adaptive:mainfrom
r0c0y:runner-overhead-scaling-workers
Dec 6, 2026
Merged

basnijholt merged 2 commits into
python-adaptive:mainfrom
r0c0y:runner-overhead-scaling-workers

Conversation

@r0c0y

@r0c0y r0c0y commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Description

overhead() divided each evaluation's time by the number of workers at the moment it finished (t / self._get_max_tasks()), and divided the sum by the elapsed time. That is right only while the number of workers stays the same. With a cluster that scales (#187, dask's adapt), the evaluations that finish after a scale-down are divided by the smaller number, so they count several times over, and the overhead goes negative. After a scale-up it is too high.

The overhead is now 1 - t_function / t_workers:

  • t_function is the time of all evaluations together, each measured from submitting the point to processing its result, as before.
  • t_workers is the worker-seconds the executor offered. The runner sums it piece by piece, and updates it whenever the number of workers or of pending points changes.

Between updates the capacity is max(n_workers, n_pending). When workers leave while points are still pending, those points count as well. Since t_function is at most the integral of the number of pending points (a failed evaluation is not counted), it can never exceed t_workers, so the overhead stays between 0 and 100.

With a fixed number of workers the result is the same as before: n_workers * elapsed. The number of workers is now read once per runner iteration, not once per finished point. For distributed, _get_ncores asks the scheduler (client.ncores()), so that is one scheduler call per iteration instead of one per finished point.

Measured with real threads, 0.05 s evaluations, 8 threads and _get_ncores reporting a schedule:

schedule main this branch
8 workers throughout 1.0% 1.6%
8 workers, then 1 -24.0% 1.3%
1 worker, then 8 3.7% 3.4%

Tests in test_runner.py:

  • test_overhead_is_the_share_of_unused_worker_time runs a fake clock and an executor whose futures the test completes, so the expected overhead is exact:

    • 2 workers throughout: 20%. That is the same as before, and main passes.
    • 4 workers down to 1 while 4 points are pending: 10%. main gives -157%.
    • 1 worker up to 3: 20%. main gives 33%.

    Counting only the workers (not the pending points), skipping either update, or keeping the old division each fails at least one case.

  • test_overhead_with_a_changing_number_of_workers covers the same three schedules with real threads, through a patched _get_ncores. The overhead must be at least 0 and below 50. On main the scale-down case fails.

The docstrings that gave the old formula now give the new one.

Fixes #187

Run here (macOS, on main at 2507701): nox -e pytest_min_deps-3.12 pytest_all_deps-3.12 pytest_min_deps-3.13 pytest_all_deps-3.13 pytest_typeguard pytest_rust_backend all pass, with all six new tests in every session (3.11 and 3.14 are not installed here), and pre-commit passes on the changed files, mypy included.

This touches runner.py in places #514 does not, and the two merge cleanly there. In test_runner.py they meet only in the import block at the top; whichever lands second, I will rebase it. With that resolved, this branch, current main and #514 (including d61bba6) together pass test_runner.py, test_learner1d.py and test_learnernd.py.

Checklist

  • Fixed style issues using pre-commit run --all (first install using pip install pre-commit)
  • pytest passed

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • (Code) style fix or documentation update
  • This change requires a documentation update

r0c0y added 2 commits December 6, 2026 06:52
overhead() divided each evaluation's time by the number of workers when
it finished and the sum by the elapsed time, which only holds while that
number stays the same. With a scaling cluster the evaluations that finish
after a scale-down counted several times over and the overhead went
negative (python-adaptive#187); after a scale-up it was too high. Divide the total
function time by the worker-seconds offered instead, summed piece by
piece as the number of workers or of pending points changes, with
max(n_workers, n_pending) as the capacity, so the overhead stays between
0 and 100. With a fixed number of workers the result is the same, and the
number of workers is now read once per iteration, not per finished point.

Fixes python-adaptive#187
…ling-workers

# Conflicts:
#	adaptive/tests/test_runner.py

@basnijholt basnijholt 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.

Looks great, thanks for fixing #187 and resolving the conflict with main so quickly! Merging now.

@basnijholt
basnijholt merged commit 121260d into python-adaptive:main Dec 6, 2026
17 checks passed
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.

overhead is reported incorrectly with adaptively scaling cluster

2 participants