Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LAPACK instead of LAPACKE #111

Closed
termoshtt opened this issue May 31, 2018 · 5 comments · Fixed by #206
Closed

Use LAPACK instead of LAPACKE #111

termoshtt opened this issue May 31, 2018 · 5 comments · Fixed by #206
Labels
breaking change Non-compatible change
Milestone

Comments

@termoshtt
Copy link
Member

Note that using row-major ordering may require more memory and time than column-major ordering, because the routine must transpose the row-major order to the column-major order required by the underlying LAPACK routine.

http://www.netlib.org/lapack/lapacke.html

I found while implementing rust-ndarray/ndarray#445 that the interface between CBLAS and LAPACKE around UPLO and other parameter are incompatible. I will use BLAS instead of CBLAS for it since the interface of BLAS is compatible with LAPACK (use u8 always). We should use LAPACK instead of LAPACKE (´・ω・`)

@termoshtt
Copy link
Member Author

related to #74

@jturner314
Copy link
Member

A while ago, I worked on adding support for solving with 2-D X and B matrices (A * X = B). Unfortunately, I didn't finish the implementation (this is as far as I got), but I did learn a few things about ndarray-linalg and LAPACKE/LAPACK memory layout:

  1. The LAPACKE functions allow row-major memory layout, but they require all arrays in an operation to have the same layout. This is a problem because input ndarray arrays can have differing memory layouts, and 2-D solve has 2 input arrays.
  2. For the Netlib implementation of the LAPACKE methods I was looking at, for row-major layout inputs, they copied the inputs to generate column-major arrays, called the Fortran routines, and then copied the column-major result into the row-major output array. (This matches the quote you've pasted above.)
  3. Unlike LAPACKE, there's no reason for ndarray-linalg to maintain the same memory layout across input and output arrays because ndarray allows for arrays of arbitrary layout.

I came to the conclusion that the best thing to do is:

  1. Always convert input arrays to column-major layout (copying only when necessary) and call the LAPACK (Fortran) routine. This is no more expensive than LAPACKE because the LAPACKE implementations do this anyway. It also makes it simpler to support input arrays of arbitrary layout (non-row/column-major).
  2. For functions that operate in-place on a mutable reference to an array, we have to copy the column-major result into the borrowed array. However, for functions that return an owned array, we should always return a column-major array to avoid unnecessary copying. (We should be able to outperform LAPACKE this way.)

I'd also prefer using LAPACK over LAPACKE simply because the documentation is better.

Regarding the implementation, a related issue is rust-ndarray/ndarray#390, although in my initial attempt at an implementation, I used a closure-based approach instead.

@termoshtt
Copy link
Member Author

It makes sense to me.
There is also a merit to use LAPACKE that it manage "work" array.
We use LAPACKE only with column major to avoid unnecessary transpose of result array.

Column major Row major
owned call; return C-major Transpose; call; return C-major
view_mut call; return C-major ref Transpose; call; return C-major ref

Anyway, we need fast transpose routine.

@alexbool
Copy link
Contributor

alexbool commented Jun 1, 2018

AFAIK using LAPACK directly will also allow using Accelerate for ndarray-linalg on macOS because Accelerate has no LAPACKE.

@termoshtt termoshtt added the breaking change Non-compatible change label Jul 13, 2020
@termoshtt termoshtt added this to the 0.13 milestone Jul 13, 2020
@louy2
Copy link

louy2 commented Nov 3, 2021

Excuse me, but with this change, is macOS Accelerate usable as a backend to ndarray-linalg? If so, how do I enable it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Non-compatible change
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants