Skip to content

Guard watershed_dinf() against unbounded memory allocations (#1345)#1348

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

Guard watershed_dinf() against unbounded memory allocations (#1345)#1348
brendancol merged 1 commit into
mainfrom
issue-1345

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

Bytes per pixel

CPU: fd cast (8) + labels (8) + state int8 (1) + path_r int64 (8) + path_c int64 (8) = 33 B/px.

GPU (cupy path copies the device input to host, runs the CPU kernel, ships the result back): caller's flow_dir (8) + caller's pour_points (8) + final cp.asarray(out) (8) = 24 B/px on device.

D-inf carries a single real-valued angle, so the CPU footprint matches D8 rather than MFD's 8-channel fractions buffer.

Test plan

  • Five new tests in test_watershed_dinf.py::TestMemoryGuard:
    • test_numpy_huge_raster_raises patches available memory to 1 byte and expects MemoryError.
    • test_numpy_normal_input_succeeds runs a 10x10 raster through the guard with real RAM.
    • test_dask_path_skips_guard confirms the dask backend bypasses the guard even when memory is patched to 1 byte.
    • test_error_message_mentions_dimensions checks the error string includes grid shape and the "dask" hint.
    • test_cupy_huge_raster_raises is gated on @cuda_and_cupy_available and patches GPU memory to 1 byte.
  • pytest xrspatial/hydro/tests/test_watershed_dinf.py: 19/19 pass.
  • Full hydro suite: 643 pass. Only failure is the pre-existing flake test_basin_dask_temp_cleanup.

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.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol brendancol merged commit df600fb into main Apr 29, 2026
11 checks passed
@brendancol brendancol deleted the issue-1345 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_dinf: no memory guard on H*W working arrays

1 participant