Skip to content
Merged
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
8 changes: 8 additions & 0 deletions devito/arch/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ def jit_compile(self, soname, code):
# Typically we end up here
# Make a suite of cache directories based on the soname
cache_dir.mkdir(parents=True, exist_ok=True)

# Has the library already been compiled?
Comment thread
mloubout marked this conversation as resolved.
try:
self.load(target)
return False, src_file
except OSError:
Comment thread
JDBetteridge marked this conversation as resolved.
# The .so file isn't present, we need to compile it
pass
else:
# Warning: dropping `code` on the floor in favor to whatever is written
# within `src_file`
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cinterface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import sys
from contextlib import suppress

from devito import Eq, Grid, Operator, TimeFunction, configuration
from devito.types import Timer
Expand Down Expand Up @@ -37,3 +39,31 @@ def test_basic():
assert isinstance(timers, Timer)
assert 'struct profiler\n{' not in ccode
assert 'struct profiler\n{' in hcode


def test_load_without_source():
grid = Grid(shape=(4, 4))

f = TimeFunction(name='f', grid=grid)

eq = Eq(f.forward, f + 1.)

name = "foo"
op = Operator(eq, name=name)

# Make sure the so file is not present
dirname = op._compiler.get_jit_dir()
soname = op._soname
ext = '.dylib' if sys.platform == "darwin" else '.so'
with suppress(FileNotFoundError):
os.remove(f"{os.path.join(dirname, soname)}{ext}")

# Trigger compilation
recompiled, src_file = op._compiler.jit_compile(op._soname, str(op))
assert recompiled

os.remove(src_file)
# Trigger compilation again. It should skip the C part and directly load the .so file
recompiled, src_file = op._compiler.jit_compile(op._soname, str(op))
assert not recompiled
assert not os.path.isfile(src_file)
Loading