From 62f9126d34e2c6c025734cb343f239a5c437a589 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 23 Dec 2016 15:38:00 -0600 Subject: [PATCH] Codegen: Add a pass that converts big integers into floats. This allows us to use Taylor expansions for the Laplace FMM with order >= 9. Without this, loopy will complain that it can't infer the type of large ints. --- sumpy/codegen.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/sumpy/codegen.py b/sumpy/codegen.py index 9c8a4958..0cb14028 100644 --- a/sumpy/codegen.py +++ b/sumpy/codegen.py @@ -430,6 +430,41 @@ class FractionKiller(CSECachingMapperMixin, IdentityMapper): # }}} +# {{{ convert big integers into floats + +from loopy.tools import is_integer + + +class BigIntegerKiller(CSECachingMapperMixin, IdentityMapper): + + def __init__(self, warn_on_first=True, int_type=np.int64, float_type=np.float64): + IdentityMapper.__init__(self) + self.bits = 64 + self.warn = warn_on_first + self.float_type = float_type + self.iinfo = np.iinfo(int_type) + + def map_constant(self, expr): + if not is_integer(expr): + return IdentityMapper.map_constant(self, expr) + + if self.iinfo.min <= expr <= self.iinfo.max: + return expr + + if self.warn: + from warnings import warn + warn("Converting '%d' to float: this may result in " + "loss of digits." % expr) + # Suppress further warnings. + self.warn = False + + return self.float_type(expr) + + map_common_subexpression_uncached = IdentityMapper.map_common_subexpression + +# }}} + + # {{{ vector component rewriter INDEXED_VAR_RE = re.compile("^([a-zA-Z_]+)([0-9]+)$") @@ -534,6 +569,7 @@ def to_loopy_insns(assignments, vector_names=set(), pymbolic_expr_maps=[], pwr = PowerRewriter() ssg = SumSignGrouper() fck = FractionKiller() + bik = BigIntegerKiller() def convert_expr(name, expr): logger.debug("generate expression for: %s" % name) @@ -543,6 +579,7 @@ def to_loopy_insns(assignments, vector_names=set(), pymbolic_expr_maps=[], expr = pwr(expr) expr = fck(expr) expr = ssg(expr) + expr = bik(expr) #expr = cse_tag(expr) for m in pymbolic_expr_maps: expr = m(expr) -- GitLab