Skip to content

Commit bc6fb8a

Browse files
committed
Improve peak memory usage in unstructured grid conventions
1 parent 328a365 commit bc6fb8a

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/emsarray/conventions/ugrid.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import enum
99
import logging
1010
import pathlib
11+
import math
1112
import warnings
1213
from collections import defaultdict
1314
from collections.abc import Hashable, Iterable, Sequence
@@ -1092,7 +1093,7 @@ def grid_kinds(self) -> frozenset[UGridKind]:
10921093
items.append(UGridKind.edge)
10931094
return frozenset(items)
10941095

1095-
def _make_polygons(self) -> numpy.ndarray:
1096+
def _make_polygons(self):
10961097
"""Generate list of Polygons"""
10971098
topology = self.topology
10981099
# X,Y coords of each node
@@ -1115,10 +1116,15 @@ def _make_polygons(self) -> numpy.ndarray:
11151116
for unique_size in unique_sizes:
11161117
# Extract the face node data for every polygon of this size
11171118
indices = numpy.flatnonzero(polygon_sizes == unique_size)
1118-
nodes = numpy.ma.getdata(face_node)[indices, :unique_size]
1119-
coords = numpy.stack([node_x[nodes], node_y[nodes]], axis=-1)
1120-
# Generate the polygons directly in to their correct locations
1121-
shapely.polygons(coords, indices=indices, out=polygons)
1119+
chunk_size = 1000
1120+
chunk_count = math.ceil(len(indices) / chunk_size)
1121+
for chunk_index in range(chunk_count):
1122+
chunk_slice = slice(chunk_index * chunk_size, (chunk_index + 1) * chunk_size)
1123+
chunk_indices = indices[chunk_slice]
1124+
nodes = numpy.ma.getdata(face_node)[chunk_indices, :unique_size]
1125+
coords = numpy.stack([node_x[nodes], node_y[nodes]], axis=-1)
1126+
# Generate the polygons directly in to their correct locations
1127+
shapely.polygons(coords, indices=chunk_indices, out=polygons)
11221128

11231129
return polygons
11241130

tests/conventions/test_ugrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,13 @@ def test_has_valid_face_edge_connectivity():
988988

989989
@pytest.mark.memory_usage
990990
def test_make_polygons_memory_usage():
991-
dataset = make_dataset(width=500, height=400)
991+
dataset = make_dataset(width=600, height=600)
992992

993993
with track_peak_memory_usage() as tracker:
994994
assert len(dataset.ems.polygons) == dataset.ems.topology.face_count
995995

996996
logger.info(f"current memory usage: %d, peak memory usage: %d", tracker.current, tracker.peak)
997997

998-
target = 124_000_000
998+
target = 78_000_000
999999
assert tracker.peak < target, "Peak memory allocation is too large"
10001000
assert tracker.peak > target * 0.9, "Peak memory allocation is suspiciously small - did you improve things?"

0 commit comments

Comments
 (0)