Skip to content

Commit

Permalink
Test for transforms.reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushikcfd committed Dec 17, 2022
1 parent e974c53 commit 9f2286d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,48 @@ def test_prefetch_to_same_temp_var(ctx_factory):
lp.auto_test_vs_ref(ref_tunit, ctx, t_unit)


def test_sum_redn_algebraic_transforms(ctx_factory):
from pymbolic import variables
from loopy.symbolic import Reduction

t_unit = lp.make_kernel(
"{[e,i,j,x,r]: 0<=e<N_e and 0<=i,j<35 and 0<=x,r<3}",
"""
y[i] = sum([r,j], J[x, r, e]*D[r,i,j]*u[e,j])
""",
[lp.GlobalArg("J,D,u", dtype=np.float64, shape=lp.auto),
...],
)
knl = t_unit.default_entrypoint

knl = lp.split_reduction_inward(knl, "j")
knl = lp.hoist_invariant_multiplicative_terms_in_sum_reduction(
knl,
reduction_inames="j"
)
knl = lp.extract_multiplicative_terms_in_sum_reduction_as_subst(
knl,
within=None,
subst_name="grad_without_jacobi_subst",
arguments=variables("r i e"),
terms_filter=lambda x: isinstance(x, Reduction)
)

transformed_t_unit = t_unit.with_kernel(knl)
transformed_t_unit = lp.precompute(
transformed_t_unit,
"grad_without_jacobi_subst",
sweep_inames=["r", "i"],
precompute_outer_inames=frozenset({"e"}),
temporary_address_space=lp.AddressSpace.PRIVATE)

x1 = lp.get_op_map(t_unit, subgroup_size=1).eval_and_sum({"N_e": 1})
x2 = lp.get_op_map(transformed_t_unit, subgroup_size=1).eval_and_sum({"N_e": 1})

assert x1 == 33075
assert x2 == 7980 # i.e. demonstrates a 4.14x fewer flops


if __name__ == "__main__":
if len(sys.argv) > 1:
exec(sys.argv[1])
Expand Down

0 comments on commit 9f2286d

Please sign in to comment.