Skip to content

Commit

Permalink
arch: fix compiler sniff error handling
Browse files Browse the repository at this point in the history
Fixed compiler version sniff and test

Added more information for failed MPI import

Carry mpi4py import error in dummy MPI class

Raised RuntimeError for missing compiler instead of sys.exit()

Fixed flake8 errors

Moved test to test_arch.py
  • Loading branch information
gbruer15 authored and mloubout committed Apr 29, 2024
1 parent e2743e0 commit a1dabe9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion benchmarks/user/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def test(problem, **kwargs):
"DEVITO_MPI is unset. Setting `DEVITO_MPI=basic`..."
% MPI.COMM_WORLD.size)
configuration['mpi'] = 'basic'
except TypeError:
except (TypeError, ModuleNotFoundError):
# MPI not available
pass

Expand Down
8 changes: 3 additions & 5 deletions devito/arch/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
check_call, run)
import platform
import warnings
import sys
import time

import numpy.ctypeslib as npct
Expand All @@ -18,7 +17,7 @@
IntelDevice, get_nvidia_cc, check_cuda_runtime,
get_m1_llvm_path)
from devito.exceptions import CompilationError
from devito.logger import debug, warning, error
from devito.logger import debug, warning
from devito.parameters import configuration
from devito.tools import (as_list, change_directory, filter_ordered,
memoized_func, make_tempdir)
Expand All @@ -42,12 +41,11 @@ def sniff_compiler_version(cc, allow_fail=False):
return Version("0")
except UnicodeDecodeError:
return Version("0")
except FileNotFoundError:
except OSError:
if allow_fail:
return Version("0")
else:
error("The `%s` compiler isn't available on this system" % cc)
sys.exit(1)
raise RuntimeError("The `%s` compiler isn't available on this system" % cc)

if ver.startswith("gcc"):
compiler = "gcc"
Expand Down
8 changes: 7 additions & 1 deletion devito/mpi/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ def cleanup():
if init_by_devito and MPI.Is_initialized() and not MPI.Is_finalized():
MPI.Finalize()
atexit.register(cleanup)
except ImportError:
except ImportError as e:
# Dummy fallback in case mpi4py/MPI aren't available
class NoneMetaclass(type):
def __getattr__(self, name):
return None

class MPI(object, metaclass=NoneMetaclass):
init_error = e

@classmethod
def Init(cls):
raise cls.init_error

@classmethod
def Is_initialized(cls):
return False
Expand Down
12 changes: 12 additions & 0 deletions tests/test_arch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

from devito.arch.compiler import sniff_compiler_version


@pytest.mark.parametrize("cc", [
"doesn'texist",
"/root/doesn'texist",
])
def test_sniff_compiler_version(cc):
with pytest.raises(RuntimeError, match=cc):
sniff_compiler_version(cc)

0 comments on commit a1dabe9

Please sign in to comment.