Skip to content

Commit

Permalink
BLD: improve handling of CBLAS, add -Duse-ilp64 build option (numpy…
Browse files Browse the repository at this point in the history
…#23984)

In a conda env:
```
Run-time dependency openblas found: YES 0.3.23
Checking if "CBLAS" with dependency openblas: links: YES
```

On Arch Linux before the just-updated 0.23.23-2 build that included
CBLAS and LAPACK in the `openblas` package (see numpygh-23909):
```
$ python -m build --wheel
...
Run-time dependency openblas found: YES
Checking if "CBLAS" with dependency openblas: links: NO
Run-time dependency cblas found: YES 3.11.0
...

$ ldd numpy/core/_multiarray_umath.cpython-311-x86_64-linux-gnu.so
        linux-vdso.so.1 (0x00007fff59235000)
        libopenblas.so.0 => /usr/lib/libopenblas.so.0 (0x00007f5cb4b08000)
        libcblas.so.3 => /usr/lib/libcblas.so.3 (0x00007f5cb62b3000)
        libm.so.6 => /usr/lib/libm.so.6 (0x00007f5cb4a1b000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f5cb628e000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f5cb4831000)
        libgomp.so.1 => /usr/lib/libgomp.so.1 (0x00007f5cb623d000)
        /usr/lib64/ld-linux-x86-64.so.2 (0x00007f5cb62f2000)

```

Closes numpygh-23909
  • Loading branch information
rgommers authored Jun 26, 2023
1 parent 5db8628 commit 32ba5eb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
4 changes: 4 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ option('blas', type: 'string', value: 'openblas',
description: 'option for BLAS library switching')
option('lapack', type: 'string', value: 'openblas',
description: 'option for LAPACK library switching')
option('use-ilp64', type: 'boolean', value: false,
description: 'Use ILP64 (64-bit integer) BLAS and LAPACK interfaces')
option('blas-symbol-suffix', type: 'string', value: '',
description: 'BLAS and LAPACK symbol suffix to use, if any (often `64_` for ILP64)')
option('disable-svml', type: 'boolean', value: false,
description: 'Disable building against SVML')
option('disable-threading', type: 'boolean', value: false,
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ py.extension_module('_multiarray_umath',
'src/npymath',
'src/umath',
],
dependencies: blas,
dependencies: blas_dep,
link_with: npymath_lib,
install: true,
subdir: 'numpy/core',
Expand Down
2 changes: 1 addition & 1 deletion numpy/linalg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif

py.extension_module('lapack_lite',
lapack_lite_module_src,
dependencies: [np_core_dep, lapack],
dependencies: [np_core_dep, blas_dep, lapack_dep],
install: true,
subdir: 'numpy/linalg',
)
Expand Down
72 changes: 60 additions & 12 deletions numpy/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,84 @@ else
blas = dependency(blas_name, required: false)
endif
have_blas = blas.found()
if have_blas and blas_name == 'blas'
cblas = []
if have_blas
# Netlib BLAS has a separate `libcblas.so` which we use directly in the g77
# ABI wrappers, so detect it and error out if we cannot find it.
# ABI wrappers, so detect it and error out if we cannot find it. OpenBLAS can
# be built without CBLAS too (see gh-23909, done by Arch Linux until
# recently)
# In the future, this should be done automatically for:
# `dependency('blas', modules: cblas)`
# see https://github.com/mesonbuild/meson/pull/10921.
cblas = dependency('cblas')
else
cblas = []
have_cblas = false
if cc.links('''
#include <cblas.h>
int main(int argc, const char *argv[])
{
double a[4] = {1,2,3,4};
double b[4] = {5,6,7,8};
return cblas_ddot(4, a, 1, b, 1) > 10;
}
''',
dependencies: blas,
name: 'CBLAS',
)
have_cblas = true
else
cblas = dependency('cblas', required: false)
if cblas.found()
have_cblas = true
endif
endif
endif

if lapack_name == 'openblas'
lapack_name = ['openblas', 'OpenBLAS']
endif
lapack = dependency(lapack_name, required: false)
have_lapack = lapack.found()
lapack_dep = dependency(lapack_name, required: false)
have_lapack = lapack_dep.found()

dependency_map = {
'BLAS': blas,
'LAPACK': lapack,
'LAPACK': lapack_dep,
}

use_ilp64 = get_option('use-ilp64')
if not use_ilp64
# For now, keep supporting this environment variable too (same as in setup.py)
# `false is the default for the CLI flag, so check if env var was set
use_ilp64 = run_command(py,
[
'-c',
'import os; print(1) if os.environ.get("NPY_USE_BLAS_ILP64", "0") != "0" else print(0)'
],
check: true
).stdout().strip() == '1'
endif

# BLAS and LAPACK are optional dependencies for NumPy. We can only use a BLAS
# which provides a CBLAS interface.
# TODO: add ILP64 support
if have_blas
# TODO: this is a shortcut - it needs to be checked rather than used
# unconditionally, and then added to the extension modules that need the
# macro, rather than as a project argument.
add_project_arguments('-DHAVE_CBLAS', language: ['c', 'cpp'])
c_args_blas = [] # note: used for C and C++ via `blas_dep` below
if have_cblas
c_args_blas += ['-DHAVE_CBLAS']
endif
if use_ilp64
c_args_blas += ['-DHAVE_BLAS_ILP64']
endif
# This is currently injected directly into CFLAGS/CXXFLAGS for wheel builds
# (see cibuildwheel settings in pyproject.toml)
blas_symbol_suffix = get_option('blas-symbol-suffix')
if blas_symbol_suffix != ''
c_args_blas += ['-DBLAS_SYMBOL_SUFFIX=' + blas_symbol_suffix]
endif
blas_dep = declare_dependency(
dependencies: [blas, cblas],
compile_args: c_args_blas,
)
else
blas_dep = []
endif

# Copy the main __init__.py|pxd files to the build dir (needed for Cython)
Expand Down

0 comments on commit 32ba5eb

Please sign in to comment.