From 2eba1c54a8cf0baaa71c141584c01cd001fcc636 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Sun, 4 Feb 2018 18:13:47 -0600 Subject: [PATCH 1/2] Limit recursion errors --- loopy/target/c/codegen/expression.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/loopy/target/c/codegen/expression.py b/loopy/target/c/codegen/expression.py index caee73eb1..c53d224dd 100644 --- a/loopy/target/c/codegen/expression.py +++ b/loopy/target/c/codegen/expression.py @@ -525,11 +525,15 @@ class ExpressionToCExpressionMapper(IdentityMapper): real_sum = p.flattened_sum([self.rec(r, type_context) for r in reals]) - complex_sum = self.rec(complexes[0], type_context, tgt_dtype) - for child in complexes[1:]: - complex_sum = var("%s_add" % tgt_name)( - complex_sum, - self.rec(child, type_context, tgt_dtype)) + def binary_tree_add(start, end): + if start + 1 == end: + return self.rec(complexes[start], type_context, tgt_dtype) + mid = (start + end)//2 + lsum = binary_tree_add(start, mid) + rsum = binary_tree_add(mid, end) + return var("%s_add" % tgt_name)(lsum, rsum) + + complex_sum = binary_tree_add(0, len(complexes)) if real_sum: return var("%s_radd" % tgt_name)(real_sum, complex_sum) @@ -569,11 +573,15 @@ class ExpressionToCExpressionMapper(IdentityMapper): real_prd = p.flattened_product( [self.rec(r, type_context) for r in reals]) - complex_prd = self.rec(complexes[0], type_context, tgt_dtype) - for child in complexes[1:]: - complex_prd = var("%s_mul" % tgt_name)( - complex_prd, - self.rec(child, type_context, tgt_dtype)) + def binary_tree_mul(start, end): + if start + 1 == end: + return self.rec(complexes[start], type_context, tgt_dtype) + mid = (start + end)//2 + lsum = binary_tree_mul(start, mid) + rsum = binary_tree_mul(mid, end) + return var("%s_mul" % tgt_name)(lsum, rsum) + + complex_prd = binary_tree_mul(0, len(complexes)) if real_prd: return var("%s_rmul" % tgt_name)(real_prd, complex_prd) -- GitLab From 608bdc0ed1f6c7d60e8bee6129dc0a03a9096ec7 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Sun, 4 Feb 2018 18:38:03 -0600 Subject: [PATCH 2/2] Don't call self.rec inside recursive function --- loopy/target/c/codegen/expression.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/loopy/target/c/codegen/expression.py b/loopy/target/c/codegen/expression.py index c53d224dd..c111a02b7 100644 --- a/loopy/target/c/codegen/expression.py +++ b/loopy/target/c/codegen/expression.py @@ -525,15 +525,17 @@ class ExpressionToCExpressionMapper(IdentityMapper): real_sum = p.flattened_sum([self.rec(r, type_context) for r in reals]) + c_applied = [self.rec(c, type_context, tgt_dtype) for c in complexes] + def binary_tree_add(start, end): if start + 1 == end: - return self.rec(complexes[start], type_context, tgt_dtype) + return c_applied[start] mid = (start + end)//2 lsum = binary_tree_add(start, mid) rsum = binary_tree_add(mid, end) return var("%s_add" % tgt_name)(lsum, rsum) - complex_sum = binary_tree_add(0, len(complexes)) + complex_sum = binary_tree_add(0, len(c_applied)) if real_sum: return var("%s_radd" % tgt_name)(real_sum, complex_sum) @@ -573,9 +575,11 @@ class ExpressionToCExpressionMapper(IdentityMapper): real_prd = p.flattened_product( [self.rec(r, type_context) for r in reals]) + c_applied = [self.rec(c, type_context, tgt_dtype) for c in complexes] + def binary_tree_mul(start, end): if start + 1 == end: - return self.rec(complexes[start], type_context, tgt_dtype) + return c_applied[start] mid = (start + end)//2 lsum = binary_tree_mul(start, mid) rsum = binary_tree_mul(mid, end) -- GitLab