Skip to content

Commit 71ce918

Browse files
committed
Tighten viewshed Tier B memory estimate and avoid needless copy
The dask Tier B memory guard underestimated peak usage at 280 bytes/pixel. Actual peak during lexsort reaches ~360 bytes/pixel (sorted + unsorted event_list coexist) plus 8 bytes/pixel for the computed raster. Updated estimate to 368 bytes/pixel to prevent borderline OOM. Also use astype(copy=False) to skip the float64 copy when data is already float64.
1 parent 1920f9a commit 71ce918

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

xrspatial/viewshed.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ def _viewshed_cpu(
15601560
num_events = 3 * (n_rows * n_cols - 1)
15611561
event_list = np.zeros((num_events, 7), dtype=np.float64)
15621562

1563-
raster.data = raster.data.astype(np.float64)
1563+
raster.data = raster.data.astype(np.float64, copy=False)
15641564

15651565
_init_event_list(event_list=event_list, raster=raster.data,
15661566
vp_row=viewpoint_row, vp_col=viewpoint_col,
@@ -2167,7 +2167,9 @@ def _viewshed_dask(raster, x, y, observer_elev, target_elev):
21672167
cupy_backed = is_dask_cupy(raster)
21682168

21692169
# --- Tier B: full grid fits in memory → compute and run exact algo ---
2170-
r2_bytes = 280 * height * width
2170+
# Peak memory: event_list sort needs 2x 168*H*W + raster 8*H*W +
2171+
# visibility_grid 8*H*W ≈ 360 bytes/pixel, plus the computed raster.
2172+
r2_bytes = 360 * height * width + 8 * height * width # working + raster
21712173
avail = _available_memory_bytes()
21722174
if r2_bytes < 0.5 * avail:
21732175
raster_mem = raster.copy()

0 commit comments

Comments
 (0)