From a1dabe9a7c42f1fb26385603c5286eed435f80c0 Mon Sep 17 00:00:00 2001 From: Grant Bruer Date: Wed, 27 Mar 2024 16:29:55 -0400 Subject: [PATCH] arch: fix compiler sniff error handling 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 --- benchmarks/user/benchmark.py | 2 +- devito/arch/compiler.py | 8 +++----- devito/mpi/distributed.py | 8 +++++++- tests/test_arch.py | 12 ++++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 tests/test_arch.py diff --git a/benchmarks/user/benchmark.py b/benchmarks/user/benchmark.py index f737e870a3..420f18df02 100644 --- a/benchmarks/user/benchmark.py +++ b/benchmarks/user/benchmark.py @@ -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 diff --git a/devito/arch/compiler.py b/devito/arch/compiler.py index c51bd90daf..c85ec156f2 100644 --- a/devito/arch/compiler.py +++ b/devito/arch/compiler.py @@ -6,7 +6,6 @@ check_call, run) import platform import warnings -import sys import time import numpy.ctypeslib as npct @@ -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) @@ -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" diff --git a/devito/mpi/distributed.py b/devito/mpi/distributed.py index b9efa0010f..b1b0d6de1a 100644 --- a/devito/mpi/distributed.py +++ b/devito/mpi/distributed.py @@ -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 diff --git a/tests/test_arch.py b/tests/test_arch.py new file mode 100644 index 0000000000..fd00bd70da --- /dev/null +++ b/tests/test_arch.py @@ -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)