Skip to content

Let callers cap KD-tree threads (workers= keyword and SURFA_NUM_THREADS) - #90

Merged
jnolan14 merged 3 commits into
freesurfer:masterfrom
inhuszar:fix/kdtree-num-threads
Nov 12, 2026
Merged

jnolan14 merged 3 commits into
freesurfer:masterfrom
inhuszar:fix/kdtree-num-threads

Conversation

@inhuszar

Copy link
Copy Markdown
Contributor

Every KD-tree query used workers=-1, which oversubscribes cgroup-limited batch jobs (e.g. SLURM) with no
way for the caller to cap it. Each public entry point that queries a KD-tree (Mesh.nearest_vertex,
find_self_intersecting_faces, remove_self_intersections, surface_distance,
barycentric_spherical_map, and the four spherical resampling and map classes) now takes workers=, as
SciPy does. When it is not given, the optional SURFA_NUM_THREADS environment variable decides, so a batch
script can cap everything without code changes; with neither, all CPUs are used as before. Tests cover the
precedence, validation and every entry point.

inhuszar and others added 3 commits November 12, 2026 18:35
All KD-tree queries in surfa.mesh hard-code workers=-1, which makes SciPy
spawn one thread per CPU reported by the OS. On cgroup-limited batch jobs
(e.g. SLURM allocations of 1-2 cores on large nodes) this oversubscribes
the allocation and there is no way for the caller to cap it.

Add surfa.system.kdtree_workers(), which returns -1 by default (unchanged
behaviour) or the value of the optional SURFA_NUM_THREADS environment
variable (-1 or a positive integer; anything else raises ValueError), and
use it at every KD-tree query site:

  - Mesh.nearest_vertex
  - Mesh.find_self_intersecting_faces / remove_self_intersections
  - sphere.barycentric_spherical_map (spherical resampling)
  - mesh.surface_distance

Adds test/test_kdtree_workers.py (parsing, validation, and identical
results with and without the cap).

Originally committed in the inhuszar/surfa fork as part of 8ef40a9
("Bugfix: thread limit, overlay accumulation"), which covered only the
three call sites in mesh.py; this version extends the cap to the
spherical-resampling and surface-distance queries so the limit holds
process-wide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit b0b50f1)
…READS

A per-call keyword makes the thread cap visible and testable in calling code,
while the environment variable keeps capping batch jobs without code changes.
The keyword takes precedence; when it is None (the default) the variable
decides, and all CPUs are used when neither is given, as before.

kdtree_workers(workers=None) validates an explicit value (-1 or a positive
integer) and otherwise reads SURFA_NUM_THREADS. The keyword is added to
Mesh.nearest_vertex, Mesh.find_self_intersecting_faces,
Mesh.remove_self_intersections, surface_distance, barycentric_spherical_map,
SphericalResamplingNearest, SphericalResamplingBarycentric,
SphericalMapNearest and SphericalMapBarycentric.

Tests: explicit values, precedence over the variable, invalid values, and
every public entry point with the keyword (an invalid variable proves the
keyword is used), giving the same results as the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Nh3ictregns29GiD8fnqCm
The resampling and map classes require a sphere centred at the origin; the
random point cloud of the previous test was not, so it stopped at the first
resampling class. Use Fibonacci spheres pinned by the six axis points.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Nh3ictregns29GiD8fnqCm
@jnolan14
jnolan14 merged commit 8c5535f into freesurfer:master Nov 12, 2026
12 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants