|
7 | 7 |
|
8 | 8 | from devito import ( |
9 | 9 | Buffer, CondEq, ConditionalDimension, Constant, Eq, Function, Grid, Inc, Operator, |
10 | | - SpaceDimension, SubDomain, SubGrid, TimeFunction, switchconfig |
| 10 | + PrecomputedSparseFunction, SpaceDimension, SparseFunction, SubDomain, SubGrid, |
| 11 | + TimeFunction, switchconfig |
11 | 12 | ) |
12 | | -from devito.ir.iet import Conditional, Expression, FindNodes, Iteration |
| 13 | +from devito.ir.iet import Conditional, Expression, FindNodes, FindSymbols, Iteration |
| 14 | +from devito.mpi import MPI |
13 | 15 | from devito.symbolics import retrieve_indexed |
14 | 16 | from devito.tools import as_tuple |
15 | 17 | from devito.types.dimension import SubsamplingOffset |
@@ -841,11 +843,109 @@ def test_derivative_numerics(self): |
841 | 843 |
|
842 | 844 | assert np.allclose(d.data[1:-1, 1:-1], 3., atol=1e-4) |
843 | 845 |
|
| 846 | + @pytest.mark.parametrize('so', [4, 8]) |
| 847 | + @pytest.mark.parametrize('shape,factor,opt', [ |
| 848 | + ((33, 33), 2, 'noop'), |
| 849 | + ((33, 33), 2, 'advanced'), |
| 850 | + ((33, 33), (2, 3), 'advanced'), |
| 851 | + ((33, 33), (2, 3), ('advanced', {'blockrelax': True})), |
| 852 | + ((17, 17, 17), (2, 2, 4), 'advanced'), |
| 853 | + ]) |
| 854 | + def test_high_order_derivatives(self, so, shape, factor, opt): |
| 855 | + """ |
| 856 | + Stencils reaching beyond the adjacent SubGrid points match those on a |
| 857 | + plain Grid of the same shape and extent, also once CIRE kicks in. |
| 858 | + """ |
| 859 | + sub = SubGrid(Grid(shape=shape), factor=factor) |
| 860 | + plain = Grid(shape=sub.shape, extent=sub.extent) |
| 861 | + |
| 862 | + results = [] |
| 863 | + for grid, o in ((sub, opt), (plain, 'noop')): |
| 864 | + u = Function(name='u', grid=grid, space_order=so) |
| 865 | + v = Function(name='v', grid=grid, space_order=so) |
| 866 | + u.data[:] = np.sin(np.arange(u.data.size).reshape(u.shape)/7.) |
| 867 | + |
| 868 | + op = Operator(Eq(v, u.laplace + u.dx.dy), opt=o) |
| 869 | + op.apply() |
| 870 | + results.append(np.array(v.data)) |
| 871 | + |
| 872 | + if grid is sub and opt != 'noop': |
| 873 | + assert any(i.is_Array for i in FindSymbols().visit(op)) |
| 874 | + |
| 875 | + assert np.abs(results[0] - results[1]).max() <= 1e-6*np.abs(results[1]).max() |
| 876 | + |
| 877 | + @pytest.mark.parametrize('opt', [ |
| 878 | + 'noop', 'advanced', |
| 879 | + ('advanced', {'blockrelax': True, 'cire-block-temps': False}), |
| 880 | + ]) |
| 881 | + def test_subdomain_high_order(self, opt): |
| 882 | + """As above, for Functions on a SubDomain of the SubGrid.""" |
| 883 | + sub = SubGrid(Grid(shape=(33, 33)), factor=2) |
| 884 | + plain = Grid(shape=sub.shape, extent=sub.extent) |
| 885 | + |
| 886 | + results = [] |
| 887 | + for grid, o in ((sub, opt), (plain, 'noop')): |
| 888 | + middle = Middle(grid=grid) |
| 889 | + u = Function(name='u', grid=middle, space_order=4) |
| 890 | + v = Function(name='v', grid=middle, space_order=4) |
| 891 | + u.data[:] = np.sin(np.arange(u.data.size).reshape(u.shape)/7.) |
| 892 | + |
| 893 | + Operator(Eq(v, u.laplace + u.dx.dy), opt=o).apply() |
| 894 | + results.append(np.array(v.data)) |
| 895 | + |
| 896 | + assert np.abs(results[0] - results[1]).max() <= 1e-6*np.abs(results[1]).max() |
| 897 | + |
| 898 | + @pytest.mark.parametrize('interpolation', ['linear', 'sinc', 'precomputed']) |
| 899 | + @pytest.mark.parametrize('subdomain', [False, True]) |
| 900 | + def test_sparse_matches_plain(self, interpolation, subdomain): |
| 901 | + """ |
| 902 | + Interpolation and injection on a SubGrid match those on a plain Grid of |
| 903 | + the same shape and extent, including in the last cell and at the corners. |
| 904 | + """ |
| 905 | + sub = SubGrid(Grid(shape=(33, 33), extent=(32., 32.)), factor=2) |
| 906 | + plain = Grid(shape=sub.shape, extent=sub.extent) |
| 907 | + coords = np.array([[5., 5.], [12.5, 5.], [29., 29.], [31.9, 7.], |
| 908 | + [32., 32.], [32., 5.], [0., 0.]]) |
| 909 | + npoint = len(coords) |
| 910 | + |
| 911 | + results = [] |
| 912 | + for grid in (sub, plain): |
| 913 | + target = Middle(grid=grid) if subdomain else grid |
| 914 | + u = Function(name='u', grid=target, space_order=4) |
| 915 | + v = Function(name='v', grid=target, space_order=4) |
| 916 | + u.data[:] = np.sin(np.arange(u.data.size).reshape(u.shape)/7.) |
| 917 | + |
| 918 | + if interpolation == 'precomputed': |
| 919 | + gridpoints = np.minimum(coords // np.array(grid.spacing), |
| 920 | + np.array(grid.shape) - 2).astype(np.int32) |
| 921 | + coeffs = np.random.default_rng(0).uniform(size=(npoint, 2, 2)) |
| 922 | + kwargs = {'r': 2, 'gridpoints': gridpoints, |
| 923 | + 'interpolation_coeffs': coeffs.astype(np.float32)} |
| 924 | + sf = PrecomputedSparseFunction |
| 925 | + else: |
| 926 | + kwargs = {'coordinates': coords, 'interpolation': interpolation, |
| 927 | + 'r': 2, 'space_order': 4} |
| 928 | + sf = SparseFunction |
| 929 | + |
| 930 | + s = sf(name='s', grid=grid, npoint=npoint, **kwargs) |
| 931 | + Operator(s.interpolate(u)).apply() |
| 932 | + results.append(np.array(s.data)) |
| 933 | + |
| 934 | + s.data[:] = np.arange(1, npoint + 1) |
| 935 | + Operator(s.inject(v, expr=s)).apply() |
| 936 | + results.append(np.array(v.data)) |
| 937 | + |
| 938 | + assert np.array_equal(results[0], results[2]) |
| 939 | + assert np.array_equal(results[1], results[3]) |
| 940 | + |
844 | 941 | @pytest.mark.parametrize('factor,err', [ |
845 | 942 | (0, 'must be >= 1'), |
846 | 943 | (-1, 'must be >= 1'), |
847 | 944 | (2.5, 'Non-integer'), |
848 | 945 | ((2, 2, 2), 'Expected 1 or 2 factors'), |
| 946 | + (9, 'single point'), |
| 947 | + (10, 'single point'), |
| 948 | + ((2, 9), 'single point'), |
849 | 949 | ]) |
850 | 950 | def test_invalid_factor(self, factor, err): |
851 | 951 | grid = Grid(shape=(9, 9)) |
@@ -1377,6 +1477,84 @@ def test_conditional_dimension_built_without_mpi(self, mode): |
1377 | 1477 |
|
1378 | 1478 | assert np.all(g.data == np.arange(0, 21, 2)[g.local_indices]) |
1379 | 1479 |
|
| 1480 | + @pytest.mark.parallel(mode=[(4, 'basic'), (4, 'diag2'), (4, 'full')]) |
| 1481 | + @pytest.mark.parametrize('shape,topology,factor', [ |
| 1482 | + ((16, 24), (1, 4), (1, 10)), |
| 1483 | + ((24, 24), (4, 1), (10, 1)), |
| 1484 | + ((12, 16), (4, 1), (6, 1)), |
| 1485 | + ]) |
| 1486 | + def test_empty_ranks(self, mode, shape, topology, factor): |
| 1487 | + """ |
| 1488 | + The halo of a rank next to one owning no SubGrid points comes from the |
| 1489 | + next populated rank, diagonals included. |
| 1490 | + """ |
| 1491 | + grid = Grid(shape=shape, topology=topology) |
| 1492 | + sub = SubGrid(grid, factor=factor) |
| 1493 | + t = sub.stepping_dim |
| 1494 | + x, y = sub.dimensions |
| 1495 | + |
| 1496 | + assert any(len(i) == 0 for d in sub.distributor.decomposition for i in d) |
| 1497 | + |
| 1498 | + u = TimeFunction(name='u', grid=sub, space_order=1) |
| 1499 | + glob = np.sin(np.arange(np.prod(sub.shape)).reshape(sub.shape)) |
| 1500 | + u.data[0] = glob |
| 1501 | + |
| 1502 | + Operator(Eq(u.forward, u[t, x + 1, y] + u[t, x - 1, y + 1] |
| 1503 | + + 2*u[t, x, y - 1])).apply(time_M=0) |
| 1504 | + |
| 1505 | + # The empty ranks have nothing to check, and `local_indices` would give |
| 1506 | + # them `slice(0, -1)` |
| 1507 | + if not sub.distributor.loc_empty: |
| 1508 | + padded = np.pad(glob, 1) |
| 1509 | + expected = padded[2:, 1:-1] + padded[:-2, 2:] + 2*padded[1:-1, :-2] |
| 1510 | + assert np.allclose(np.asarray(u.data[1]), expected[u.local_indices[1:]]) |
| 1511 | + |
| 1512 | + @pytest.mark.parallel(mode=[4]) |
| 1513 | + def test_sparse_empty_ranks(self, mode): |
| 1514 | + """Sparse operations with ranks owning no SubGrid points.""" |
| 1515 | + grid = Grid(shape=(24, 24), extent=(23., 23.), topology=(4, 1)) |
| 1516 | + sub = SubGrid(grid, factor=(10, 1)) |
| 1517 | + coords = np.random.default_rng(0).uniform(size=(20, 2))*np.array(sub.extent) |
| 1518 | + xx, yy = np.meshgrid(*[np.arange(n)*h for n, h in |
| 1519 | + zip(sub.shape, sub.spacing, strict=True)], |
| 1520 | + indexing='ij') |
| 1521 | + |
| 1522 | + u = Function(name='u', grid=sub) |
| 1523 | + v = Function(name='v', grid=sub) |
| 1524 | + u.data[:] = xx + 10*yy |
| 1525 | + s = SparseFunction(name='s', grid=sub, npoint=len(coords), coordinates=coords) |
| 1526 | + |
| 1527 | + # Linear interpolation is exact on a linear field |
| 1528 | + Operator(s.interpolate(u)).apply() |
| 1529 | + expected = coords[:, 0] + 10*coords[:, 1] |
| 1530 | + assert np.allclose(np.asarray(s.data), expected[s.local_indices], rtol=1e-5) |
| 1531 | + |
| 1532 | + # The linear weights sum to one, so injection conserves the total |
| 1533 | + s.data[:] = 1. |
| 1534 | + Operator(s.inject(v, expr=s)).apply() |
| 1535 | + total = grid.distributor.comm.allreduce(float(np.sum(v.data))) |
| 1536 | + assert np.isclose(total, len(coords)) |
| 1537 | + |
| 1538 | + @pytest.mark.parallel(mode=[(2, 'basic'), (4, 'diag2')]) |
| 1539 | + def test_high_order_mpi(self, mode): |
| 1540 | + """High-order stencils on a SubGrid match a serial plain Grid.""" |
| 1541 | + sub = SubGrid(Grid(shape=(33, 33)), factor=2) |
| 1542 | + plain = Grid(shape=sub.shape, extent=sub.extent, comm=MPI.COMM_SELF) |
| 1543 | + |
| 1544 | + glob = np.sin(np.arange(np.prod(sub.shape)).reshape(sub.shape)/7.) |
| 1545 | + |
| 1546 | + vs = [] |
| 1547 | + for grid in (sub, plain): |
| 1548 | + u = Function(name='u', grid=grid, space_order=4) |
| 1549 | + v = Function(name='v', grid=grid, space_order=4) |
| 1550 | + u.data[:] = glob |
| 1551 | + |
| 1552 | + Operator(Eq(v, u.laplace + u.dx.dy)).apply() |
| 1553 | + vs.append(v) |
| 1554 | + |
| 1555 | + v, expected = vs[0], np.asarray(vs[1].data)[vs[0].local_indices] |
| 1556 | + assert np.abs(np.asarray(v.data) - expected).max() <= 1e-6*np.abs(expected).max() |
| 1557 | + |
1380 | 1558 |
|
1381 | 1559 | class TestIncReduction: |
1382 | 1560 |
|
|
0 commit comments