Skip to content

Guard stream_order_dinf() against unbounded memory allocations (#1350)#1354

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

Guard stream_order_dinf() against unbounded memory allocations (#1350)#1354
brendancol merged 1 commit into
mainfrom
issue-1350

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Fixes #1350.

Adds memory guards to stream_order_dinf, matching the pattern used for stream_order_d8 (#1331) and stream_link_dinf (#1347):

  • _check_memory: 40 B/px CPU (Strahler kernel peak: order f64, in_degree i32, max_in f64, cnt_max i32, queue_r i64, queue_c i64). Shreve drops max_in/cnt_max and is ~32 B/px. We budget for the worst case.
  • _check_gpu_memory: 40 B/px GPU (37 B/px kernel + 8 B/px fa_cp cast).
  • 50% of available RAM / GPU RAM threshold, same as the d8 fix.
  • Dask backend skips the guard since per-tile allocations are already bounded by chunk size.

D-inf encodes one continuous downstream angle per cell (two bracketing neighbors with proportional fractions), so there is no (8, H, W) per-neighbor weight buffer like MFD. The working set matches the d8 budget.

Tests (5):

  • numpy oversize raises MemoryError
  • numpy normal input succeeds
  • dask path bypasses the guard
  • error message mentions HxW dimensions and "dask"
  • cupy oversize raises MemoryError

Full hydro suite passes apart from the known flake test_stream_link_dask_temp_cleanup.

Adds _check_memory and _check_gpu_memory helpers, wired into the numpy
and cupy dispatch in stream_order_dinf(). Dask backend skips the guard
since per-tile allocations are already bounded by chunk size.

CPU peak working set is ~40 B/px for Strahler (order f64, in_degree i32,
max_in f64, cnt_max i32, queue_r i64, queue_c i64), ~32 B/px for Shreve.
We budget the worst case. GPU peak is 37 B/px kernel + 8 B/px fa_cp
cast; 40 B/px conservative. Threshold is 50% of available memory.

Tests (5): numpy oversize raises, numpy normal succeeds, dask bypasses,
error message mentions HxW + dask, cupy oversize raises.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol brendancol merged commit 64b2bac into main Apr 29, 2026
10 of 11 checks passed
@brendancol brendancol deleted the issue-1350 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.

stream_order_dinf: no memory guard on H*W working arrays

1 participant