From a09262757b451ddd51b2cb2de00ed104cc38568c Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Mon, 12 Dec 2016 17:42:35 -0600 Subject: [PATCH] Kernel: Return a transformer object which preserves cached CSEs. This is necessary so that CSEs are not duplicated across instructions (see also !11, #3 which deal with the same issue). This requires a kernel version bump. --- sumpy/e2e.py | 2 +- sumpy/e2p.py | 2 +- sumpy/expansion/__init__.py | 4 ++-- sumpy/kernel.py | 45 ++++++++++++++++++++++++------------- sumpy/p2e.py | 2 +- sumpy/p2p.py | 3 ++- sumpy/qbx.py | 2 +- sumpy/version.py | 2 +- 8 files changed, 38 insertions(+), 24 deletions(-) diff --git a/sumpy/e2e.py b/sumpy/e2e.py index 4e739bf5..f5b34940 100644 --- a/sumpy/e2e.py +++ b/sumpy/e2e.py @@ -103,7 +103,7 @@ class E2EBase(KernelCacheWrapper): return to_loopy_insns( assignments, vector_names=set(["d"]), - pymbolic_expr_maps=[self.tgt_expansion.transform_to_code], + pymbolic_expr_maps=[self.tgt_expansion.get_code_transformer()], complex_dtype=np.complex128 # FIXME ) diff --git a/sumpy/e2p.py b/sumpy/e2p.py index 66f5d813..c7f82175 100644 --- a/sumpy/e2p.py +++ b/sumpy/e2p.py @@ -100,7 +100,7 @@ class E2PBase(KernelCacheWrapper): from sumpy.codegen import to_loopy_insns loopy_insns = to_loopy_insns(assignments, vector_names=set(["b"]), - pymbolic_expr_maps=[self.expansion.transform_to_code], + pymbolic_expr_maps=[self.expansion.get_code_transformer()], complex_dtype=np.complex128 # FIXME ) diff --git a/sumpy/expansion/__init__.py b/sumpy/expansion/__init__.py index 280eb8b7..995f522d 100644 --- a/sumpy/expansion/__init__.py +++ b/sumpy/expansion/__init__.py @@ -57,8 +57,8 @@ class ExpansionBase(object): def prepare_loopy_kernel(self, loopy_knl): return self.kernel.prepare_loopy_kernel(loopy_knl) - def transform_to_code(self, expr): - return self.kernel.transform_to_code(expr) + def get_code_transformer(self): + return self.kernel.get_code_transformer() def get_scaling(self): return self.kernel.get_scaling() diff --git a/sumpy/kernel.py b/sumpy/kernel.py index cf0835de..39f490a3 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -166,12 +166,12 @@ class Kernel(object): """ return loopy_knl - def transform_to_code(self, expr): - """Postprocess the :mod:`pymbolic` expression - generated from the result of :meth:`get_expression` - on the way to code generation. + def get_code_transformer(self): + """Return a function to postprocess the :mod:`pymbolic` + expression generated from the result of + :meth:`get_expression` on the way to code generation. """ - return expr + return lambda expr: expr def get_expression(self, dist_vec): """Return a :mod:`sympy` expression for the kernel. @@ -570,12 +570,16 @@ class StressletKernel(ExpressionKernel): dim_tags="sep,C")) ] - def transform_to_code(self, expr): + def get_code_tranformer(self): from sumpy.codegen import VectorComponentRewriter vcr = VectorComponentRewriter([self.stresslet_vector_name]) from pymbolic.primitives import Variable - return _VectorIndexAdder(self.stresslet_vector_name, (Variable("isrc"),))( - vcr(expr)) + via = _VectorIndexAdder(self.stresslet_vector_name, (Variable("isrc"),)) + + def transform(expr): + return via(vcr(expr)) + + return transform mapper_method = "map_stresslet_kernel" @@ -611,8 +615,8 @@ class KernelWrapper(Kernel): def get_scaling(self): return self.inner_kernel.get_scaling() - def transform_to_code(self, expr): - return self.inner_kernel.transform_to_code(expr) + def get_code_transformer(self): + return self.inner_kernel.get_code_transformer() def get_args(self): return self.inner_kernel.get_args() @@ -716,12 +720,16 @@ class DirectionalDerivative(DerivativeBase): class DirectionalTargetDerivative(DirectionalDerivative): directional_kind = "tgt" - def transform_to_code(self, expr): + def get_code_transformer(self): from sumpy.codegen import VectorComponentRewriter vcr = VectorComponentRewriter([self.dir_vec_name]) from pymbolic.primitives import Variable - return _VectorIndexAdder(self.dir_vec_name, (Variable("itgt"),))( - vcr(self.inner_kernel.transform_to_code(expr))) + via = _VectorIndexAdder(self.dir_vec_name, (Variable("itgt"),)) + + def transform(expr): + return via(vcr(expr)) + + return transform def postprocess_at_target(self, expr, bvec): expr = self.inner_kernel.postprocess_at_target(expr, bvec) @@ -742,12 +750,17 @@ class DirectionalTargetDerivative(DirectionalDerivative): class DirectionalSourceDerivative(DirectionalDerivative): directional_kind = "src" - def transform_to_code(self, expr): + def get_code_transformer(self): + inner = self.inner_kernel.get_code_transformer() from sumpy.codegen import VectorComponentRewriter vcr = VectorComponentRewriter([self.dir_vec_name]) from pymbolic.primitives import Variable - return _VectorIndexAdder(self.dir_vec_name, (Variable("isrc"),))( - vcr(self.inner_kernel.transform_to_code(expr))) + via = _VectorIndexAdder(self.dir_vec_name, (Variable("isrc"),)) + + def transform(expr): + return via(vcr(inner(expr))) + + return transform def postprocess_at_source(self, expr, avec): expr = self.inner_kernel.postprocess_at_source(expr, avec) diff --git a/sumpy/p2e.py b/sumpy/p2e.py index a3af57b8..1b037dd4 100644 --- a/sumpy/p2e.py +++ b/sumpy/p2e.py @@ -93,7 +93,7 @@ class P2EBase(KernelCacheWrapper): return to_loopy_insns( assignments, vector_names=set(["a"]), - pymbolic_expr_maps=[self.expansion.transform_to_code], + pymbolic_expr_maps=[self.expansion.get_code_transformer()], complex_dtype=np.complex128 # FIXME ) diff --git a/sumpy/p2p.py b/sumpy/p2p.py index 4dab9e27..7234180f 100644 --- a/sumpy/p2p.py +++ b/sumpy/p2p.py @@ -88,7 +88,8 @@ class P2PBase(KernelComputation, KernelCacheWrapper): from sumpy.codegen import to_loopy_insns loopy_insns = to_loopy_insns(six.iteritems(sac.assignments), vector_names=set(["d"]), - pymbolic_expr_maps=[knl.transform_to_code for knl in self.kernels], + pymbolic_expr_maps=[ + knl.get_code_transformer() for knl in self.kernels], complex_dtype=np.complex128 # FIXME ) diff --git a/sumpy/qbx.py b/sumpy/qbx.py index f06a0604..0b3dc035 100644 --- a/sumpy/qbx.py +++ b/sumpy/qbx.py @@ -144,7 +144,7 @@ class LayerPotentialBase(KernelComputation, KernelCacheWrapper): loopy_insns = to_loopy_insns(assignments, vector_names=set(["a", "b"]), pymbolic_expr_maps=[ - expn.kernel.transform_to_code for expn in self.expansions], + expn.kernel.get_code_transformer() for expn in self.expansions], complex_dtype=np.complex128 # FIXME ) diff --git a/sumpy/version.py b/sumpy/version.py index 5561f99d..360986ff 100644 --- a/sumpy/version.py +++ b/sumpy/version.py @@ -25,4 +25,4 @@ VERSION = (2016, 1) VERSION_STATUS = "beta1" VERSION_TEXT = ".".join(str(x) for x in VERSION) + VERSION_STATUS -KERNEL_VERSION = 11 +KERNEL_VERSION = 12 -- GitLab