Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/examples-mpi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 : |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pytest-core-mpi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
10 changes: 6 additions & 4 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
12 changes: 4 additions & 8 deletions benchmarks/user/advisor/run_advisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions devito/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import atexit
from itertools import product
import os
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -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()

Expand Down
12 changes: 6 additions & 6 deletions devito/arch/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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):
"""
Expand Down
18 changes: 18 additions & 0 deletions devito/clear_cache.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions devito/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion devito/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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',
Expand Down
22 changes: 10 additions & 12 deletions devito/tools/os_helper.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Comment thread
JDBetteridge marked this conversation as resolved.
Resolved

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):
Expand Down
12 changes: 0 additions & 12 deletions scripts/clear_devito_cache.py

This file was deleted.

Loading