Skip to content

grass.script: Add available_cpus and resolve_nprocs helpers - #7414

Merged
cwhite911 merged 2 commits into
OSGeo:mainfrom
petrasovaa:python-nprocs
Jun 13, 2026
Merged

cwhite911 merged 2 commits into
OSGeo:mainfrom
petrasovaa:python-nprocs

Conversation

@petrasovaa

Copy link
Copy Markdown
Contributor

We are using G_set_omp_num_threads that resolves nprocs <= 0, but in Python, we don't have equivalent, potentially resulting in bugs. This PR adds the function for resolving the nprocs and also function for getting available cpus that works better in containers.

  • available_cpus() — number of CPUs this process may actually use. Prefers affinity-aware sources
    (os.process_cpu_count on 3.13+, os.sched_getaffinity on Linux) over os.cpu_count, which reports the host total
    and overcounts in containers and cgroup-limited jobs.
  • resolve_nprocs(nprocs) — mirrors G_set_omp_num_threads() in lib/gis/omp_threads.c:
    • positive → used as-is
    • 0 → all available cores
    • negative → available + nprocs, clamped to at least 1

These functions are applied in the code base, resulting in some bug fixes:

t.rast.univar / t.rast3d.univar: nprocs=0 (documented as "use all available cores") was silently clamped to 1, so the tool ran serially. The tool entry points now apply gs.resolve_nprocs() so the documented G_OPT_M_NPROCS semantics take effect for 0 and negative values.

AI-assisted (Claude Code) — used for migration planning, edge case analysis, and drafting docstrings and tests.

  Affinity-aware CPU count and G_OPT_M_NPROCS resolution (positive
  as-is, 0 = all cores, negative reserves cores). Migrate ad-hoc
  cpu_count() sites in pygrass, jupyter, animation, and datacatalog
  GUI to the new helpers; all become container/cgroup aware.

  Fix t.rast.univar and t.rast3d.univar: nprocs=0 was silently
  clamped to 1 instead of using all available cores.

  New in grass.script public API in GRASS 8.6.
@github-actions github-actions Bot added GUI wxGUI related temporal Related to temporal data processing Python Related code is in Python libraries module tests Related to Test Suite notebook labels May 19, 2026
@cwhite911
cwhite911 enabled auto-merge (squash) June 11, 2026 02:10
@cwhite911
cwhite911 merged commit 6106e16 into OSGeo:main Jun 13, 2026
26 of 28 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Jun 13, 2026
@petrasovaa
petrasovaa deleted the python-nprocs branch June 14, 2026 02:49

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

I had these minor comments pending. Please, consider them for your next related PR.

:param nprocs: The maximum number of Module processes that can be
run in parallel. A positive value is used as-is,
``0`` or ``None`` means use all available CPUs, and
a negative value reserves that many CPUs (clamped

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.

This is.not clear. I don't see how it explains the behavior.


Mirrors the semantics of ``G_set_omp_num_threads()`` in
``lib/gis/omp_threads.c``: 0 means use all available cores, a positive
number is used as-is, a negative number means cpu_count + nprocs

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.

This is correct, but maybe also explaining the actual usage would be helpful (keep that many available, use the rest).

ninsbl pushed a commit to ninsbl/grass that referenced this pull request Jul 7, 2026
Affinity-aware CPU count and G_OPT_M_NPROCS resolution (positive
  as-is, 0 = all cores, negative reserves cores). Migrate ad-hoc
  cpu_count() sites in pygrass, jupyter, animation, and datacatalog
  GUI to the new helpers; all become container/cgroup aware.

  Fix t.rast.univar and t.rast3d.univar: nprocs=0 was silently
  clamped to 1 instead of using all available cores.

  New in grass.script public API in GRASS 8.6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GUI wxGUI related libraries module notebook Python Related code is in Python temporal Related to temporal data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants