Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WIP: DOC: write a contributor guide page about using BLAS/LAPACK#21130

Draft
rgommers wants to merge 1 commit into
scipy:mainfrom
rgommers:using-blas-lapack-from-c
Draft

WIP: DOC: write a contributor guide page about using BLAS/LAPACK#21130
rgommers wants to merge 1 commit into
scipy:mainfrom
rgommers:using-blas-lapack-from-c

Conversation

@rgommers
Copy link
Copy Markdown
Member

@rgommers rgommers commented Jul 7, 2024

Closes gh-20002.

@rgommers rgommers added scipy.linalg Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org C/C++ Items related to the internal C/C++ code base labels Jul 7, 2024
@github-actions github-actions Bot added the DX Everything related to making the experience of working on SciPy more pleasant label Jul 7, 2024
Copy link
Copy Markdown
Contributor

@tylerjereddy tylerjereddy 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 very useful/excellent start. I added only minor proofreading things, and learned a fair bit reading through. This is certainly quite complex, I imagine there may be some discussion.

a common symbol prefix and/or suffix. Examples:

- ``scipy-openblas`` always uses a ``scipy_`` symbol prefix.
- The ILP64 OpenBLAS builds use a ``64_`` symbol suffix (typically;it's
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

space after ;

- ``scipy-openblas`` always uses a ``scipy_`` symbol prefix.
- The ILP64 OpenBLAS builds use a ``64_`` symbol suffix (typically;it's
also possible that there is no suffix).
- Accelerate uses a ``$NEWLAPACK`` suffix for LP64 symbols, and ``$NEWLAPACK_ILP64`` for ILP64 symbols.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Interesting, is the $ explicit/raw, or meant to be substituted like a variable? That's a weird one!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's an actual raw $ as part of the symbol name.

also possible that there is no suffix).
- Accelerate uses a ``$NEWLAPACK`` suffix for LP64 symbols, and ``$NEWLAPACK_ILP64`` for ILP64 symbols.

Finally, there's is compiler mangling. Most commonly used Fortran compilers
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there's is -> there is

@ilayn
Copy link
Copy Markdown
Member

ilayn commented Jul 7, 2024

This is indeed great and much needed. Thank you for this.

``build/scipy/linalg/_lapack_subroutines.h`` with ``C_INT`` instead of
``int``...
- call the functions as ``BLAS_FUNC(name)``, where ``name`` is the canonical
name for the BLAS or LAPACK function (e.g., ``daxpy``)
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.

What seems to work today is

$ git diff HEAD^
diff --git a/scipy/interpolate/meson.build b/scipy/interpolate/meson.build
index 4c3631c0e1..25973a0846 100644
--- a/scipy/interpolate/meson.build
+++ b/scipy/interpolate/meson.build
@@ -123,8 +123,7 @@ py3.extension_module('_rgi_cython',
 
 __fitpack_lib = static_library('__fitpack',
     ['src/__fitpack.h', 'src/__fitpack.cc'],
-    dependencies:[lapack]
+    dependencies:[lapack, np_dep, py3_dep]
 )
 
 __fitpack_dep = declare_dependency(
diff --git a/scipy/interpolate/src/__fitpack.h b/scipy/interpolate/src/__fitpack.h
index 471eca55a8..6f71adb7a2 100644
--- a/scipy/interpolate/src/__fitpack.h
+++ b/scipy/interpolate/src/__fitpack.h
@@ -2,9 +2,11 @@
 #include <iostream>
 #include <tuple>
 #include <vector>
+
+#include "../_build_utils/src/npy_cblas.h"
 #include "../_build_utils/src/fortran_defs.h"
 
-#define DLARTG F_FUNC(dlartg, DLARTG)
+#define DLARTG BLAS_FUNC(dlartg)

It'd be great indeed to generate the header file so that it can be included --- unless the dance for telling meson to use a generated header is less than this (which I'm not sure about TBH).

@ev-br
Copy link
Copy Markdown
Member

ev-br commented Sep 21, 2024

Recently, we added code using two ways of declaring LAPACK prototypes from C/C++ :

  1. Declaring plain unmangled BLAS/LAPACK prototypes (https://github.com/scipy/scipy/blob/main/scipy/optimize/__lbfgsb.h#L26):
#include "../_build_utils/src/npy_cblas.h"
#include "../_build_utils/src/fortran_defs.h"
void dpotrf_(char* uplo, int* n, double* a, int* lda, int* info);
  1. Using BLAS_FUNC name mangling from npy_cblas.h (https://github.com/scipy/scipy/blob/main/scipy/interpolate/src/__fitpack.h#L83):
#include "../_build_utils/src/npy_cblas.h"
#include "../_build_utils/src/fortran_defs.h"
#define DLARTG BLAS_FUNC(dlartg)
void DLARTG(double *f, double *g, double *cs, double *sn, double *r);

Both ways pass CI, both our regular runs and wheel builds.

Ralf pointed out offline that 1. possibly works because the SciPy build process constructs name mangled wrappers already, via https://github.com/scipy/scipy/blob/main/scipy/_build_utils/_generate_blas_wrapper.py and https://github.com/scipy/scipy/blob/main/scipy/_build_utils/meson.build#L12

So does it mean we don't need to redo mangling manually and 1. is the right way forward?

@ilayn
Copy link
Copy Markdown
Member

ilayn commented Sep 21, 2024

First two lines of 1. is not needed. That's my experiment I forgot to remove. I'll fix it when I am done troubleshooting #21583, (the problem size is large so it is taking some time to figure it out). The better example is #21553 for item 1 without those lines.

@rgommers
Copy link
Copy Markdown
Member Author

Solution (2) should be the right thing to use. I'm pretty sure that (1) is not robust. Fortran compiler mangling results are compiler-dependent, so the trailing underscore in void dpotrf_ isn't guaranteed to be there. CI passes because we only test with compilers that do add that underscore, plus:

Ralf pointed out offline that 1. possibly works because the SciPy build process constructs name mangled wrappers already

Note that we do this if and only if there's a symbol prefix or suffix used (so for scipy-openblas32 and for Accelerate). The reason for that is that the generated wrappers add ~1600 files and increase compile times by 10-20%. Hence, a regular LAPACK install built with a compiler that doesn't add the trailing underscore won't work right now.

@ilayn
Copy link
Copy Markdown
Member

ilayn commented Sep 22, 2024

Problem I encountered with (2) is that not all symbols are available (#21314). So that gives me the impression that the list is maintained and not generated.

I would prefer a solution, if possible at all, that we create some intermediate layer that separates compiler issues from the symbol names. That is to say from the application layer all LAPACK consuming routines see a single standard (with or without underscore, or usage like (2) etc. it doesn't matter what the standard is) and we handle the symbols at a central place potentially investigating the names and generating some runtime macros, somehow take care of it in one place.

I still don't have a good grip on where things happen or which tool is leaking symbols of (1) or (2) but when you are back in action let's have an offline session together with Evgeni if you folks are up to it.

@ev-br
Copy link
Copy Markdown
Member

ev-br commented Sep 22, 2024

I would prefer a solution, if possible at all, that we create some intermediate layer that separates compiler issues from the symbol names.

+1. We had an offline discussion that there's a header file autogenerated at some build stage, and maybe the right thing is to just commit it to the repository instead. That would be my preferred solution, too.
Will need to poke around a bit though.

let's have an offline session ... if you folks are up to it.

Definitely let's!

@rgommers
Copy link
Copy Markdown
Member Author

xref gh-22743 for some relevant discussion and updates to support ILP64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C/C++ Items related to the internal C/C++ code base Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org DX Everything related to making the experience of working on SciPy more pleasant scipy.linalg

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: linking LAPACK from C/C++

4 participants