From 8fd54b705b44b9a9df7ffd18c51d743b54a47333 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Wed, 28 Feb 2024 13:25:52 -0600 Subject: [PATCH 1/5] support aborting on cache miss --- loopy/__init__.py | 3 +++ loopy/codegen/__init__.py | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/loopy/__init__.py b/loopy/__init__.py index e5aa4259a..c5988bc54 100644 --- a/loopy/__init__.py +++ b/loopy/__init__.py @@ -427,6 +427,9 @@ def register_symbol_manglers(kernel, manglers): and "CG_NO_CACHE" not in os.environ) +from pytools import strtobool +ABORT_ON_CACHE_MISS = strtobool(os.environ.get("LOOPY_ABORT_ON_CACHE_MISS", "False")) + def set_caching_enabled(flag): """Set whether :mod:`loopy` is allowed to use disk caching for its various diff --git a/loopy/codegen/__init__.py b/loopy/codegen/__init__.py index 32f89992a..392a5689a 100644 --- a/loopy/codegen/__init__.py +++ b/loopy/codegen/__init__.py @@ -573,7 +573,7 @@ def generate_code_v2(program): # {{{ cache retrieval - from loopy import CACHING_ENABLED + from loopy import CACHING_ENABLED, ABORT_ON_CACHE_MISS if CACHING_ENABLED: input_program = program @@ -583,6 +583,9 @@ def generate_code_v2(program): " code generation cache hit") return result except KeyError: + if ABORT_ON_CACHE_MISS: + raise + logger.debug(f"TranslationUnit with entrypoints {program.entrypoints}:" " code generation cache miss") From a0aaa2955bc4a89917692259c4c3ea00de6298d1 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Tue, 4 Feb 2025 15:50:40 -0600 Subject: [PATCH 2/5] fix ruff --- loopy/__init__.py | 2 ++ loopy/codegen/__init__.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/loopy/__init__.py b/loopy/__init__.py index a3d2a5c72..e3c094714 100644 --- a/loopy/__init__.py +++ b/loopy/__init__.py @@ -509,6 +509,8 @@ def register_symbol_manglers(kernel, manglers): not strtobool(os.environ.get("CG_NO_CACHE", "false"))) from pytools import strtobool + + ABORT_ON_CACHE_MISS = strtobool(os.environ.get("LOOPY_ABORT_ON_CACHE_MISS", "False")) diff --git a/loopy/codegen/__init__.py b/loopy/codegen/__init__.py index a76c5f0f0..e7decb8f2 100644 --- a/loopy/codegen/__init__.py +++ b/loopy/codegen/__init__.py @@ -550,7 +550,7 @@ def all_code(self): def generate_code_v2(t_unit: TranslationUnit) -> CodeGenerationResult: # {{{ cache retrieval - from loopy import CACHING_ENABLED, ABORT_ON_CACHE_MISS + from loopy import ABORT_ON_CACHE_MISS, CACHING_ENABLED from loopy.kernel import LoopKernel from loopy.translation_unit import make_program From 87beabbf6952e53200140a2c9b3f15ab4fe8886c Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Tue, 4 Feb 2025 16:38:29 -0600 Subject: [PATCH 3/5] cleanups, add to CI --- .github/workflows/ci.yml | 1 + doc/ref_other.rst | 7 +++++++ loopy/__init__.py | 4 +--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 85e0336b0..bf5fd8d84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -111,6 +111,7 @@ jobs: . ./ci-support-v0 build_py_project_in_conda_env ( test_py_project ) + export LOOPY_ABORT_ON_CACHE_MISS=1 ( test_py_project ) examples: diff --git a/doc/ref_other.rst b/doc/ref_other.rst index 538f0cdb9..81b6eca1e 100644 --- a/doc/ref_other.rst +++ b/doc/ref_other.rst @@ -25,6 +25,13 @@ Controlling caching is suppressed. +.. envvar:: LOOPY_ABORT_ON_CACHE_MISS + + If set to a string that :func:`pytools.strtobool` evaluates as ``True``, + loopy will raise an exception if a cache miss occurs. This can be useful + for debugging cache-related issues. For example, it can be used to automatically test whether caching is successful for a particular code, by setting this variable to ``True`` and re-running the code. + + .. autofunction:: set_caching_enabled .. autoclass:: CacheMode diff --git a/loopy/__init__.py b/loopy/__init__.py index e3c094714..8060ee0bc 100644 --- a/loopy/__init__.py +++ b/loopy/__init__.py @@ -508,10 +508,8 @@ def register_symbol_manglers(kernel, manglers): and not strtobool(os.environ.get("CG_NO_CACHE", "false"))) -from pytools import strtobool - -ABORT_ON_CACHE_MISS = strtobool(os.environ.get("LOOPY_ABORT_ON_CACHE_MISS", "False")) +ABORT_ON_CACHE_MISS = strtobool(os.environ.get("LOOPY_ABORT_ON_CACHE_MISS", "false")) def set_caching_enabled(flag): From 54979333dc1fb79e566d4fb2b515942b4e269984 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Wed, 5 Feb 2025 12:50:34 -0600 Subject: [PATCH 4/5] partial rng fix caching --- test/test_c_execution.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/test_c_execution.py b/test/test_c_execution.py index 9943d41df..33e50e3ab 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -93,10 +93,11 @@ def __get_kernel(order="C"): def test_c_target_strides_nonsquare(): from loopy.target.c import ExecutableCTarget + rng = np.random.default_rng(seed=42) def __get_kernel(order="C"): indices = ["i", "j", "k"] - sizes = tuple(np.random.randint(1, 11, size=len(indices))) + sizes = tuple(rng.integers(1, 11, size=len(indices))) # create domain strings domain_template = "{{ [{iname}]: 0 <= {iname} < {size} }}" domains = [] @@ -141,9 +142,11 @@ def __get_kernel(order="C"): def test_c_optimizations(): from loopy.target.c import ExecutableCTarget + rng = np.random.default_rng(seed=42) + def __get_kernel(order="C"): indices = ["i", "j", "k"] - sizes = tuple(np.random.randint(1, 11, size=len(indices))) + sizes = tuple(rng.integers(1, 11, size=len(indices))) # create domain strings domain_template = "{{ [{iname}]: 0 <= {iname} < {size} }}" domains = [] From 339ecdecb780f1dae99ac8b7422bcf8bafa51ff5 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Thu, 6 Feb 2025 18:02:26 -0600 Subject: [PATCH 5/5] Update ci.yml --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bf5fd8d84..0b1f8156c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -107,11 +107,15 @@ jobs: - uses: actions/checkout@v4 - name: "Main Script" run: | + # This test makes sure that loopy can run with kernels loaded from disk cache. curl -L -O https://tiker.net/ci-support-v0 . ./ci-support-v0 build_py_project_in_conda_env ( test_py_project ) - export LOOPY_ABORT_ON_CACHE_MISS=1 + + # See https://github.com/inducer/loopy/pull/828 why this is disabled. + # export LOOPY_ABORT_ON_CACHE_MISS=1 + ( test_py_project ) examples: