Skip to content

Guard flow_path_d8() against unbounded memory allocations (#1364)#1368

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

Guard flow_path_d8() against unbounded memory allocations (#1364)#1368
brendancol merged 1 commit into
mainfrom
issue-1364

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

Closes #1364.

Add a 50%-of-available-memory guard to the eager numpy and cupy backends of flow_path_d8(). The dask and dask+cupy backends already stream chunks lazily and skip the guard.

The numpy path holds a float64 working copy of flow_dir, a float64 copy of start_points, and the float64 output. Measured peak via tracemalloc on a 2000x2000 grid: ~21 B/pixel; budgeted at 24 B/pixel. The cupy path detours through the CPU kernel, so it pays the same host budget plus device input + output (~32 B/pixel device).

Pattern follows #1318, #1319, #1303, #1334, #1338, #1339, #1344, #1355.

Test plan

  • pytest xrspatial/hydro/tests/test_flow_path_d8.py — 22 passed (17 existing + 5 new)
  • pytest xrspatial/hydro/tests/ — 731 passed (2 known-flaky temp-cleanup tests deselected)
  • numpy backend raises MemoryError when host memory is patched low
  • cupy backend raises MemoryError when GPU memory is patched low
  • dask backend bypasses the guard
  • normal-size raster passes the guard with real memory
  • error message names the grid dimensions and points to dask

Add a 50%-of-available-memory guard to the eager numpy and cupy
backends of flow_path_d8(). The numpy path holds a float64 working
copy of flow_dir, a float64 copy of start_points, and the float64
output (~21 B/pixel measured, 24 B/pixel budgeted). The cupy path
detours through the host kernel, so it pays the same CPU budget plus
device input + output.

Dask and dask+cupy backends already stream chunks lazily, so they
skip the guard.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol brendancol merged commit 31a7caa into main Apr 29, 2026
10 of 11 checks passed
@brendancol brendancol deleted the issue-1364 branch May 4, 2026 19:50
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.

flow_path_d8: no memory guard on H*W working arrays

1 participant