From 6abc5aa575763455ebd909d7468900dd52493330 Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Tue, 1 Dec 2026 22:53:24 +0000 Subject: [PATCH] misc: Move the Devito cache directory --- .github/workflows/examples-mpi.yaml | 2 +- .github/workflows/pytest-core-mpi.yaml | 4 ++-- FAQ.md | 10 ++++++---- benchmarks/user/advisor/run_advisor.py | 12 ++++-------- devito/__init__.py | 9 +++++++++ devito/arch/compiler.py | 12 ++++++------ devito/clear_cache.py | 18 ++++++++++++++++++ devito/operator/operator.py | 4 ++-- devito/parameters.py | 4 +++- devito/tools/os_helper.py | 22 ++++++++++------------ scripts/clear_devito_cache.py | 12 ------------ 11 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 devito/clear_cache.py delete mode 100644 scripts/clear_devito_cache.py diff --git a/.github/workflows/examples-mpi.yaml b/.github/workflows/examples-mpi.yaml index 56caf8dec38..b414af82899 100644 --- a/.github/workflows/examples-mpi.yaml +++ b/.github/workflows/examples-mpi.yaml @@ -63,7 +63,7 @@ jobs: run: | pip install --upgrade pip pip install -e ".[extras,mpi,tests]" - python3 scripts/clear_devito_cache.py + python3 -m devito.clear_cache - name: Test mpi notebooks run : | diff --git a/.github/workflows/pytest-core-mpi.yaml b/.github/workflows/pytest-core-mpi.yaml index 1c18f64525a..b49429eb0cf 100644 --- a/.github/workflows/pytest-core-mpi.yaml +++ b/.github/workflows/pytest-core-mpi.yaml @@ -46,7 +46,7 @@ jobs: - name: Test with pytest run: | - python3 scripts/clear_devito_cache.py + python3 -m devito.clear_cache python3 -m pytest \ -v \ --cov \ @@ -57,7 +57,7 @@ jobs: - name: Test examples with MPI run: | - python3 scripts/clear_devito_cache.py + python3 -m devito.clear_cache DEVITO_MPI=1 mpirun -n 2 \ python3 -m pytest \ -v \ diff --git a/FAQ.md b/FAQ.md index 19e6bbfd4dc..f769e6252fb 100644 --- a/FAQ.md +++ b/FAQ.md @@ -5,7 +5,7 @@ - [How can I see the code generated by Devito?](#how-can-i-see-the-code-generated-by-devito) - [How can I see the compilation command with which Devito compiles the generated code?](#how-can-i-see-the-compilation-command-with-which-devito-compiles-the-generated-code) - [Where does the generated code go and how do I look at it?](#where-does-the-generated-code-go-and-how-do-i-look-at-it) -- [Can I change the directory where Devito stashes the generated code?](#can-i-change-the-directory-where-devito-stashes-the-generated-code) +- [Can I change the directory where Devito caches the generated code?](#can-i-change-the-directory-where-devito-caches-the-generated-code) - [I create an Operator, look at the generated code, and the equations appear in a different order than I expected.](#i-create-an-operator-look-at-the-generated-code-and-the-equations-appear-in-a-different-order-than-i-expected) - [Do Devito Operators release the GIL when executing C code?](#do-devito-operators-release-the-GIL-when-executing-C-code) - [What performance optimizations does Devito apply?](#what-performance-optimizations-does-devito-apply) @@ -150,7 +150,7 @@ Set the environment variable `DEVITO_LOGGING=DEBUG`. When an Operator gets compi If nothing seems to change, it is possible that no compilation is happening under-the-hood as all kernels have already been compiled in a previous run. You will then have to clear up the Devito kernel cache. From the Devito root directory, run: ```bash -python scripts/clear_devito_cache.py +python -m devito.clear_cache ``` [top](#Frequently-Asked-Questions) @@ -162,8 +162,10 @@ Devito stores the generated code as well as the jit-compiled libraries in a temp [top](#Frequently-Asked-Questions) -## Can I change the directory where Devito stashes the generated code? -Yes, just set the environment variable `TMPDIR` to your favorite location. +## Can I change the directory where Devito caches the generated code? +Yes, just set the environment variable `DEVITO_CACHE_DIR` to your preferred location. +Previously `TMPDIR` was used, now `$XDG_CACHE_DIR/devito` or `$HOME/.cache/devito` is used instead. +To restore the previous behaviour set `DEVITO_CACHE_DIR=$TMPDIR` or `DEVITO_CACHE_DIR=/tmp`, but beware that this may be a security concern on shared systems. [top](#Frequently-Asked-Questions) diff --git a/benchmarks/user/advisor/run_advisor.py b/benchmarks/user/advisor/run_advisor.py index 8d20607da43..3e756592b1d 100644 --- a/benchmarks/user/advisor/run_advisor.py +++ b/benchmarks/user/advisor/run_advisor.py @@ -4,11 +4,12 @@ import sys from pathlib import Path from subprocess import PIPE, Popen, check_output -from tempfile import gettempdir, mkdtemp import click from advisor_logging import check, log, log_process, progress +from devito.tools.os_helper import make_cache_dir + @click.command() # Required arguments @@ -36,15 +37,10 @@ def run_with_advisor(path, output, name, exec_args): if exec_args: name = f"{name}_{''.join(exec_args.split())}" if output is None: - output = Path(gettempdir()).joinpath('devito-profilings') - output.mkdir(parents=True, exist_ok=True) + output = make_cache_dir(output, prefix='profilings') else: output = Path(output) - if name is None: - output = Path(mkdtemp(dir=str(output), prefix=f"{name}-")) - else: - output = Path(output).joinpath(name) - output.mkdir(parents=True, exist_ok=True) + output = output/name # advixe-cl and icx should be available through Intel oneAPI # (tested with Intel oneAPI 2025.1) diff --git a/devito/__init__.py b/devito/__init__.py index 753f85b7304..572f28d21be 100644 --- a/devito/__init__.py +++ b/devito/__init__.py @@ -1,6 +1,7 @@ import atexit from itertools import product import os +from pathlib import Path import numpy as np @@ -178,6 +179,14 @@ def profiling_preprocessor(i): configuration.add('profiling', 'basic', list(profiler_registry), preprocessor=profiling_preprocessor, impacts_jit=False) +configuration.add( + 'cache-dir', + Path(os.environ.get('XDG_CACHE_HOME', Path().home()/'.cache'))/'devito', + preprocessor=lambda p: Path(p).absolute(), + callback=lambda p: (p.mkdir(parents=True, exist_ok=True), p)[1], + impacts_jit=False +) + # Initialize `configuration` init_configuration() diff --git a/devito/arch/compiler.py b/devito/arch/compiler.py index 2bb9db57b3e..b21d52475e2 100644 --- a/devito/arch/compiler.py +++ b/devito/arch/compiler.py @@ -23,9 +23,9 @@ from devito.logger import debug, warning from devito.parameters import configuration from devito.tools import ( - Signer, as_list, atomic_write, change_directory, filter_ordered, make_tempdir, - memoized_func + Signer, as_list, atomic_write, change_directory, filter_ordered, memoized_func ) +from devito.tools.os_helper import make_cache_dir __all__ = ['compiler_registry', 'sniff_mpi_distro'] @@ -278,12 +278,12 @@ def get_version(self): return stdout def get_jit_dir(self): - """A deterministic temporary directory for jit-compiled objects.""" - return make_tempdir('jitcache') + """A deterministic cache directory for jit-compiled objects.""" + return make_cache_dir('jitcache') def get_codepy_dir(self): - """A deterministic temporary directory for the codepy cache.""" - return make_tempdir('codepy') + """A deterministic cache directory for the codepy cache.""" + return make_cache_dir('codepy') def load(self, soname): """ diff --git a/devito/clear_cache.py b/devito/clear_cache.py new file mode 100644 index 00000000000..227f4eacee8 --- /dev/null +++ b/devito/clear_cache.py @@ -0,0 +1,18 @@ +from contextlib import suppress +from os import listdir, path +from shutil import rmtree + +from . import configuration + + +def main(): + cache_dir = configuration['cache-dir'] + for i in listdir(cache_dir): + if i.startswith('codepy-uid') or i.startswith('jitcache-uid'): + with suppress(Exception): + target = path.join(cache_dir, i) + rmtree(target) + + +if __name__ == '__main__': + main() diff --git a/devito/operator/operator.py b/devito/operator/operator.py index 70d700cba36..fe61a38151e 100644 --- a/devito/operator/operator.py +++ b/devito/operator/operator.py @@ -5,7 +5,7 @@ from functools import cached_property from math import ceil from operator import attrgetter -from tempfile import gettempdir +from pathlib import Path import numpy as np import sympy @@ -1500,7 +1500,7 @@ def nbytes_avail_mapper(self): mapper = {} # The amount of space available on the disk - usage = shutil.disk_usage(gettempdir()) + usage = shutil.disk_usage(Path()) mapper[disk_layer] = usage.free # The amount of space available on the device diff --git a/devito/parameters.py b/devito/parameters.py index 64ec3c9ea3e..5785dd5cdd3 100644 --- a/devito/parameters.py +++ b/devito/parameters.py @@ -6,7 +6,8 @@ from functools import wraps from devito.logger import info, warning -from devito.tools import Signer, filter_ordered +from devito.tools.abc import Signer +from devito.tools.utils import filter_ordered __all__ = ['configuration', 'init_configuration', 'print_defaults', 'print_state', 'switchconfig', 'switchenv'] @@ -144,6 +145,7 @@ def _signature_items(self): 'DEVITO_ARCH': 'compiler', 'DEVITO_AUTOPADDING_MODE': 'autopadding-mode', 'DEVITO_AUTOTUNING': 'autotuning', + 'DEVITO_CACHE_DIR': 'cache-dir', 'DEVITO_DEVELOP': 'develop-mode', 'DEVITO_DEVICEID': 'deviceid', 'DEVITO_FIRST_TOUCH': 'first-touch', diff --git a/devito/tools/os_helper.py b/devito/tools/os_helper.py index bc8ef635a4f..32f18ce324b 100644 --- a/devito/tools/os_helper.py +++ b/devito/tools/os_helper.py @@ -1,9 +1,9 @@ import os from contextlib import suppress from pathlib import Path -from tempfile import gettempdir, mkstemp +from tempfile import mkstemp -__all__ = ['atomic_write', 'change_directory', 'make_tempdir'] +__all__ = ['atomic_write', 'change_directory', 'make_cache_dir'] class change_directory: @@ -25,16 +25,14 @@ def __exit__(self, etype, value, traceback): os.chdir(self.savedPath) -def make_tempdir(prefix=None): - """Create a temporary directory having a deterministic name. The directory - is created within the default OS temporary directory.""" - if prefix is None: - name = f'devito-uid{os.getuid()}' - else: - name = f'devito-{str(prefix)}-uid{os.getuid()}' - tmpdir = Path(gettempdir()).joinpath(name) - tmpdir.mkdir(parents=True, exist_ok=True) - return tmpdir +def make_cache_dir(prefix=None): + """Create a cache directory for Devito generated files""" + from devito import configuration + + name = f'{str(prefix) + "-" if prefix is not None else ""}uid{os.getuid()}' + cache_dir = configuration['cache-dir']/name + cache_dir.mkdir(exist_ok=True) + return cache_dir def atomic_write(path, data, mode='w', *, overwrite=True): diff --git a/scripts/clear_devito_cache.py b/scripts/clear_devito_cache.py deleted file mode 100644 index d0568db3638..00000000000 --- a/scripts/clear_devito_cache.py +++ /dev/null @@ -1,12 +0,0 @@ -from os import listdir, path -from shutil import rmtree -from tempfile import gettempdir - -tempdir = gettempdir() -for i in listdir(tempdir): - if i.startswith('devito-'): - try: - target = path.join(tempdir, i) - rmtree(target) - except: - pass