From 75665003e17a49547f9ebdb835ba6d7c58aa3737 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Sat, 7 Mar 2020 16:40:12 -0600 Subject: [PATCH 1/4] Use make_cse --- sumpy/assignment_collection.py | 12 ++++++++---- sumpy/codegen.py | 4 ---- sumpy/expansion/local.py | 2 +- sumpy/expansion/multipole.py | 6 +++--- sumpy/symbolic.py | 10 ++++++---- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/sumpy/assignment_collection.py b/sumpy/assignment_collection.py index dadf3fc9..f12c6d59 100644 --- a/sumpy/assignment_collection.py +++ b/sumpy/assignment_collection.py @@ -185,16 +185,20 @@ class SymbolicAssignmentCollection(object): new_assignments, new_exprs = cse(assign_exprs + extra_exprs, symbols=self.symbol_generator) + xreplace_dict = {} + for name, value in new_assignments: + new_value = sym.make_cse(value.xreplace(xreplace_dict)) + xreplace_dict[name] = new_value + + for i in range(len(new_exprs)): + new_exprs[i] = new_exprs[i].xreplace(xreplace_dict) + new_assign_exprs = new_exprs[:len(assign_exprs)] new_extra_exprs = new_exprs[len(assign_exprs):] for name, new_expr in zip(assign_names, new_assign_exprs): self.assignments[name] = new_expr - for name, value in new_assignments: - assert isinstance(name, sym.Symbol) - self.add_assignment(name.name, value) - logger.info("common subexpression elimination: done after {dur:.2f} s" .format(dur=time.time() - start_time)) return new_extra_exprs diff --git a/sumpy/codegen.py b/sumpy/codegen.py index 085f9bd4..9e52afa0 100644 --- a/sumpy/codegen.py +++ b/sumpy/codegen.py @@ -67,10 +67,6 @@ class SympyToPymbolicMapper(SympyToPymbolicMapperBase): def not_supported(self, expr): if isinstance(expr, int): return expr - elif getattr(expr, "is_Function", False): - func_name = SympyToPymbolicMapperBase.function_name(self, expr) - return prim.Variable(func_name)( - *tuple(self.rec(arg) for arg in expr.args)) else: return SympyToPymbolicMapperBase.not_supported(self, expr) diff --git a/sumpy/expansion/local.py b/sumpy/expansion/local.py index b4b435de..78560f6f 100644 --- a/sumpy/expansion/local.py +++ b/sumpy/expansion/local.py @@ -270,7 +270,7 @@ class VolumeTaylorLocalExpansionBase(LocalExpansionBase): rscale=src_rscale) replace_dict = dict((d, d/src_rscale) for d in dvec) taker = MiDerivativeTaker(expr, dvec) - rscale_ratio = sym.UnevaluatedExpr(tgt_rscale/src_rscale) + rscale_ratio = sym.make_cse(tgt_rscale/src_rscale) result = [ (taker.diff(mi).xreplace(replace_dict) * rscale_ratio**sum(mi)) for mi in self.get_coefficient_identifiers()] diff --git a/sumpy/expansion/multipole.py b/sumpy/expansion/multipole.py index e3759d43..b643004b 100644 --- a/sumpy/expansion/multipole.py +++ b/sumpy/expansion/multipole.py @@ -101,7 +101,7 @@ class VolumeTaylorMultipoleExpansionBase(MultipoleExpansionBase): for i, mi in enumerate(coeff_identifiers): result[i] /= (mi_factorial(mi) * rscale ** sum(mi)) else: - avec = [sym.UnevaluatedExpr(a * rscale**-1) for a in avec] + avec = [sym.make_cse(a * rscale**-1) for a in avec] result = [ mi_power(avec, mi) / mi_factorial(mi) @@ -170,7 +170,7 @@ class VolumeTaylorMultipoleExpansionBase(MultipoleExpansionBase): src_coeff_exprs = list(src_coeff_exprs) for i, mi in enumerate(src_expansion.get_coefficient_identifiers()): - src_coeff_exprs[i] *= sym.UnevaluatedExpr(src_rscale/tgt_rscale)**sum(mi) + src_coeff_exprs[i] *= sym.make_cse(src_rscale/tgt_rscale)**sum(mi) result = [0] * len(self.get_full_coefficient_identifiers()) @@ -223,7 +223,7 @@ class VolumeTaylorMultipoleExpansionBase(MultipoleExpansionBase): k = src_mi[idim] assert n >= k contrib /= mi_factorial((n-k,)) - contrib *= sym.UnevaluatedExpr(dvec[idim]/tgt_rscale)**(n-k) + contrib *= sym.make_cse(dvec[idim]/tgt_rscale)**(n-k) result[i] += contrib diff --git a/sumpy/symbolic.py b/sumpy/symbolic.py index 7a86958a..0de2f80a 100644 --- a/sumpy/symbolic.py +++ b/sumpy/symbolic.py @@ -80,13 +80,15 @@ Derivative Integer Matrix Subs I pi functions""".split() if USE_SYMENGINE: import symengine as sym - from pymbolic.interop.symengine import ( + from pymbolic.interop.symengine import ( # noqa: F401 PymbolicToSymEngineMapper as PymbolicToSympyMapper, - SymEngineToPymbolicMapper as SympyToPymbolicMapper) + SymEngineToPymbolicMapper as SympyToPymbolicMapper, + make_cse) else: import sympy as sym - from pymbolic.interop.sympy import ( - PymbolicToSympyMapper, SympyToPymbolicMapper) + from pymbolic.interop.sympy import ( # noqa: F401 + PymbolicToSympyMapper, SympyToPymbolicMapper, + make_cse) for _apifunc in SYMBOLIC_API: globals()[_apifunc] = getattr(sym, _apifunc) -- GitLab From 91c8370045b457e81a52db43530038fd4158b561 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Sat, 7 Mar 2020 17:05:49 -0600 Subject: [PATCH 2/4] Need to wait till #125 lands --- sumpy/expansion/local.py | 2 +- sumpy/expansion/multipole.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sumpy/expansion/local.py b/sumpy/expansion/local.py index 78560f6f..b4b435de 100644 --- a/sumpy/expansion/local.py +++ b/sumpy/expansion/local.py @@ -270,7 +270,7 @@ class VolumeTaylorLocalExpansionBase(LocalExpansionBase): rscale=src_rscale) replace_dict = dict((d, d/src_rscale) for d in dvec) taker = MiDerivativeTaker(expr, dvec) - rscale_ratio = sym.make_cse(tgt_rscale/src_rscale) + rscale_ratio = sym.UnevaluatedExpr(tgt_rscale/src_rscale) result = [ (taker.diff(mi).xreplace(replace_dict) * rscale_ratio**sum(mi)) for mi in self.get_coefficient_identifiers()] diff --git a/sumpy/expansion/multipole.py b/sumpy/expansion/multipole.py index b643004b..e3759d43 100644 --- a/sumpy/expansion/multipole.py +++ b/sumpy/expansion/multipole.py @@ -101,7 +101,7 @@ class VolumeTaylorMultipoleExpansionBase(MultipoleExpansionBase): for i, mi in enumerate(coeff_identifiers): result[i] /= (mi_factorial(mi) * rscale ** sum(mi)) else: - avec = [sym.make_cse(a * rscale**-1) for a in avec] + avec = [sym.UnevaluatedExpr(a * rscale**-1) for a in avec] result = [ mi_power(avec, mi) / mi_factorial(mi) @@ -170,7 +170,7 @@ class VolumeTaylorMultipoleExpansionBase(MultipoleExpansionBase): src_coeff_exprs = list(src_coeff_exprs) for i, mi in enumerate(src_expansion.get_coefficient_identifiers()): - src_coeff_exprs[i] *= sym.make_cse(src_rscale/tgt_rscale)**sum(mi) + src_coeff_exprs[i] *= sym.UnevaluatedExpr(src_rscale/tgt_rscale)**sum(mi) result = [0] * len(self.get_full_coefficient_identifiers()) @@ -223,7 +223,7 @@ class VolumeTaylorMultipoleExpansionBase(MultipoleExpansionBase): k = src_mi[idim] assert n >= k contrib /= mi_factorial((n-k,)) - contrib *= sym.make_cse(dvec[idim]/tgt_rscale)**(n-k) + contrib *= sym.UnevaluatedExpr(dvec[idim]/tgt_rscale)**(n-k) result[i] += contrib -- GitLab From 61c2db02b8ab0dee905c0dec800fcba14d576b38 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Sat, 7 Mar 2020 17:48:58 -0600 Subject: [PATCH 3/4] Use tiker.net --- .test-conda-env-py3.yml | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.test-conda-env-py3.yml b/.test-conda-env-py3.yml index 2264e926..202edac8 100644 --- a/.test-conda-env-py3.yml +++ b/.test-conda-env-py3.yml @@ -19,5 +19,5 @@ dependencies: - pip: - git+https://github.com/inducer/pytools - git+https://gitlab.tiker.net/inducer/boxtree - - git+https://github.com/inducer/pymbolic + - git+https://gitlab.tiker.net/inducer/pymbolic - git+https://github.com/inducer/loopy diff --git a/requirements.txt b/requirements.txt index efe91f94..f522da7d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ numpy sympy==1.1.1 git+https://github.com/inducer/pytools -git+https://github.com/inducer/pymbolic +git+https://gitlab.tiker.net/inducer/pymbolic git+https://github.com/inducer/islpy git+https://github.com/inducer/pyopencl git+https://gitlab.tiker.net/inducer/boxtree -- GitLab From 3f0ef2163a0724c3147d0fe068153582df94737f Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Tue, 17 Mar 2020 02:31:07 +0100 Subject: [PATCH 4/4] Update symengine to 0.6.1 --- .test-conda-env-py3.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.test-conda-env-py3.yml b/.test-conda-env-py3.yml index 202edac8..9f8c38d0 100644 --- a/.test-conda-env-py3.yml +++ b/.test-conda-env-py3.yml @@ -12,7 +12,7 @@ dependencies: - islpy - pyopencl - python=3 -- python-symengine=0.6.0 +- python-symengine=0.6.1 - pyfmmlib - pip -- GitLab