|
| 1 | +from devito.symbolics import retrieve_indexed, retrieve_dimensions, uxreplace |
| 2 | +from devito.types.dimension import SpaceDimension, CustomDimension |
| 3 | +from devito import Min, Max |
| 4 | + |
| 5 | +from devito.petsc.types.equation import ConstrainBC |
| 6 | +from devito.petsc.types.dimension import ( |
| 7 | + SubDimMax, SubDimMin, |
| 8 | + SpaceDimMax, SpaceDimMin, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def lower_exprs_petsc(expressions, **kwargs): |
| 13 | + |
| 14 | + # Process `ConstrainBC` equations |
| 15 | + expressions = constrain_essential_bcs(expressions, **kwargs) |
| 16 | + |
| 17 | + return expressions |
| 18 | + |
| 19 | + |
| 20 | +def constrain_essential_bcs(expressions, **kwargs): |
| 21 | + """ |
| 22 | + Expand loop bounds for `ConstrainBC` expressions so that each MPI rank |
| 23 | + iterates over all locally visible constrained points, including those in |
| 24 | + the halo. PETSc requires each rank to report all constrained nodes in its |
| 25 | + local data region. The loops are not used for data access — only to |
| 26 | + identify which local indices are constrained. |
| 27 | + """ |
| 28 | + constrain_expressions = [e for e in expressions if isinstance(e, ConstrainBC)] |
| 29 | + if not constrain_expressions: |
| 30 | + return expressions |
| 31 | + |
| 32 | + sregistry = kwargs.get('sregistry') |
| 33 | + new_exprs = [] |
| 34 | + |
| 35 | + # TODO: rethink |
| 36 | + halo_size = {e.target.function._size_halo for e in constrain_expressions} |
| 37 | + assert len(halo_size) == 1 |
| 38 | + halo_size = halo_size.pop() |
| 39 | + |
| 40 | + all_dims = {d for e in constrain_expressions for d in extract_dims(e)} |
| 41 | + subdims = [d for d in all_dims if d.is_Sub and not d.local] |
| 42 | + space_dims = [d for d in all_dims if isinstance(d, SpaceDimension)] |
| 43 | + |
| 44 | + mapper = {} |
| 45 | + |
| 46 | + for d in subdims: |
| 47 | + halo = halo_size[d] |
| 48 | + |
| 49 | + subdim_max = SubDimMax( |
| 50 | + sregistry.make_name(prefix=f"{d.name}_max"), subdim=d |
| 51 | + ) |
| 52 | + subdim_min = SubDimMin( |
| 53 | + sregistry.make_name(prefix=f"{d.name}_min"), subdim=d |
| 54 | + ) |
| 55 | + |
| 56 | + mapper[d] = CustomDimension( |
| 57 | + name=d.name, |
| 58 | + symbolic_min=Max(subdim_min, d.parent.symbolic_min - halo.left), |
| 59 | + symbolic_max=Min(subdim_max, d.parent.symbolic_max + halo.right), |
| 60 | + ) |
| 61 | + |
| 62 | + for d in space_dims: |
| 63 | + halo = halo_size[d] |
| 64 | + space_dim_max = SpaceDimMax( |
| 65 | + sregistry.make_name(prefix=f"{d.name}_max"), space_dim=d |
| 66 | + ) |
| 67 | + space_dim_min = SpaceDimMin( |
| 68 | + sregistry.make_name(prefix=f"{d.name}_min"), space_dim=d |
| 69 | + ) |
| 70 | + |
| 71 | + mapper[d] = CustomDimension( |
| 72 | + name=sregistry.make_name(prefix=f"{d.name}_expanded"), |
| 73 | + symbolic_min=Max(space_dim_min, d.symbolic_min - halo.left), |
| 74 | + symbolic_max=Min(space_dim_max, d.symbolic_max + halo.right), |
| 75 | + ) |
| 76 | + |
| 77 | + # Apply mapper to expressions |
| 78 | + for e in expressions: |
| 79 | + if not isinstance(e, ConstrainBC): |
| 80 | + new_exprs.append(e) |
| 81 | + continue |
| 82 | + |
| 83 | + dims = extract_dims(e) |
| 84 | + if not dims: |
| 85 | + new_exprs.append(e) |
| 86 | + continue |
| 87 | + |
| 88 | + new_e = uxreplace(e, mapper) |
| 89 | + |
| 90 | + if e.implicit_dims: |
| 91 | + new_e = new_e._rebuild( |
| 92 | + implicit_dims=tuple(mapper.get(d, d) for d in e.implicit_dims) |
| 93 | + ) |
| 94 | + new_exprs.append(new_e) |
| 95 | + return new_exprs |
| 96 | + |
| 97 | + |
| 98 | +def extract_dims(expr): |
| 99 | + indexeds = retrieve_indexed(expr) |
| 100 | + dims = retrieve_dimensions( |
| 101 | + [i for j in indexeds for i in j.indices], |
| 102 | + mode="unique", |
| 103 | + ) |
| 104 | + dims.update(expr.implicit_dims) |
| 105 | + return dims |
0 commit comments