From ee561bb8da29a5e97fbb724eb1327edc881a2dc4 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Sat, 15 Jul 2017 18:32:45 -0500 Subject: [PATCH 1/3] get_typed_and_scheduled_kernel(): Make sure the kernel's dtypes are cacheable before creating the cache key. --- loopy/execution.py | 7 ++++++- test/test_loopy.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/loopy/execution.py b/loopy/execution.py index dac5b2ff8..07e28f06d 100644 --- a/loopy/execution.py +++ b/loopy/execution.py @@ -187,7 +187,12 @@ class KernelExecutorBase(object): def get_typed_and_scheduled_kernel(self, arg_to_dtype_set): from loopy import CACHING_ENABLED - cache_key = (type(self).__name__, self.kernel, arg_to_dtype_set) + from loopy.preprocess import prepare_for_caching + # prepare_for_caching() gets run by preprocess, but the kernel at this + # stage is not guaranteed to be preprocessed. + cacheable_kernel = prepare_for_caching(self.kernel) + cache_key = (type(self).__name__, cacheable_kernel, arg_to_dtype_set) + if CACHING_ENABLED: try: return typed_and_scheduled_cache[cache_key] diff --git a/test/test_loopy.py b/test/test_loopy.py index d8bf76f96..61f992c5d 100644 --- a/test/test_loopy.py +++ b/test/test_loopy.py @@ -2335,6 +2335,21 @@ def test_kernel_var_name_generator(): assert vng("b") != "b" +def test_execution_backend_can_cache_dtypes(ctx_factory): + # When the kernel is invoked, the execution backend uses it as a cache key + # for the type inference and scheduling cache. This tests to make sure that + # dtypes in the kernel can be cached, even though they may not have a + # target. + + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + + knl = lp.make_kernel("{[i]: 0 <= i < 10}", "<>tmp[i] = i") + knl = lp.add_dtypes(knl, dict(tmp=int)) + + knl(queue) + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab From 3c760bee62935e60b9d92a6ca9856481d16bb768 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Sat, 15 Jul 2017 18:34:15 -0500 Subject: [PATCH 2/3] Fix TemporaryVariable.update_persistent_hash() to handle storage shapes which contain pymbolic expressions. --- loopy/kernel/array.py | 20 ++++++++++++-------- loopy/kernel/data.py | 5 +++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/loopy/kernel/array.py b/loopy/kernel/array.py index a02fc58d9..dab338486 100644 --- a/loopy/kernel/array.py +++ b/loopy/kernel/array.py @@ -862,6 +862,17 @@ class ArrayBase(ImmutableRecord): def __repr__(self): return "<%s>" % self.__str__() + def update_persistent_hash_for_shape(self, key_hash, key_builder, shape): + if isinstance(shape, tuple): + for shape_i in shape: + if shape_i is None: + key_builder.rec(key_hash, shape_i) + else: + key_builder.update_for_pymbolic_expression(key_hash, shape_i) + else: + key_builder.rec(key_hash, shape) + + def update_persistent_hash(self, key_hash, key_builder): """Custom hash computation function for use with :class:`pytools.persistent_dict.PersistentDict`. @@ -869,14 +880,7 @@ class ArrayBase(ImmutableRecord): key_builder.rec(key_hash, self.name) key_builder.rec(key_hash, self.dtype) - if isinstance(self.shape, tuple): - for shape_i in self.shape: - if shape_i is None: - key_builder.rec(key_hash, shape_i) - else: - key_builder.update_for_pymbolic_expression(key_hash, shape_i) - else: - key_builder.rec(key_hash, self.shape) + self.update_persistent_hash_for_shape(key_hash, key_builder, self.shape) key_builder.rec(key_hash, self.dim_tags) key_builder.rec(key_hash, self.offset) key_builder.rec(key_hash, self.dim_names) diff --git a/loopy/kernel/data.py b/loopy/kernel/data.py index 001dd0632..94b31df12 100644 --- a/loopy/kernel/data.py +++ b/loopy/kernel/data.py @@ -497,7 +497,8 @@ class TemporaryVariable(ArrayBase): """ super(TemporaryVariable, self).update_persistent_hash(key_hash, key_builder) - key_builder.rec(key_hash, self.storage_shape) + self.update_persistent_hash_for_shape(key_hash, key_builder, + self.storage_shape) key_builder.rec(key_hash, self.base_indices) initializer = self.initializer @@ -510,7 +511,7 @@ class TemporaryVariable(ArrayBase): # }}} -# {{{ subsitution rule +# {{{ substitution rule class SubstitutionRule(ImmutableRecord): """ -- GitLab From 5e1664ff193bec7ab54d2f29363099701b88f3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Sun, 16 Jul 2017 13:26:04 -0400 Subject: [PATCH 3/3] Placate flake8 in array.py --- loopy/kernel/array.py | 1 - 1 file changed, 1 deletion(-) diff --git a/loopy/kernel/array.py b/loopy/kernel/array.py index dab338486..531cc822e 100644 --- a/loopy/kernel/array.py +++ b/loopy/kernel/array.py @@ -872,7 +872,6 @@ class ArrayBase(ImmutableRecord): else: key_builder.rec(key_hash, shape) - def update_persistent_hash(self, key_hash, key_builder): """Custom hash computation function for use with :class:`pytools.persistent_dict.PersistentDict`. -- GitLab