Skip to content

Commit b01fbe9

Browse files
committed
data: support assigning a value read by window
`Data.__setitem__` already restricts the assigned value to the part of it the calling rank owns. Accept any value exposing a `shape` and slicing, such as a memory mapped array, an HDF5 dataset or a file reader, so that it is only read where it is assigned and never read in full by any rank.
1 parent de79615 commit b01fbe9

4 files changed

Lines changed: 91 additions & 5 deletions

File tree

‎.github/workflows/lint.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ jobs:
8888
for DOCKERFILE in docker/Dockerfile.*; \
8989
do \
9090
echo " Linting $DOCKERFILE"; \
91-
hadolint "$DOCKERFILE" \
91+
hadolint --ignore DL3066 "$DOCKERFILE" \
9292
|| exit 1; \
9393
done

‎devito/data/data.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def __setitem__(self, glb_idx, val):
324324
or data_global[j].size == 0
325325
if not skip:
326326
self.__setitem__(idx_global[j], data_global[j])
327-
elif isinstance(val, np.ndarray):
327+
elif isinstance(val, np.ndarray) or is_windowed(val):
328328
if self._is_decomposed:
329329
# `val` is replicated, `self` is decomposed -> `val` gets decomposed
330330
glb_idx = self._normalize_index(glb_idx)
@@ -342,7 +342,7 @@ def __setitem__(self, glb_idx, val):
342342
# * they are equal, or
343343
# * one of them is 1
344344
# Conceptually, below we apply the same rule
345-
val_idx = val_idx[len(val_idx)-val.ndim:]
345+
val_idx = val_idx[len(val_idx)-len(val.shape):]
346346
processed = []
347347
# Handle step size > 1
348348
for i, j in zip(glb_idx, val_idx, strict=False):
@@ -352,8 +352,13 @@ def __setitem__(self, glb_idx, val):
352352
else:
353353
processed.append(j)
354354
val_idx = as_tuple(processed)
355-
val = val[val_idx]
356-
super().__setitem__(glb_idx, val)
355+
elif is_windowed(val):
356+
# Read all of it, there is no decomposition to restrict it to
357+
val_idx = tuple(slice(0, s) for s in val.shape)
358+
else:
359+
val_idx = None
360+
361+
super().__setitem__(glb_idx, val if val_idx is None else val[val_idx])
357362
elif isinstance(val, Iterable):
358363
if self._is_decomposed:
359364
raise NotImplementedError("With MPI, data can only be set "

‎devito/data/utils.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@
1212
'index_dist_to_repl',
1313
'index_handle_oob',
1414
'index_is_basic',
15+
'is_windowed',
1516
'loc_data_idx',
1617
]
1718

1819

20+
def is_windowed(val):
21+
"""
22+
Whether `val` provides its values by window rather than from memory, such
23+
as a memory mapped array, an HDF5 dataset or any object exposing a `shape`
24+
and slicing. Such a value is only read where it is assigned, so with MPI a
25+
rank never reads more of it than the part it owns.
26+
"""
27+
return (not isinstance(val, np.ndarray) and hasattr(val, 'shape')
28+
and hasattr(val, '__getitem__'))
29+
30+
1931
class Index(Tag):
2032
pass
2133
NONLOCAL = Index('nonlocal') # noqa

‎tests/test_data.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,75 @@ def test_setitem_shorthands(self, mode):
17361736
assert np.all(result[3] == [[3, 2, 1, 0]])
17371737

17381738

1739+
class Windowed:
1740+
1741+
"""
1742+
A value read by window rather than held in memory, e.g. a memory mapped
1743+
array, an HDF5 dataset or a file reader. Records the windows it is read
1744+
with, so that a test can check what was needed.
1745+
"""
1746+
1747+
def __init__(self, array):
1748+
self.array = array
1749+
self.shape = array.shape
1750+
self.reads = []
1751+
1752+
def __getitem__(self, window):
1753+
self.reads.append(window)
1754+
return self.array[window]
1755+
1756+
1757+
class TestWindowedInsertion:
1758+
1759+
"""
1760+
Test assigning a value that is read by window, which every rank does for
1761+
the part of it that it owns.
1762+
"""
1763+
1764+
def test_insertion(self):
1765+
grid = Grid(shape=(6, 4))
1766+
f = Function(name='f', grid=grid, space_order=0)
1767+
glb = np.arange(24, dtype=grid.dtype).reshape(6, 4)
1768+
1769+
source = Windowed(glb)
1770+
f.data[:] = source
1771+
1772+
assert np.all(f.data == glb)
1773+
assert len(source.reads) == 1
1774+
1775+
@pytest.mark.parallel(mode=4)
1776+
def test_insertion_parallel(self, mode):
1777+
grid = Grid(shape=(6, 4))
1778+
f = Function(name='f', grid=grid, space_order=0)
1779+
g = Function(name='g', grid=grid, space_order=0)
1780+
glb = np.arange(24, dtype=grid.dtype).reshape(6, 4)
1781+
1782+
source = Windowed(glb)
1783+
f.data[:] = source
1784+
g.data[:] = glb
1785+
1786+
# Only the part owned by this rank was read, and it is what was set
1787+
assert np.all(f.data_local == g.data_local)
1788+
assert len(source.reads) == 1
1789+
assert glb[source.reads[0]].shape == f.data_local.shape
1790+
1791+
@pytest.mark.parallel(mode=4)
1792+
def test_insertion_subsection(self, mode):
1793+
grid = Grid(shape=(8, 8))
1794+
f = Function(name='f', grid=grid, space_order=0)
1795+
glb = np.arange(36, dtype=grid.dtype).reshape(6, 6)
1796+
1797+
source = Windowed(glb)
1798+
f.data[:] = 0.
1799+
f.data[1:7, 1:7] = source
1800+
1801+
expected = np.zeros(grid.shape, dtype=grid.dtype)
1802+
expected[1:7, 1:7] = glb
1803+
gathered = f.data_gather(rank=0)
1804+
if grid.distributor.myrank == 0:
1805+
assert np.all(gathered == expected)
1806+
1807+
17391808
class TestDataGather:
17401809

17411810
@pytest.mark.parallel(mode=4)

0 commit comments

Comments
 (0)