From 06532a823fb5ccf6847480a2b2dd811dce836ba9 Mon Sep 17 00:00:00 2001 From: "Michael F. Herbst" Date: Sat, 9 Nov 2019 14:11:00 +0100 Subject: [PATCH] Detect and honour an MKL numpy when building adccore (#23) --- docs/developers.rst | 17 +++++++++++++++++ extension/AdcCore.py | 27 ++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/developers.rst b/docs/developers.rst index 080f7be0..14c8c983 100644 --- a/docs/developers.rst +++ b/docs/developers.rst @@ -139,6 +139,23 @@ This is done to avoid automatically overwriting some development changes you might have made inside ``adccore``. +Building adccore with MKL support +--------------------------------- + +If you have full source code access +and you are able to follow the :ref:`adccore-sources`, +the `Intel Math Kernel Library (R) `_ +can also be integrated into adccore and thus adcc. +In fact this integration happens automatically during the build +process of adccore, given that a numpy linked to the MKL was +detected. For this reason proceed as follows: + +1. Load the MKL modules or activate the MKL in your shell as you usally do. +2. Build and install numpy with linkage to this MKL, + e.g. `Build numpy from source `_. +3. Build adcc and adccore as described in :ref:`adccore-sources`. + + ``setup.py`` reference ---------------------- The ``setup.py`` script of adcc is a largely a typical setuptools script, diff --git a/extension/AdcCore.py b/extension/AdcCore.py index 97de88ea..cfe7dc52 100644 --- a/extension/AdcCore.py +++ b/extension/AdcCore.py @@ -41,6 +41,27 @@ def get_platform(): return result +def has_mkl_numpy(): + """Has numpy been installed and linked against MKL""" + try: + import numpy.__config__ + + if not hasattr(numpy.__config__, "blas_mkl_info"): + return False + return any("mkl" in lib + for lib in numpy.__config__.blas_mkl_info.get("libraries", {})) + except ImportError as e: + if "mkl" in str(e): + # numpy seems to be installed and linked against MKL, but mkl was not found. + raise ImportError("Trying to import numpy for MKL check, but obtained an " + "import error indicating a missing MKL dependency. " + "Did you load the MKL modules properly?") from e + + # This indicates a missing numpy or a big error in numpy. It's best to assume + # MKL is not there and (potentially) install the non-mkl version from pypi + return False + + def request_urllib(url, filename): """Download a file from the net using requests, displaying a nice progress bar along the way""" @@ -123,8 +144,12 @@ def build(self): import build_adccore + features = [] + if has_mkl_numpy(): + features += ["mkl"] + build_dir = join(self.source_dir, "build") - build_adccore.build_install(build_dir, self.install_dir) + build_adccore.build_install(build_dir, self.install_dir, features=features) def build_documentation(self): """Build adccore documentation. Only valid if has_source is true"""