Skip to content

Commit 72358f5

Browse files
committed
misc: Move the Devito cache directory
1 parent bbf8169 commit 72358f5

11 files changed

Lines changed: 61 additions & 48 deletions

File tree

‎.github/workflows/examples-mpi.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
run: |
6464
pip install --upgrade pip
6565
pip install -e ".[extras,mpi,tests]"
66-
python3 scripts/clear_devito_cache.py
66+
python3 -m devito.clear_caches
6767
6868
- name: Test mpi notebooks
6969
run : |

‎.github/workflows/pytest-core-mpi.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
4747
- name: Test with pytest
4848
run: |
49-
python3 scripts/clear_devito_cache.py
49+
python3 -m devito.clear_caches
5050
python3 -m pytest \
5151
-v \
5252
--cov \
@@ -57,7 +57,7 @@ jobs:
5757
5858
- name: Test examples with MPI
5959
run: |
60-
python3 scripts/clear_devito_cache.py
60+
python3 -m devito.clear_caches
6161
DEVITO_MPI=1 mpirun -n 2 \
6262
python3 -m pytest \
6363
-v \

‎FAQ.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [How can I see the code generated by Devito?](#how-can-i-see-the-code-generated-by-devito)
66
- [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)
77
- [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)
8-
- [Can I change the directory where Devito stashes the generated code?](#can-i-change-the-directory-where-devito-stashes-the-generated-code)
8+
- [Can I change the directory where Devito caches the generated code?](#can-i-change-the-directory-where-devito-caches-the-generated-code)
99
- [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)
1010
- [Do Devito Operators release the GIL when executing C code?](#do-devito-operators-release-the-GIL-when-executing-C-code)
1111
- [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
150150
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:
151151
152152
```bash
153-
python scripts/clear_devito_cache.py
153+
python -m devito.clear_cache
154154
```
155155

156156
[top](#Frequently-Asked-Questions)
@@ -162,8 +162,10 @@ Devito stores the generated code as well as the jit-compiled libraries in a temp
162162
[top](#Frequently-Asked-Questions)
163163

164164

165-
## Can I change the directory where Devito stashes the generated code?
166-
Yes, just set the environment variable `TMPDIR` to your favorite location.
165+
## Can I change the directory where Devito caches the generated code?
166+
Yes, just set the environment variable `DEVITO_CACHE_DIR` to your preferred location.
167+
Previously `TMPDIR` was used, now `$XDG_CACHE_DIR/devito` or `$HOME/.cache/devito` is used instead.
168+
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.
167169

168170
[top](#Frequently-Asked-Questions)
169171

‎benchmarks/user/advisor/run_advisor.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import sys
55
from pathlib import Path
66
from subprocess import PIPE, Popen, check_output
7-
from tempfile import gettempdir, mkdtemp
87

98
import click
109
from advisor_logging import check, log, log_process, progress
1110

11+
from devito.tools.os_helper import make_cache_dir
12+
1213

1314
@click.command()
1415
# Required arguments
@@ -36,15 +37,10 @@ def run_with_advisor(path, output, name, exec_args):
3637
if exec_args:
3738
name = f"{name}_{''.join(exec_args.split())}"
3839
if output is None:
39-
output = Path(gettempdir()).joinpath('devito-profilings')
40-
output.mkdir(parents=True, exist_ok=True)
40+
output = make_cache_dir(output, prefix='profilings')
4141
else:
4242
output = Path(output)
43-
if name is None:
44-
output = Path(mkdtemp(dir=str(output), prefix=f"{name}-"))
45-
else:
46-
output = Path(output).joinpath(name)
47-
output.mkdir(parents=True, exist_ok=True)
43+
output = output/name
4844

4945
# advixe-cl and icx should be available through Intel oneAPI
5046
# (tested with Intel oneAPI 2025.1)

‎devito/__init__.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import atexit
22
from itertools import product
33
import os
4+
from pathlib import Path
45

56
import numpy as np
67

@@ -178,6 +179,14 @@ def profiling_preprocessor(i):
178179
configuration.add('profiling', 'basic', list(profiler_registry),
179180
preprocessor=profiling_preprocessor, impacts_jit=False)
180181

182+
configuration.add(
183+
'cache-dir',
184+
Path(os.environ.get('XDG_CACHE_HOME', Path().home()/'.cache'))/'devito',
185+
preprocessor=lambda p: Path(p).absolute(),
186+
callback=lambda p: (p.mkdir(parents=True, exist_ok=True), p)[1],
187+
impacts_jit=False
188+
)
189+
181190
# Initialize `configuration`
182191
init_configuration()
183192

‎devito/arch/compiler.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
from devito.logger import debug, warning
2424
from devito.parameters import configuration
2525
from devito.tools import (
26-
Signer, as_list, atomic_write, change_directory, filter_ordered, make_tempdir,
27-
memoized_func
26+
Signer, as_list, atomic_write, change_directory, filter_ordered, memoized_func
2827
)
28+
from devito.tools.os_helper import make_cache_dir
2929

3030
__all__ = ['compiler_registry', 'sniff_mpi_distro']
3131

@@ -278,12 +278,12 @@ def get_version(self):
278278
return stdout
279279

280280
def get_jit_dir(self):
281-
"""A deterministic temporary directory for jit-compiled objects."""
282-
return make_tempdir('jitcache')
281+
"""A deterministic cache directory for jit-compiled objects."""
282+
return make_cache_dir('jitcache')
283283

284284
def get_codepy_dir(self):
285-
"""A deterministic temporary directory for the codepy cache."""
286-
return make_tempdir('codepy')
285+
"""A deterministic cache directory for the codepy cache."""
286+
return make_cache_dir('codepy')
287287

288288
def load(self, soname):
289289
"""

‎devito/clear_cache.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from contextlib import suppress
2+
from os import listdir, path
3+
from shutil import rmtree
4+
5+
from . import configuration
6+
7+
8+
def main():
9+
cache_dir = configuration['cache-dir']
10+
for i in listdir(cache_dir):
11+
if i.startswith('codepy-uid') or i.startswith('jitcache-uid'):
12+
with suppress(Exception):
13+
target = path.join(cache_dir, i)
14+
rmtree(target)
15+
16+
17+
if __name__ == '__main__':
18+
main()

‎devito/operator/operator.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from functools import cached_property
66
from math import ceil
77
from operator import attrgetter
8-
from tempfile import gettempdir
8+
from pathlib import Path
99

1010
import numpy as np
1111
import sympy
@@ -1500,7 +1500,7 @@ def nbytes_avail_mapper(self):
15001500
mapper = {}
15011501

15021502
# The amount of space available on the disk
1503-
usage = shutil.disk_usage(gettempdir())
1503+
usage = shutil.disk_usage(Path())
15041504
mapper[disk_layer] = usage.free
15051505

15061506
# The amount of space available on the device

‎devito/parameters.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from functools import wraps
77

88
from devito.logger import info, warning
9-
from devito.tools import Signer, filter_ordered
9+
from devito.tools.abc import Signer
10+
from devito.tools.utils import filter_ordered
1011

1112
__all__ = ['configuration', 'init_configuration', 'print_defaults', 'print_state',
1213
'switchconfig', 'switchenv']
@@ -144,6 +145,7 @@ def _signature_items(self):
144145
'DEVITO_ARCH': 'compiler',
145146
'DEVITO_AUTOPADDING_MODE': 'autopadding-mode',
146147
'DEVITO_AUTOTUNING': 'autotuning',
148+
'DEVITO_CACHE_DIR': 'cache-dir',
147149
'DEVITO_DEVELOP': 'develop-mode',
148150
'DEVITO_DEVICEID': 'deviceid',
149151
'DEVITO_FIRST_TOUCH': 'first-touch',

‎devito/tools/os_helper.py‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
22
from contextlib import suppress
33
from pathlib import Path
4-
from tempfile import gettempdir, mkstemp
4+
from tempfile import mkstemp
55

6-
__all__ = ['atomic_write', 'change_directory', 'make_tempdir']
6+
__all__ = ['atomic_write', 'change_directory', 'make_cache_dir']
77

88

99
class change_directory:
@@ -25,16 +25,14 @@ def __exit__(self, etype, value, traceback):
2525
os.chdir(self.savedPath)
2626

2727

28-
def make_tempdir(prefix=None):
29-
"""Create a temporary directory having a deterministic name. The directory
30-
is created within the default OS temporary directory."""
31-
if prefix is None:
32-
name = f'devito-uid{os.getuid()}'
33-
else:
34-
name = f'devito-{str(prefix)}-uid{os.getuid()}'
35-
tmpdir = Path(gettempdir()).joinpath(name)
36-
tmpdir.mkdir(parents=True, exist_ok=True)
37-
return tmpdir
28+
def make_cache_dir(prefix=None):
29+
"""Create a cache directory for Devito generated files"""
30+
from devito import configuration
31+
32+
name = f'{str(prefix) + "-" if prefix is not None else ""}uid{os.getuid()}'
33+
cache_dir = configuration['cache-dir']/name
34+
cache_dir.mkdir(exist_ok=True)
35+
return cache_dir
3836

3937

4038
def atomic_write(path, data, mode='w', *, overwrite=True):

0 commit comments

Comments
 (0)