Skip to content

Commit e792422

Browse files
authored
Add memory guard to surface_distance geodesic dd_grid (#1129)
* Add sweep-performance design spec Parallel subagent triage + ralph-loop workflow for auditing all xrspatial modules for performance bottlenecks, OOM risk under 30TB dask workloads, and backend-specific anti-patterns. * Add sweep-performance implementation plan 7 tasks covering command scaffold, module scoring, parallel subagent dispatch, report merging, ralph-loop generation, and smoke tests. * Add sweep-performance slash command * Add memory guard to surface_distance geodesic dd_grid (#1128)
1 parent 93f59b7 commit e792422

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

xrspatial/surface_distance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,20 @@ def _precompute_dd_grid(lat_2d, lon_2d, dy, dx):
305305
"""
306306
H, W = lat_2d.shape
307307
n = len(dy)
308+
# Memory guard: dd_grid is (n_neighbors, H, W) float64
309+
estimated = n * H * W * 8
310+
try:
311+
from xrspatial.zonal import _available_memory_bytes
312+
avail = _available_memory_bytes()
313+
except ImportError:
314+
avail = 2 * 1024**3
315+
if estimated > 0.8 * avail:
316+
raise MemoryError(
317+
f"Geodesic dd_grid needs ~{estimated / 1e9:.1f} GB "
318+
f"({n} neighbors x {H}x{W} x 8 bytes) but only "
319+
f"~{avail / 1e9:.1f} GB available. Use planar mode "
320+
f"or downsample the raster."
321+
)
308322
dd_grid = np.zeros((n, H, W), dtype=np.float64)
309323

310324
for i in range(n):

0 commit comments

Comments
 (0)