From 11d4c1ac37686bf94fd06eacabe81548c864fde2 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 23 Nov 2017 17:03:25 -0600 Subject: [PATCH 1/4] Improve error message for prefech-on-indirect-access, add test (#104 on gitlab) --- loopy/symbolic.py | 17 ++++++++++++++++- loopy/transform/array_buffer_map.py | 15 +++++++-------- test/test_apps.py | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/loopy/symbolic.py b/loopy/symbolic.py index 543c2743b..6e661f422 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -63,6 +63,8 @@ from islpy import dim_type import re import numpy as np +from loopy.diagnostic import LoopyError + # {{{ mappers with support for loopy-specific primitives @@ -1284,7 +1286,20 @@ def aff_from_expr(space, expr, vars_to_zero=frozenset()): def pwaff_from_expr(space, expr, vars_to_zero=frozenset()): - return PwAffEvaluationMapper(space, vars_to_zero)(expr) + eval_mapper = PwAffEvaluationMapper(space, vars_to_zero) + + from pymbolic.mapper.evaluator import UnknownVariableError + try: + return eval_mapper(expr) + except UnknownVariableError as e: + raise LoopyError("unable to build (piecewise) affine expression " + "in terms of variables '%s' " + "for expression '%s' " + "because '%s: %s'" + % ( + ", ".join(space.get_var_dict()), + expr, + type(e).__name__, e)) # }}} diff --git a/loopy/transform/array_buffer_map.py b/loopy/transform/array_buffer_map.py index 618e36f20..cdd45a069 100644 --- a/loopy/transform/array_buffer_map.py +++ b/loopy/transform/array_buffer_map.py @@ -1,5 +1,4 @@ from __future__ import division, absolute_import -from six.moves import range, zip __copyright__ = "Copyright (C) 2012-2015 Andreas Kloeckner" @@ -23,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from six.moves import range, zip + import islpy as isl from islpy import dim_type from loopy.symbolic import (get_dependencies, SubstitutionMapper) @@ -92,9 +93,9 @@ def build_per_access_storage_to_domain_map(storage_axis_exprs, domain, from loopy.symbolic import aff_from_expr for saxis, sa_expr in zip(storage_axis_names, storage_axis_exprs): - cns = isl.Constraint.equality_from_aff( - aff_from_expr(set_space, - var(saxis+"'") - prime_sweep_inames(sa_expr))) + cns_expr = var(saxis+"'") - prime_sweep_inames(sa_expr) + cns_aff = aff_from_expr(set_space, cns_expr) + cns = isl.Constraint.equality_from_aff(cns_aff) cns_map = isl.BasicMap.from_constraint(cns) if stor2sweep is None: @@ -352,13 +353,11 @@ class ArrayToBufferMap(object): self.stor2sweep, except_inames=frozenset(self.primed_sweep_inames)).domain() - arg_inames = ( - set(global_s2s_par_dom.get_var_names(dim_type.param)) - & self.kernel.all_inames()) + arg_inames = set(global_s2s_par_dom.get_var_names(dim_type.param)) for arg in storage_axis_exprs: arg_inames.update(get_dependencies(arg)) - arg_inames = frozenset(arg_inames) + arg_inames = frozenset(arg_inames & self.kernel.all_inames()) from loopy.kernel import CannotBranchDomainTree try: diff --git a/test/test_apps.py b/test/test_apps.py index c4844d3a3..9e7c2ee5c 100644 --- a/test/test_apps.py +++ b/test/test_apps.py @@ -43,6 +43,8 @@ else: from pyopencl.tools import pytest_generate_tests_for_pyopencl \ as pytest_generate_tests +from loopy.diagnostic import LoopyError + __all__ = [ "pytest_generate_tests", "cl" # 'cl.create_some_context' @@ -663,6 +665,26 @@ def test_domain_tree_nesting(): assert depth(i) < 2 +def test_prefetch_through_indirect_access(): + knl = lp.make_kernel("{[i, j, k]: 0 <= i,k < 10 and 0<=j<2}", + """ + for i, j, k + a[map1[indirect[i], j], k] = 2 + end + """, + [ + lp.GlobalArg("a", strides=(2, 1), dtype=int), + lp.GlobalArg("map1", shape=(10, 10), dtype=int), + ... + ], + target=lp.CTarget()) + + knl = lp.prioritize_loops(knl, "i,j,k") + + with pytest.raises(LoopyError): + knl = lp.add_prefetch(knl, "map1[:, j]") + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab From cfd5b944fa02b76331bd2f3d6aafddf92e1cb7e2 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 23 Nov 2017 17:20:08 -0600 Subject: [PATCH 2/4] Introduce separate UnableToConvertToAffineExpression exception --- loopy/diagnostic.py | 4 ++++ loopy/symbolic.py | 9 ++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/loopy/diagnostic.py b/loopy/diagnostic.py index 512e4ac86..bec0888e5 100644 --- a/loopy/diagnostic.py +++ b/loopy/diagnostic.py @@ -107,6 +107,10 @@ class UnscheduledInstructionError(LoopyError): class ReductionIsNotTriangularError(LoopyError): pass + +class UnableToConvertToAffineExpression(LoopyError): + pass + # }}} diff --git a/loopy/symbolic.py b/loopy/symbolic.py index 6e661f422..7a9765fe2 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -63,7 +63,7 @@ from islpy import dim_type import re import numpy as np -from loopy.diagnostic import LoopyError +from loopy.diagnostic import UnableToConvertToAffineExpression # {{{ mappers with support for loopy-specific primitives @@ -1292,7 +1292,8 @@ def pwaff_from_expr(space, expr, vars_to_zero=frozenset()): try: return eval_mapper(expr) except UnknownVariableError as e: - raise LoopyError("unable to build (piecewise) affine expression " + raise UnableToConvertToAffineExpression( + "unable to build (piecewise) affine expression " "in terms of variables '%s' " "for expression '%s' " "because '%s: %s'" @@ -1311,8 +1312,6 @@ def simplify_using_aff(kernel, expr): domain = kernel.get_inames_domain(inames) - from pymbolic.mapper.evaluator import UnknownVariableError - try: with isl.SuppressedWarnings(kernel.isl_context): aff = aff_from_expr(domain.space, expr) @@ -1320,7 +1319,7 @@ def simplify_using_aff(kernel, expr): return expr except TypeError: return expr - except UnknownVariableError: + except UnableToConvertToAffineExpression: return expr # FIXME: Deal with assumptions, too. -- GitLab From 1e52119ad00bf7052f807d4ed34a7ce09cee085d Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 23 Nov 2017 17:43:52 -0600 Subject: [PATCH 3/4] Fix py2 incompatibility in test_prefetch_through_indirect_access --- test/test_apps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_apps.py b/test/test_apps.py index 9e7c2ee5c..e1a9eb63b 100644 --- a/test/test_apps.py +++ b/test/test_apps.py @@ -675,7 +675,7 @@ def test_prefetch_through_indirect_access(): [ lp.GlobalArg("a", strides=(2, 1), dtype=int), lp.GlobalArg("map1", shape=(10, 10), dtype=int), - ... + "..." ], target=lp.CTarget()) -- GitLab From e6274333942ee2333f98d31ef915680458a6b090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Fri, 29 Jun 2018 13:49:29 -0400 Subject: [PATCH 4/4] Simplify simplify_using_aff by using guarded_aff_from_expr --- loopy/symbolic.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/loopy/symbolic.py b/loopy/symbolic.py index 798995536..8927cd6fb 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -1404,12 +1404,7 @@ def simplify_using_aff(kernel, expr): domain = kernel.get_inames_domain(inames) try: - with isl.SuppressedWarnings(kernel.isl_context): - aff = aff_from_expr(domain.space, expr) - except isl.Error: - return expr - except TypeError: - return expr + aff = guarded_aff_from_expr(domain.space, expr) except ExpressionToAffineConversionError: return expr -- GitLab