From 17ff7c5a702f40b90fded7812a732e061e04c4c4 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 15:51:09 -0600 Subject: [PATCH 01/11] Added a new Exception `ExpressionNotInames` --- loopy/diagnostic.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/loopy/diagnostic.py b/loopy/diagnostic.py index 512e4ac86..9c441acca 100644 --- a/loopy/diagnostic.py +++ b/loopy/diagnostic.py @@ -107,6 +107,10 @@ class UnscheduledInstructionError(LoopyError): class ReductionIsNotTriangularError(LoopyError): pass + +class ExpressionNotAffineInamesError(LoopyError): + pass + # }}} -- GitLab From c46436c63c3d639c7a95ebf34da6617525bea67f Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 15:54:05 -0600 Subject: [PATCH 02/11] Added support for ignoring a global encountered in `LocalMemAccessCounter` rather than throwing an error. Added support to catch the `ExpressionNotAffineInameError`. Some minor fixed to only pay heed to countable insns and ignoring `BarrierInstruction` --- loopy/statistics.py | 91 +++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 33 deletions(-) diff --git a/loopy/statistics.py b/loopy/statistics.py index 21c8be307..dae0f7939 100755 --- a/loopy/statistics.py +++ b/loopy/statistics.py @@ -794,8 +794,17 @@ class LocalMemAccessCounter(MemAccessCounter): sub_map = ToCountMap() if name in self.knl.temporary_variables: array = self.knl.temporary_variables[name] - if array.is_local: - sub_map[MemAccess(mtype='local', dtype=dtype)] = 1 + try: + if array.is_local: + sub_map[MemAccess(mtype='local', dtype=dtype)] = 1 + except LoopyError as e: + error_str = str(e) + expected = "TemporaryVariable.is_local called on global" + "temporaty variable '%s'" % array.name + if error_str == expected: + pass + else: + LoopyError(error_str) return sub_map def map_variable(self, expr): @@ -902,7 +911,13 @@ class GlobalMemAccessCounter(MemAccessCounter): for idx, axis_tag in zip(index, array.dim_tags): from loopy.symbolic import simplify_using_aff - coeffs = CoefficientCollector()(simplify_using_aff(self.knl, idx)) + from loopy.diagnostic import ExpressionNotAffineInamesError + try: + coeffs = CoefficientCollector()( + simplify_using_aff(self.knl, idx)) + except ExpressionNotAffineInamesError: + stride = None + continue # check if he contains the lid 0 guy try: coeff_min_lid = coeffs[Variable(min_lid)] @@ -1319,37 +1334,47 @@ def get_mem_access_map(knl, numpy_types=True, count_redundant_work=False): access_counter_g = GlobalMemAccessCounter(knl) access_counter_l = LocalMemAccessCounter(knl) + from loopy.kernel.instruction import ( + CallInstruction, CInstruction, Assignment, + NoOpInstruction, BarrierInstruction) + for insn in knl.instructions: - access_expr = ( - access_counter_g(insn.expression) - + access_counter_l(insn.expression) - ).with_set_attributes(direction="load") - - access_assignee_g = access_counter_g(insn.assignee).with_set_attributes( - direction="store") - - # FIXME: (!!!!) for now, don't count writes to local mem - - # use count excluding local index tags for uniform accesses - for key, val in six.iteritems(access_expr.count_map): - is_uniform = (key.mtype == 'global' and - isinstance(key.stride, int) and - key.stride == 0) - access_map = ( - access_map - + ToCountMap({key: val}) - * get_insn_count(knl, insn.id, is_uniform)) - #currently not counting stride of local mem access - - for key, val in six.iteritems(access_assignee_g.count_map): - is_uniform = (key.mtype == 'global' and - isinstance(key.stride, int) and - key.stride == 0) - access_map = ( - access_map - + ToCountMap({key: val}) - * get_insn_count(knl, insn.id, is_uniform)) - # for now, don't count writes to local mem + if isinstance(insn, (CallInstruction, CInstruction, Assignment)): + access_expr = ( + access_counter_g(insn.expression) + + access_counter_l(insn.expression) + ).with_set_attributes(direction="load") + + access_assignee_g = access_counter_g(insn.assignee).with_set_attributes( + direction="store") + + # FIXME: (!!!!) for now, don't count writes to local mem + + # use count excluding local index tags for uniform accesses + for key, val in six.iteritems(access_expr.count_map): + is_uniform = (key.mtype == 'global' and + isinstance(key.stride, int) and + key.stride == 0) + access_map = ( + access_map + + ToCountMap({key: val}) + * get_insn_count(knl, insn.id, is_uniform)) + #currently not counting stride of local mem access + + for key, val in six.iteritems(access_assignee_g.count_map): + is_uniform = (key.mtype == 'global' and + isinstance(key.stride, int) and + key.stride == 0) + access_map = ( + access_map + + ToCountMap({key: val}) + * get_insn_count(knl, insn.id, is_uniform)) + # for now, don't count writes to local mem + elif isinstance(insn, (NoOpInstruction, BarrierInstruction)): + pass + else: + raise NotImplementedError("unexpected instruction item type: '%s'" + % type(insn).__name__) if numpy_types: # FIXME: Don't modify in-place -- GitLab From 34f0b63f928a284c74b091c99c7c5305fc1487ef Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 15:55:11 -0600 Subject: [PATCH 03/11] Removed the `indirect-access` error as `LoopyError` and replaced it with `ExpressionNotAffineInameError` --- loopy/symbolic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loopy/symbolic.py b/loopy/symbolic.py index 2d31c63ef..cdea08a26 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -1177,7 +1177,8 @@ class CoefficientCollector(CoefficientCollectorBase): map_tagged_variable = CoefficientCollectorBase.map_variable def map_subscript(self, expr): - raise RuntimeError("cannot gather coefficients--indirect addressing in use") + from loopy.diagnostic import ExpressionNotAffineInamesError + raise ExpressionNotAffineInamesError("%s has indirect accesses" % expr) # }}} @@ -1374,7 +1375,6 @@ def simplify_using_aff(kernel, expr): # FIXME: Deal with assumptions, too. aff = aff.gist(domain) - return aff_to_expr(aff) # }}} -- GitLab From a669e87d5127dc36317ab7745273ca2e26b7e876 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 22:13:23 -0600 Subject: [PATCH 04/11] Added new `LoopyError` `GlobalInIsLocalError` --- loopy/diagnostic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/loopy/diagnostic.py b/loopy/diagnostic.py index 9c441acca..ffd08b2c5 100644 --- a/loopy/diagnostic.py +++ b/loopy/diagnostic.py @@ -108,9 +108,12 @@ class ReductionIsNotTriangularError(LoopyError): pass -class ExpressionNotAffineInamesError(LoopyError): +class ExpressionNotAffineError(LoopyError): pass + +class GlobalInIsLocalError(LoopyError): + pass # }}} -- GitLab From e7e90e37bd1d124e54bbb830ab86b6071250e1ad Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 22:14:13 -0600 Subject: [PATCH 05/11] Changed the LoopyError to `GlobalInIsLocalError` --- loopy/kernel/data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loopy/kernel/data.py b/loopy/kernel/data.py index 96933f57a..0844e1f90 100644 --- a/loopy/kernel/data.py +++ b/loopy/kernel/data.py @@ -29,7 +29,7 @@ from six.moves import intern import numpy as np # noqa from pytools import ImmutableRecord from loopy.kernel.array import ArrayBase -from loopy.diagnostic import LoopyError +from loopy.diagnostic import LoopyError, GlobalInIsLocalError from loopy.kernel.instruction import ( # noqa InstructionBase, memory_ordering, @@ -472,7 +472,7 @@ class TemporaryVariable(ArrayBase): elif self.scope == temp_var_scope.PRIVATE: return False elif self.scope == temp_var_scope.GLOBAL: - raise LoopyError("TemporaryVariable.is_local called on " + raise GlobalInIsLocalError("TemporaryVariable.is_local called on " "global temporary variable '%s'" % self.name) else: raise LoopyError("unexpected value of TemporaryVariable.scope") -- GitLab From a19e4e41792472d224928f6608f24336493195de Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 22:16:48 -0600 Subject: [PATCH 06/11] Added support for `GlobalInIsLocalError`. Made corrections for `total_stride` --- loopy/statistics.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/loopy/statistics.py b/loopy/statistics.py index dae0f7939..d84c149db 100755 --- a/loopy/statistics.py +++ b/loopy/statistics.py @@ -31,7 +31,7 @@ from pytools import memoize_in from pymbolic.mapper import CombineMapper from functools import reduce from loopy.kernel.data import MultiAssignmentBase -from loopy.diagnostic import warn_with_kernel, LoopyError +from loopy.diagnostic import warn_with_kernel, LoopyError, GlobalInIsLocalError __doc__ = """ @@ -797,14 +797,8 @@ class LocalMemAccessCounter(MemAccessCounter): try: if array.is_local: sub_map[MemAccess(mtype='local', dtype=dtype)] = 1 - except LoopyError as e: - error_str = str(e) - expected = "TemporaryVariable.is_local called on global" - "temporaty variable '%s'" % array.name - if error_str == expected: - pass - else: - LoopyError(error_str) + except GlobalInIsLocalError: + pass return sub_map def map_variable(self, expr): @@ -911,13 +905,13 @@ class GlobalMemAccessCounter(MemAccessCounter): for idx, axis_tag in zip(index, array.dim_tags): from loopy.symbolic import simplify_using_aff - from loopy.diagnostic import ExpressionNotAffineInamesError + from loopy.diagnostic import ExpressionNotAffineError try: coeffs = CoefficientCollector()( simplify_using_aff(self.knl, idx)) - except ExpressionNotAffineInamesError: - stride = None - continue + except ExpressionNotAffineError: + total_stride = None + break # check if he contains the lid 0 guy try: coeff_min_lid = coeffs[Variable(min_lid)] -- GitLab From b04601518160c0e9815be103247aa47209e9182f Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 29 Nov 2017 22:17:28 -0600 Subject: [PATCH 07/11] changed the name of the new error to `ExpressionNotAffineError` --- loopy/symbolic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loopy/symbolic.py b/loopy/symbolic.py index cdea08a26..9a0fd72ea 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -1177,8 +1177,8 @@ class CoefficientCollector(CoefficientCollectorBase): map_tagged_variable = CoefficientCollectorBase.map_variable def map_subscript(self, expr): - from loopy.diagnostic import ExpressionNotAffineInamesError - raise ExpressionNotAffineInamesError("%s has indirect accesses" % expr) + from loopy.diagnostic import ExpressionNotAffineError + raise ExpressionNotAffineError("%s has indirect accesses" % expr) # }}} -- GitLab From f0a2bc08edc7b9c97281a22fae51d392e3011d99 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 30 Nov 2017 13:24:05 -0600 Subject: [PATCH 08/11] Removed the `GlobalIsInLocalError` --- loopy/diagnostic.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/loopy/diagnostic.py b/loopy/diagnostic.py index ffd08b2c5..16e859e0b 100644 --- a/loopy/diagnostic.py +++ b/loopy/diagnostic.py @@ -111,9 +111,6 @@ class ReductionIsNotTriangularError(LoopyError): class ExpressionNotAffineError(LoopyError): pass - -class GlobalInIsLocalError(LoopyError): - pass # }}} -- GitLab From f9c7a5f300dabe2dbca0956f906db44ea91be7d2 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 30 Nov 2017 13:24:47 -0600 Subject: [PATCH 09/11] Reverted the changes in `loopy/kernel/data.py` --- loopy/kernel/data.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/loopy/kernel/data.py b/loopy/kernel/data.py index 0844e1f90..a4e6036cb 100644 --- a/loopy/kernel/data.py +++ b/loopy/kernel/data.py @@ -29,7 +29,7 @@ from six.moves import intern import numpy as np # noqa from pytools import ImmutableRecord from loopy.kernel.array import ArrayBase -from loopy.diagnostic import LoopyError, GlobalInIsLocalError +from loopy.diagnostic import LoopyError from loopy.kernel.instruction import ( # noqa InstructionBase, memory_ordering, @@ -472,8 +472,8 @@ class TemporaryVariable(ArrayBase): elif self.scope == temp_var_scope.PRIVATE: return False elif self.scope == temp_var_scope.GLOBAL: - raise GlobalInIsLocalError("TemporaryVariable.is_local called on " - "global temporary variable '%s'" % self.name) + raise LoopyError("TemporaryVariable.is_local called on " + "global temporary variable '%s'" % self.name) else: raise LoopyError("unexpected value of TemporaryVariable.scope") -- GitLab From 78e7e4cbe27a8ded37e7843bae2556a890d2ead2 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 30 Nov 2017 13:25:38 -0600 Subject: [PATCH 10/11] Again made changes so `GlobalInIsLovalError` is removed --- loopy/statistics.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/loopy/statistics.py b/loopy/statistics.py index d84c149db..a2dcb6846 100755 --- a/loopy/statistics.py +++ b/loopy/statistics.py @@ -30,8 +30,9 @@ import islpy as isl from pytools import memoize_in from pymbolic.mapper import CombineMapper from functools import reduce -from loopy.kernel.data import MultiAssignmentBase -from loopy.diagnostic import warn_with_kernel, LoopyError, GlobalInIsLocalError +from loopy.kernel.data import ( + MultiAssignmentBase, TemporaryVariable, temp_var_scope) +from loopy.diagnostic import warn_with_kernel, LoopyError __doc__ = """ @@ -794,11 +795,9 @@ class LocalMemAccessCounter(MemAccessCounter): sub_map = ToCountMap() if name in self.knl.temporary_variables: array = self.knl.temporary_variables[name] - try: - if array.is_local: - sub_map[MemAccess(mtype='local', dtype=dtype)] = 1 - except GlobalInIsLocalError: - pass + if isinstance(array, TemporaryVariable) and ( + array.scope == temp_var_scope.LOCAL): + sub_map[MemAccess(mtype='local', dtype=dtype)] = 1 return sub_map def map_variable(self, expr): -- GitLab From eb8a6d7f0bcb293ebe60cf087cde7568d1902ed2 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 30 Nov 2017 21:20:11 -0600 Subject: [PATCH 11/11] Removed the confusing error statement --- loopy/symbolic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/loopy/symbolic.py b/loopy/symbolic.py index 9a0fd72ea..9e16c3a59 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -1178,7 +1178,8 @@ class CoefficientCollector(CoefficientCollectorBase): def map_subscript(self, expr): from loopy.diagnostic import ExpressionNotAffineError - raise ExpressionNotAffineError("%s has indirect accesses" % expr) + raise ExpressionNotAffineError("cannot gather coefficients--" + "indirect addressing in use") # }}} @@ -1375,6 +1376,7 @@ def simplify_using_aff(kernel, expr): # FIXME: Deal with assumptions, too. aff = aff.gist(domain) + return aff_to_expr(aff) # }}} -- GitLab