| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2013-2019 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import typing as T |
| 7 |
import os |
| 8 |
import re |
| 9 |
|
| 10 |
from ..envconfig import detect_cpu_family |
| 11 |
from ..mesonlib import Popen_safe |
| 12 |
from .base import DependencyCandidate, DependencyException, DependencyMethods, detect_compiler, SystemDependency |
| 13 |
from .configtool import ConfigToolDependency |
| 14 |
from .detect import packages |
| 15 |
from .factory import factory_methods |
| 16 |
from .pkgconfig import PkgConfigDependency |
| 17 |
|
| 18 |
if T.TYPE_CHECKING: |
| 19 |
from .factory import DependencyGenerator |
| 20 |
from ..environment import Environment |
| 21 |
from .base import DependencyObjectKWs |
| 22 |
|
| 23 |
|
| 24 |
@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.SYSTEM}) |
| 25 |
def mpi_factory(env: 'Environment', |
| 26 |
kwargs: DependencyObjectKWs, |
| 27 |
methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']: |
| 28 |
language = kwargs.get('language') or 'c' |
| 29 |
if language not in {'c', 'cpp', 'fortran'}: |
| 30 |
# OpenMPI doesn't work without any other languages |
| 31 |
return [] |
| 32 |
|
| 33 |
for_machine = kwargs['native'] |
| 34 |
|
| 35 |
candidates: T.List['DependencyGenerator'] = [] |
| 36 |
compiler = detect_compiler('mpi', env, for_machine, language) |
| 37 |
if not compiler: |
| 38 |
return [] |
| 39 |
compiler_is_intel = compiler.get_id().startswith('intel') |
| 40 |
|
| 41 |
if DependencyMethods.CONFIG_TOOL in methods and not env.machines[for_machine].is_windows(): |
| 42 |
nwargs = kwargs.copy() |
| 43 |
|
| 44 |
# We try the environment variables for the tools first, but then |
| 45 |
# fall back to the hardcoded names |
| 46 |
|
| 47 |
if language == 'c': |
| 48 |
env_vars = ['MPICC'] |
| 49 |
elif language == 'cpp': |
| 50 |
env_vars = ['MPICXX'] |
| 51 |
elif language == 'fortran': |
| 52 |
env_vars = ['MPIFC', 'MPIF90', 'MPIF77'] |
| 53 |
|
| 54 |
tool_names = [os.environ.get(env_name) for env_name in env_vars] |
| 55 |
tool_names = [t for t in tool_names if t] # remove empty environment variables |
| 56 |
|
| 57 |
if compiler_is_intel: |
| 58 |
# The oneAPI compilers have different wrappers |
| 59 |
is_llvm_based = 'llvm' in compiler.id |
| 60 |
if env.machines[for_machine].is_windows(): |
| 61 |
nwargs['returncode_value'] = 3 |
| 62 |
|
| 63 |
if language == 'c': |
| 64 |
if is_llvm_based: |
| 65 |
tool_names.append('mpiicx') |
| 66 |
else: |
| 67 |
tool_names.append('mpiicc') |
| 68 |
elif language == 'cpp': |
| 69 |
if is_llvm_based: |
| 70 |
tool_names.append('mpiicpx') |
| 71 |
else: |
| 72 |
tool_names.append('mpiicpc') |
| 73 |
elif language == 'fortran': |
| 74 |
if is_llvm_based: |
| 75 |
tool_names.append('mpiifx') |
| 76 |
else: |
| 77 |
tool_names.append('mpiifort') |
| 78 |
|
| 79 |
# even with intel compilers, mpicc has to be considered |
| 80 |
if language == 'c': |
| 81 |
tool_names.append('mpicc') |
| 82 |
elif language == 'cpp': |
| 83 |
tool_names.extend(['mpic++', 'mpicxx', 'mpiCC']) |
| 84 |
elif language == 'fortran': |
| 85 |
tool_names.extend(['mpifort', 'mpif90', 'mpif77']) |
| 86 |
|
| 87 |
nwargs['tools'] = tool_names |
| 88 |
candidates.append(DependencyCandidate.from_dependency( |
| 89 |
tool_names[0], MPIConfigToolDependency, (env, nwargs))) |
| 90 |
|
| 91 |
if DependencyMethods.SYSTEM in methods and env.machines[for_machine].is_windows(): |
| 92 |
candidates.append(DependencyCandidate.from_dependency( |
| 93 |
'msmpi', MSMPIDependency, (env, kwargs))) |
| 94 |
candidates.append(DependencyCandidate.from_dependency( |
| 95 |
'impi', IMPIDependency, (env, kwargs))) |
| 96 |
|
| 97 |
# Only OpenMPI has pkg-config, and it doesn't work with the intel compilers |
| 98 |
# for MPI, environment variables and commands like mpicc should have priority |
| 99 |
if DependencyMethods.PKGCONFIG in methods and not compiler_is_intel: |
| 100 |
pkg_name = None |
| 101 |
if language == 'c': |
| 102 |
pkg_name = 'ompi-c' |
| 103 |
elif language == 'cpp': |
| 104 |
pkg_name = 'ompi-cxx' |
| 105 |
elif language == 'fortran': |
| 106 |
pkg_name = 'ompi-fort' |
| 107 |
candidates.append(DependencyCandidate.from_dependency( |
| 108 |
pkg_name, PkgConfigDependency, (env, kwargs))) |
| 109 |
|
| 110 |
return candidates |
| 111 |
|
| 112 |
packages['mpi'] = mpi_factory |
| 113 |
|
| 114 |
|
| 115 |
class MPIConfigToolDependency(ConfigToolDependency): |
| 116 |
"""Wrapper around mpicc, Intel's mpiicc and friends.""" |
| 117 |
|
| 118 |
def __init__(self, name: str, env: 'Environment', kwargs: DependencyObjectKWs): |
| 119 |
super().__init__(name, env, kwargs) |
| 120 |
if not self.is_found: |
| 121 |
return |
| 122 |
|
| 123 |
for comp, link in [ |
| 124 |
('--showme:compile', '--showme:link'), # for OpenMPI |
| 125 |
('-show-compile-info', '-show-link-info'), # for MPICH and Intel MPI |
| 126 |
('-compile_info', '-link_info'), # for older MPICH and Intel MPI |
| 127 |
('-show', None), |
| 128 |
]: |
| 129 |
try: |
| 130 |
# Set required=True to ensure that the next set of options is |
| 131 |
# tried when the current ones fail, even if the dependency is |
| 132 |
# not required |
| 133 |
c_args = self.get_config_value([comp], 'compile_args', required=True) |
| 134 |
l_args = self.get_config_value([link], 'link_args', required=True) if link is not None else c_args |
| 135 |
except DependencyException: |
| 136 |
continue |
| 137 |
else: |
| 138 |
break |
| 139 |
else: |
| 140 |
self.is_found = False |
| 141 |
return |
| 142 |
|
| 143 |
self.compile_args = self._filter_compile_args(c_args) |
| 144 |
self.link_args = self._filter_link_args(l_args) |
| 145 |
|
| 146 |
def _filter_compile_args(self, args: T.List[str]) -> T.List[str]: |
| 147 |
""" |
| 148 |
MPI wrappers return a bunch of garbage args. |
| 149 |
Drop -O2 and everything that is not needed. |
| 150 |
""" |
| 151 |
result = [] |
| 152 |
multi_args: T.Tuple[str, ...] = ('-I', ) |
| 153 |
if self.language == 'fortran': |
| 154 |
fc = self.env.coredata.compilers[self.for_machine]['fortran'] |
| 155 |
multi_args += fc.get_module_incdir_args() |
| 156 |
|
| 157 |
include_next = False |
| 158 |
for f in args: |
| 159 |
if f.startswith(('-D', '-f') + multi_args) or f == '-pthread' \ |
| 160 |
or (f.startswith('-W') and f != '-Wall' and not f.startswith('-Werror')): |
| 161 |
result.append(f) |
| 162 |
if f in multi_args: |
| 163 |
# Path is a separate argument. |
| 164 |
include_next = True |
| 165 |
elif include_next: |
| 166 |
include_next = False |
| 167 |
result.append(f) |
| 168 |
return result |
| 169 |
|
| 170 |
def _filter_link_args(self, args: T.List[str]) -> T.List[str]: |
| 171 |
""" |
| 172 |
MPI wrappers return a bunch of garbage args. |
| 173 |
Drop -O2 and everything that is not needed. |
| 174 |
""" |
| 175 |
result = [] |
| 176 |
include_next = False |
| 177 |
for f in args: |
| 178 |
if self._is_link_arg(f): |
| 179 |
result.append(f) |
| 180 |
if f in {'-L', '-Xlinker'}: |
| 181 |
include_next = True |
| 182 |
elif include_next: |
| 183 |
include_next = False |
| 184 |
result.append(f) |
| 185 |
return result |
| 186 |
|
| 187 |
def _is_link_arg(self, f: str) -> bool: |
| 188 |
if self.clib_compiler.id == 'intel-cl': |
| 189 |
return f == '/link' or f.startswith('/LIBPATH') or f.endswith('.lib') # always .lib whether static or dynamic |
| 190 |
else: |
| 191 |
return (f.startswith(('-L', '-l', '-Xlinker')) or |
| 192 |
f == '-pthread' or |
| 193 |
(f.startswith('-W') and f != '-Wall' and not f.startswith('-Werror'))) |
| 194 |
|
| 195 |
def _check_and_get_version(self, tool: T.List[str], returncode: int) -> T.Tuple[bool, T.Union[str, None]]: |
| 196 |
p, out = Popen_safe(tool + ['--showme:version'])[:2] |
| 197 |
valid = p.returncode == returncode |
| 198 |
if valid: |
| 199 |
# OpenMPI |
| 200 |
v = re.search(r'\d+.\d+.\d+', out) |
| 201 |
if v: |
| 202 |
version = v.group(0) |
| 203 |
else: |
| 204 |
version = None |
| 205 |
return valid, version |
| 206 |
|
| 207 |
# --version is not the same as -v |
| 208 |
p, out = Popen_safe(tool + ['-v'])[:2] |
| 209 |
valid = p.returncode == returncode |
| 210 |
first_line = out.split('\n', maxsplit=1)[0] |
| 211 |
|
| 212 |
# cases like "mpicc for MPICH version 4.2.2" |
| 213 |
v = re.search(r'\d+.\d+.\d+', first_line) |
| 214 |
if v: |
| 215 |
return valid, v.group(0) |
| 216 |
|
| 217 |
# cases like "mpigcc for Intel(R) MPI library 2021.13" |
| 218 |
v = re.search(r'\d+.\d+', first_line) |
| 219 |
if v: |
| 220 |
return valid, v.group(0) |
| 221 |
|
| 222 |
# cases like "mpiifort for the Intel(R) MPI Library 2019 Update 9 for Linux*" |
| 223 |
v = re.search(r'(\d{4}) Update (\d)', first_line) |
| 224 |
if v: |
| 225 |
return valid, f'{v.group(1)}.{v.group(2)}' |
| 226 |
|
| 227 |
return valid, None |
| 228 |
|
| 229 |
|
| 230 |
class MSMPIDependency(SystemDependency): |
| 231 |
|
| 232 |
"""The Microsoft MPI.""" |
| 233 |
|
| 234 |
def __init__(self, name: str, env: 'Environment', kwargs: DependencyObjectKWs): |
| 235 |
super().__init__(name, env, kwargs) |
| 236 |
# MSMPI only supports the C API |
| 237 |
if self.language not in {'c', 'fortran', None}: |
| 238 |
self.is_found = False |
| 239 |
return |
| 240 |
# MSMPI is only for windows, obviously |
| 241 |
if not self.env.machines[self.for_machine].is_windows(): |
| 242 |
return |
| 243 |
|
| 244 |
incdir = os.environ.get('MSMPI_INC') |
| 245 |
arch = detect_cpu_family(self.env.coredata.compilers.host) |
| 246 |
libdir = None |
| 247 |
if arch == 'x86': |
| 248 |
libdir = os.environ.get('MSMPI_LIB32') |
| 249 |
post = 'x86' |
| 250 |
elif arch == 'x86_64': |
| 251 |
libdir = os.environ.get('MSMPI_LIB64') |
| 252 |
post = 'x64' |
| 253 |
|
| 254 |
if libdir is None or incdir is None: |
| 255 |
self.is_found = False |
| 256 |
return |
| 257 |
|
| 258 |
self.is_found = True |
| 259 |
self.link_args = ['-l' + os.path.join(libdir, 'msmpi')] |
| 260 |
self.compile_args = ['-I' + incdir, '-I' + os.path.join(incdir, post)] |
| 261 |
if self.language == 'fortran': |
| 262 |
self.link_args.append('-l' + os.path.join(libdir, 'msmpifec')) |
| 263 |
|
| 264 |
|
| 265 |
class IMPIDependency(SystemDependency): |
| 266 |
|
| 267 |
"""Intel(R) MPI for Windows.""" |
| 268 |
|
| 269 |
def __init__(self, name: str, env: Environment, kwargs: DependencyObjectKWs): |
| 270 |
super().__init__(name, env, kwargs) |
| 271 |
# only for windows |
| 272 |
if not self.env.machines[self.for_machine].is_windows(): |
| 273 |
return |
| 274 |
# only for x86_64 |
| 275 |
if self.env.machines[self.for_machine].cpu_family != 'x86_64': |
| 276 |
return |
| 277 |
|
| 278 |
rootdir = os.environ.get('I_MPI_ROOT') |
| 279 |
if rootdir is None: |
| 280 |
self.is_found = False |
| 281 |
return |
| 282 |
|
| 283 |
incdir = os.path.join(rootdir, 'include') |
| 284 |
libdir = os.path.join(rootdir, 'lib') |
| 285 |
|
| 286 |
debug = env.coredata.optstore.get_value_for('debug') |
| 287 |
assert isinstance(debug, bool) |
| 288 |
libdir_post = 'debug' if debug else 'release' |
| 289 |
for subdirs in (['mpi', libdir_post], [libdir_post]): |
| 290 |
libdir_buildtype = os.path.join(libdir, *subdirs) |
| 291 |
if os.path.isdir(libdir_buildtype): |
| 292 |
libdir = libdir_buildtype |
| 293 |
break |
| 294 |
|
| 295 |
found_header = os.path.isfile(os.path.join(incdir, 'mpi.h')) |
| 296 |
found_library = os.path.isfile(os.path.join(libdir, 'impi.lib')) |
| 297 |
if not found_header or not found_library: |
| 298 |
self.is_found = False |
| 299 |
return |
| 300 |
|
| 301 |
self.is_found = True |
| 302 |
self.compile_args = ['-I' + incdir] |
| 303 |
self.link_args = ['-l' + os.path.join(libdir, 'impi')] |
| 304 |
if self.language == 'cpp': |
| 305 |
# Some installations do not have the MPI C++ bindings library |
| 306 |
if not os.path.isfile(os.path.join(libdir, 'impicxx.lib')): |
| 307 |
self.is_found = False |
| 308 |
return |
| 309 |
self.link_args = ['-l' + os.path.join(libdir, 'impicxx')] |