Skip to content

BLD: Refactor BLAS/LAPACK wrapper infrastructure#20232

Merged
rgommers merged 5 commits intoscipy:mainfrom
thalassemia:refactor_blas_lapack
Mar 14, 2024
Merged

BLD: Refactor BLAS/LAPACK wrapper infrastructure#20232
rgommers merged 5 commits intoscipy:mainfrom
thalassemia:refactor_blas_lapack

Conversation

@thalassemia
Copy link
Copy Markdown
Contributor

Reference issue

Based on the discussion in gh-19816.

What does this implement/fix?

I refactored scipy/linalg/_generate_pyx.py for better readability and flow. To minimize code duplication in gh-19816, I moved the code shared between this file and that PR into a separate file (scipy/_build_utils/_wrappers_common.py) for both to import.

Before this PR, _generate_pyx.py generated Fortran subroutine wrappers over complex-valued BLAS/LAPACK functions (see gh-19855). This PR cuts that logic (and Fortran) out by adding equivalent C wrappers to scipy/_build_utils/src/wrap_g77_abi.c and wrap_dummy_g77.abi.c.

An additional motivation for these hard-coded C wrappers comes from scipy.linalg._fblas and _flapack, which both currently rely on F2PY-generated Fortran subroutine wrappers. Specifically, in generating these wrappers, F2PY also adds some incorrect code that will cause problems down the line.

To avoid this, I refactored the complex-valued functions in scipy/linalg/fblas_l1.pyf.src into subroutines. This stops F2PY from generating both the problematic code and the Fortran wrappers. I replaced these wrappers with my newly added C wrappers by editing the fortranname statements for the relevant functions.

While I was at it, I added intent(c) statements to the other functions in fblas_l1.pyf.src and flapack_other.pyf.src, modifying callstatement and callprotoargument appropriately. This eliminated Fortran from the code generated by F2PY, leaving pure C.

@github-actions github-actions bot added scipy.sparse.linalg scipy.linalg scipy.sparse Build issues Issues with building from source, including different choices of architecture, compilers and OS C/C++ Items related to the internal C/C++ code base Cython Issues with the internal Cython code base Meson Items related to the introduction of Meson as the new build system for SciPy labels Mar 11, 2024
@lucascolley lucascolley requested a review from h-vetinari March 12, 2024 00:23
@lucascolley lucascolley added maintenance Items related to regular maintenance tasks and removed scipy.sparse.linalg scipy.linalg scipy.sparse labels Mar 12, 2024
@lucascolley lucascolley added this to the 1.13.0 milestone Mar 12, 2024
Copy link
Copy Markdown
Member

@h-vetinari h-vetinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for breaking this out! :)

I'm not familiar with the code in the *.pyf.src files, so it would be good if someone else could check that (though it looks sensible to me). Other than that, I'm happy with this, aside from some minor nits.

@thalassemia thalassemia force-pushed the refactor_blas_lapack branch 2 times, most recently from 0fd1b3b to c9e6989 Compare March 12, 2024 03:10
@tylerjereddy
Copy link
Copy Markdown
Contributor

I'd like to branch for SciPy 1.13.0 around Sunday (17th), let me know if this makes sense to have merged in by then or if we should bump the milestone.

@thalassemia
Copy link
Copy Markdown
Contributor Author

@tylerjereddy Personally, I think this is ready to go, though it would be nice to have one more reviewer.

@h-vetinari
Copy link
Copy Markdown
Member

Both this and #19816 should make it into 1.13. I'm happy with this PR, but I've been mainly waiting if others (@rgommers @ilayn @larsoner) want to comment as well.

@h-vetinari
Copy link
Copy Markdown
Member

@thalassemia, could you please rebase on main? I don't want to squash this PR as the commits are very distinct, but I also don't want to add spurious merge commits.

@thalassemia thalassemia force-pushed the refactor_blas_lapack branch from fd164e6 to 12ac2c4 Compare March 12, 2024 22:16
@rgommers
Copy link
Copy Markdown
Member

I'll try to have a look at this tomorrow.

Copy link
Copy Markdown
Member

@h-vetinari h-vetinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for breaking this out of #19816!

The changes LGTM (modulo those in *.pyf.src, which I don't feel qualified to review).

@rgommers This PR is a consequence of a review I had in the other PR (building upon yet another previous review); here we refactor the existing infrastructure based on a setup that allows easy extension for the 13.3+ enablement, without duplicating the things found in _wrappers_common.py.

That said, this is still +/- the first complete iteration that @thalassemia and I came up with, so it's possible you'd like to change some things.

PS. Here's the _cpointer comment that 12ac2c4 refers to.

@ilayn
Copy link
Copy Markdown
Member

ilayn commented Mar 13, 2024

I can't spare time from work too much these days. Hence did not look very deep into things. However I can't see how they can cause any issues either hence looks good to me.

With little time I have, testing ARPACK/PROPACK rewrite anyways, so soon we can remove these dummy wrappers completely. But probably will happen in 1.14 the earliest.

Copy link
Copy Markdown
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks quite good, thanks a lot @thalassemia. And thanks for the reviews @h-vetinari.

I agree that this looks ready to go. I did some local testing, including with building scikit-learn on top (because our Cython API is very poorly tested by our own test suite). All looked good, not a single build warning or test failure. And changing to C wrappers will likely have multiple benefits down the line.

'cython_lapack_signatures.txt',
],
# Set PYTHONPATH to import from _build_utils/_wrappers_common.py
env: {'PYTHONPATH': meson.global_source_root() / 'scipy/_build_utils'},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I wasn't 100% sure about; should be fine but in case it gives an issue later we can avoid the problem by importing _wrappers_common with importlib like so: https://github.com/numpy/numpy/blob/64676baf423c3e1c53b37ae413c3ca6aaecc642a/numpy/_core/code_generators/genapi.py#L27-L32

@rgommers rgommers merged commit 1ff9e48 into scipy:main Mar 14, 2024
@thalassemia thalassemia deleted the refactor_blas_lapack branch March 14, 2024 17:04
rgommers added a commit to rgommers/scipy that referenced this pull request Apr 11, 2024
This was introduced last month in scipygh-20232; I commented on it there
about it maybe being able to cause an issue later. And it just did,
see scipygh-20433. Upgrading Meson in an existing build environment is
technically unsupported by Meson, but has always "just worked". Now
we hit a corner case on Windows, because when we set an environment
variable in a `meson.build` file, a pickled version of Meson is called
and this is less robust to upgrading Meson in the build env.

The fix is a bit more verbose, but should be reliable - the `importlib`
importing is directly copied from how it's done in NumPy's code
generation scripts.
rgommers added a commit to rgommers/scipy that referenced this pull request Apr 11, 2024
This was introduced last month in scipygh-20232; I commented on it there
about it maybe being able to cause an issue later. And it just did,
see scipygh-20433. Upgrading Meson in an existing build environment is
technically unsupported by Meson, but has always "just worked". Now
we hit a corner case on Windows, because when we set an environment
variable in a `meson.build` file, a pickled version of Meson is called
and this is less robust to upgrading Meson in the build env.

The fix is a bit more verbose, but should be reliable - the `importlib`
importing is directly copied from how it's done in NumPy's code
generation scripts.
@rgommers rgommers mentioned this pull request May 16, 2024
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Build issues Issues with building from source, including different choices of architecture, compilers and OS C/C++ Items related to the internal C/C++ code base Cython Issues with the internal Cython code base maintenance Items related to regular maintenance tasks Meson Items related to the introduction of Meson as the new build system for SciPy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants