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

Calling BLAS and negative step views #813

Closed
ghost opened this issue Feb 26, 2021 · 4 comments
Closed

Calling BLAS and negative step views #813

ghost opened this issue Feb 26, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Feb 26, 2021

M = [2. 3.; 0. 0.] 
a = [1.,2.,3.]

v = view(a,3:-1:2)
@test M * v ≈ M * Vector(v)

should pass but fails
Evaluated: [9.0, 0.0] ≈ [12.0, 0.0]

Found while looking at #768, present on a recent 1.7.0-DEV, and 1.4.1.

Looking at LinearAlgebra/blas.jl, it seems there might be more functions which do not correctly handle negative stride yet, that is, the case of a StridedVector being a SubArray.

For the above example, there should be a fix similar to PR JuliaLang/julia#39751. If so, one should probably add an inline function computing the pointer to the first element (in the sense of having the lowest address in memory) of a StridedVector, and use this function when BLAS.dgemv is called for StridedVectors (and make the change from JuliaLang/julia#39751 use this function, too/take the code from there).

Warning: This is based on the assumption that the step size can be negative in the BLAS function.
Reading dgemv.f, I got the impression that negative strides are supported for dgemv in OpenBLAS StridedVector's.
Also page 40 of blas-report.pdf sounds like other BLAS functions should support negative stride for vectors.

@dkarrasch dkarrasch added the bug Something isn't working label Feb 26, 2021
@mbauman
Copy link
Member

mbauman commented Feb 26, 2021

What is the BLAS story on negative strides? Is there a consistent one? A quick search hit this SO question which I had apparently already seen and upvoted at some point in the past.

@ghost
Copy link

ghost commented Feb 26, 2021

Good point! I have edited the description to focus on this specific case. (However, my impression from a quick look at the report linked above is that vectors with negative stride might be fine for some other BLAS functions, too. I don't know how closely various implementations comply to the report.)

simeonschaub referenced this issue in JuliaLang/julia Mar 1, 2021
(Made while looking at #39836, which is matrix x vector.
Matrix x matrix with negative stride is fine as it is, because
it uses the Julia implementation of matrix multiplication.)
ElOceanografo referenced this issue in ElOceanografo/julia May 4, 2021
…aLang#39849)

(Made while looking at #39836, which is matrix x vector.
Matrix x matrix with negative stride is fine as it is, because
it uses the Julia implementation of matrix multiplication.)
antoine-levitt referenced this issue in antoine-levitt/julia May 9, 2021
…aLang#39849)

(Made while looking at #39836, which is matrix x vector.
Matrix x matrix with negative stride is fine as it is, because
it uses the Julia implementation of matrix multiplication.)
@vitorn
Copy link

vitorn commented Sep 30, 2021

Another example of this problem with negative stride (using Julia 1.6.3):

using LinearAlgebra
h = [1.0, 2.0]
x = [1.0, 2.0, 3.0]
xv = view(x, 2 : -1 : 1)
dot(h, xv) 
7.0

but

dot(h, x[2 : -1 : 1])
4.0

If at least one of the vectors has only integer elements, the result is correct.

The problem also happens if you use h'*xv, without loading LinearAlgebra.

@N5N3
Copy link
Member

N5N3 commented Mar 26, 2022

Should be fixed after JuliaLang/julia#42957.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants