Skip to content

Guard watershed_mfd() against unbounded memory allocations (#1339)#1342

Merged
brendancol merged 1 commit into
mainfrom
issue-1339
Apr 29, 2026
Merged

Guard watershed_mfd() against unbounded memory allocations (#1339)#1342
brendancol merged 1 commit into
mainfrom
issue-1339

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

  • Add _check_memory and _check_gpu_memory guards (50% threshold) to the numpy and cupy dispatch branches of watershed_mfd
  • Budget: 97 B/pixel host (8-channel float64 fractions + labels + state + path buffers), 72 B/pixel GPU (input fractions + final cp.asarray)
  • Dask paths skip the guard, since per-tile allocations are bounded by the user's chunks

Closes #1339.

Test plan

  • pytest xrspatial/hydro/tests/test_watershed_mfd.py (17 passed: 12 existing + 5 new)
  • pytest xrspatial/hydro/tests/ (635 passed; one unrelated test_basin_d8::test_basin_dask_temp_cleanup flake from parallel agents sharing /tmp/xrs_bdry_*, passes in isolation)
  • New TestMemoryGuard cases: numpy raises, normal input succeeds, dask path skips guard, error message mentions dimensions and dask, cupy raises (gated on CUDA availability)

Adds host (97 B/pixel) and GPU (72 B/pixel) memory guards on the numpy
and cupy dispatch branches of watershed_mfd. The MFD float64 fractions
buffer (8 channels x 8 bytes = 64 B/pixel) is the dominant cost vs. the
8 B/pixel D8 flow direction array. Dask paths are unchanged -- per-tile
allocations are bounded by the user's chunk size.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol brendancol merged commit fea3403 into main Apr 29, 2026
11 checks passed
brendancol added a commit that referenced this pull request Apr 29, 2026
…1348)

Mirrors the pattern from watershed_d8 (#1330) and watershed_mfd (#1342).
CPU peak is 33 B/pixel (single-channel float64 angle, same as D8).
GPU peak is 24 B/pixel (caller's flow_dir + pour_points + result).
Guards run on numpy/cupy dispatch paths; dask paths skip them since
per-tile allocations are bounded by chunk size.
@brendancol brendancol deleted the issue-1339 branch May 4, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

watershed_mfd: no memory guard on H*W working arrays

1 participant