|
7 | 7 |
|
8 | 8 | from devito import ( |
9 | 9 | CondEq, ConditionalDimension, Constant, Eq, Function, Grid, Inc, Operator, |
10 | | - SpaceDimension, SubGrid, TimeFunction, switchconfig |
| 10 | + SpaceDimension, SubDomain, SubGrid, TimeFunction, switchconfig |
11 | 11 | ) |
12 | 12 | from devito.ir.iet import Conditional, Expression, FindNodes, Iteration |
13 | 13 | from devito.symbolics import retrieve_indexed |
@@ -347,6 +347,11 @@ def test_time_subsampling(self): |
347 | 347 |
|
348 | 348 | Operator([Eq(u.forward, u + 1.), Eq(usave, u)]).apply(time_M=nt-2) |
349 | 349 |
|
| 350 | + # Time keeps its guard even where it is parallel |
| 351 | + op = Operator(Eq(usave, 1.)) |
| 352 | + assert FindNodes(Conditional).visit(op) |
| 353 | + assert all(i.dim is not ts for i in FindNodes(Iteration).visit(op)) |
| 354 | + |
350 | 355 | # time runs 0..nt-2, so only the slots whose time index is reached |
351 | 356 | # are written; `u` holds the step count at that point |
352 | 357 | nwritten = (nt - 2)//factor + 1 |
@@ -578,6 +583,22 @@ def test_with_openmp(self): |
578 | 583 | assert np.array_equal(g.data, data[::2]) |
579 | 584 |
|
580 | 585 |
|
| 586 | +class Middle(SubDomain): |
| 587 | + |
| 588 | + name = 'middle' |
| 589 | + |
| 590 | + def define(self, dimensions): |
| 591 | + # Thicknesses deliberately not multiples of the SubGrid factors |
| 592 | + return {d: ('middle', 2, 3) for d in dimensions} |
| 593 | + |
| 594 | + |
| 595 | +def middle_of(data): |
| 596 | + """`data` zeroed outside the `Middle` SubDomain.""" |
| 597 | + ret = np.zeros_like(data) |
| 598 | + ret[2:-3, 2:-3] = data[2:-3, 2:-3] |
| 599 | + return ret |
| 600 | + |
| 601 | + |
581 | 602 | class TestSubGrid: |
582 | 603 |
|
583 | 604 | """The `SubGrid` API.""" |
@@ -640,6 +661,22 @@ def test_copy_from_parent(self, factor): |
640 | 661 | fx, fy = sub.factor |
641 | 662 | assert np.array_equal(f.data, data[::fx, ::fy]) |
642 | 663 |
|
| 664 | + @pytest.mark.parametrize('factor', [2, 3, (2, 1), (3, 2)]) |
| 665 | + def test_copy_on_subdomain(self, factor): |
| 666 | + """Within a SubDomain, only the SubGrid points inside it are written.""" |
| 667 | + grid = Grid(shape=(12, 12)) |
| 668 | + sub = SubGrid(grid, factor=factor) |
| 669 | + |
| 670 | + u = Function(name='u', grid=grid) |
| 671 | + f = Function(name='f', grid=sub, space_order=0) |
| 672 | + data = np.arange(1, 145, dtype=u.dtype).reshape(12, 12) |
| 673 | + u.data[:] = data |
| 674 | + |
| 675 | + Operator(Eq(f, u, subdomain=Middle(grid=grid))).apply() |
| 676 | + |
| 677 | + fx, fy = sub.factor |
| 678 | + assert np.array_equal(f.data, middle_of(data)[::fx, ::fy]) |
| 679 | + |
643 | 680 | def test_copy_is_strided(self): |
644 | 681 | """ |
645 | 682 | No guard is emitted, every loop keeps a unit stride, and the Operator |
@@ -980,6 +1017,25 @@ def test_copy_from_parent(self, mode, factor): |
980 | 1017 | expected = glob[::factor, ::factor][g.local_indices] |
981 | 1018 | assert np.array_equal(np.asarray(g.data), expected) |
982 | 1019 |
|
| 1020 | + @pytest.mark.parallel(mode=[2, 4]) |
| 1021 | + @pytest.mark.parametrize('factor', [2, 3]) |
| 1022 | + def test_copy_on_subdomain(self, mode, factor): |
| 1023 | + """The SubDomain bounds the SubGrid on every rank.""" |
| 1024 | + n = 24 |
| 1025 | + grid = Grid(shape=(n, n)) |
| 1026 | + sub = SubGrid(grid, factor=factor) |
| 1027 | + |
| 1028 | + u = Function(name='u', grid=grid) |
| 1029 | + g = Function(name='g', grid=sub, space_order=0) |
| 1030 | + |
| 1031 | + glob = np.arange(1, n*n + 1, dtype=u.dtype).reshape(n, n) |
| 1032 | + u.data[:] = glob |
| 1033 | + |
| 1034 | + Operator(Eq(g, u, subdomain=Middle(grid=grid))).apply() |
| 1035 | + |
| 1036 | + expected = middle_of(glob)[::factor, ::factor][g.local_indices] |
| 1037 | + assert np.array_equal(np.asarray(g.data), expected) |
| 1038 | + |
983 | 1039 | @pytest.mark.parallel(mode=[2, 4]) |
984 | 1040 | @pytest.mark.parametrize('factor', [2, 3, 4]) |
985 | 1041 | def test_rank_boundary_off_the_subgrid(self, mode, factor): |
|
0 commit comments