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

axpy! error:parent must be contiguous #44497

Closed
andreasvarga opened this issue Mar 7, 2022 · 7 comments · Fixed by #44507
Closed

axpy! error:parent must be contiguous #44497

andreasvarga opened this issue Mar 7, 2022 · 7 comments · Fixed by #44507
Labels
linear algebra Linear algebra

Comments

@andreasvarga
Copy link
Contributor

andreasvarga commented Mar 7, 2022

After a routine update of MatrixEquations, the nightly run fails with strange errors see here, while the tests on Julia 1.6 and 1.7 run error free. All errors contain

ArgumentError: Parent must be contiguous.

occured in

[5] axpy!(alpha::Float64, x::SubArray{Float64, 1, Matrix{Float64}, Tuple{UnitRange{Int64}, Int64}, true}, y::SubArray{Float64, 1, Base.ReshapedArray{Float64, 1, SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}, Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}, Tuple{UnitRange{Int64}}, false})
      @ LinearAlgebra.BLAS /opt/hostedtoolcache/julia/nightly/x64/share/julia/stdlib/v1.9/LinearAlgebra/src/blas.jl:496

@fredrikekre fredrikekre changed the title Failures with the nightly version axpy! error:parent must be contiguous Mar 7, 2022
@fredrikekre fredrikekre added the linear algebra Linear algebra label Mar 7, 2022
@andreasvarga
Copy link
Contributor Author

andreasvarga commented Mar 7, 2022

As for my part, I fixed the problem, making some vectors contiguous (avoiding reshaping of views). This probably will make the code even more efficient. With my last run, I see no more errors of type axpy! error:parent must be contiguous.

However, I assume that somebody from the Julia team is trying to also fix the problem, since I obtained new error messages (which were not present before):

ArgumentError: only support vector like inputs
  Stacktrace:
    [1] vec_pointer_stride
      @ /opt/hostedtoolcache/julia/nightly/x64/share/julia/stdlib/v1.9/LinearAlgebra/src/blas.jl:152 [inlined]
    [2] vec_pointer_stride
      @ /opt/hostedtoolcache/julia/nightly/x64/share/julia/stdlib/v1.9/LinearAlgebra/src/blas.jl:151 [inlined]
    [3] axpy!(alpha::Float64, x::SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}, y::SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false})
      @ LinearAlgebra.BLAS /opt/hostedtoolcache/julia/nightly/x64/share/julia/stdlib/v1.9/LinearAlgebra/src/blas.jl:496

@dkarrasch
Copy link
Member

From your fix in andreasvarga/MatrixEquations.jl@d3289c6, it looks like you were having zero-stride cases, which may have undefined behavior depending on which BLAS engine is used... or something like that. @N5N3 will probably know.

@N5N3
Copy link
Member

N5N3 commented Mar 7, 2022

The first error comes from strides for ReshapedArray. It currently only support "dense" array and StridedVector as parent. From the error message, your parent array isa SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}. IIUC you want to vec() it, so it's very likely the strides is undefined (and undefinable).

As for the second error, it hit the layout check before BLAS call. BLAS.axpy! is a level I function, thus it only support vector-like inputs. e.g.

julia> BLAS.vec_pointer_stride(view(randn(3,3),:,1:2))
(Ptr{Float64} @0x000000004778dcc0, 1)

julia> BLAS.vec_pointer_stride(view(randn(3,3),1:3,1:2))
(Ptr{Float64} @0x000000004dfe08c0, 1)

julia> BLAS.vec_pointer_stride(view(randn(3,3),1:2,1:2))
ERROR: ArgumentError: only support vector like inputs
Stacktrace:
 [1] vec_pointer_stride
   @ C:\Users\MYJ\AppData\Local\Programs\Julia-1.9.0-DEV\share\julia\stdlib\v1.9\LinearAlgebra\src\blas.jl:152 [inlined]
 [2] vec_pointer_stride(x::SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false})
   @ LinearAlgebra.BLAS C:\Users\MYJ\AppData\Local\Programs\Julia-1.9.0-DEV\share\julia\stdlib\v1.9\LinearAlgebra\src\blas.jl:151
 [3] top-level scope
   @ REPL[6]:1

I didn't look into your code. But I guess you'd better use LinearAlgebra.axpy! instead.

Edit: some special cases.

julia> BLAS.vec_pointer_stride(view(randn(3,3),1:2,1:1))
ERROR: ArgumentError: only support vector like inputs
Stacktrace:
 [1] vec_pointer_stride
   @ C:\Users\MYJ\AppData\Local\Programs\Julia-1.9.0-DEV\share\julia\stdlib\v1.9\LinearAlgebra\src\blas.jl:152 [inlined]
 [2] vec_pointer_stride(x::SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false})
   @ LinearAlgebra.BLAS C:\Users\MYJ\AppData\Local\Programs\Julia-1.9.0-DEV\share\julia\stdlib\v1.9\LinearAlgebra\src\blas.jl:151
 [3] top-level scope
   @ REPL[18]:1

julia> BLAS.vec_pointer_stride(view(randn(3,3),1:2,1))
(Ptr{Float64} @0x0000000048b16fc0, 1)

Maybe we should ingore all 1-sized dim in the check.

@andreasvarga
Copy link
Contributor Author

The first error has been fixed.

I didn't look into your code. But I guess you'd better use LinearAlgebra.axpy! instead.

I tried it, but I get the same errors: ERROR: ArgumentError: only support vector like inputs

A line where this happens is
LinearAlgebra.axpy!(-α[1,1], v, rbar)
where rbar and v are views in Wr and Wv, respectively, defined as below:

           Wr = Matrix{T1}(undef,n,2)
            Wv = similar(Wr)
            dll = 1:dl
            ir1 = 1:n-j+1
            rbar = view(Wr,ir1,dll)
            v = view(Wv,ir1,dll)

The error occurs only for dl = 1.

@N5N3
Copy link
Member

N5N3 commented Mar 7, 2022

Well, that's the case above, which should be fixable.
As for your case, IIUC, axpy! is used only when dll == 1.
Call axpy!(-α[1,1], view(Wv,ir1,1), view(Wv,ir1,1)) should be enough. (Although I wonder why LinearAlgebra.axpy! not work.) It turns out LinearAlgebra.axpy! === BLAS.axpy!, my fault.

@andreasvarga
Copy link
Contributor Author

andreasvarga commented Mar 7, 2022 via email

N5N3 added a commit to N5N3/julia that referenced this issue Mar 9, 2022
N5N3 added a commit to N5N3/julia that referenced this issue Mar 9, 2022
commit d07b00e
Author: N5N3 <2642243996@qq.com>
Date:   Tue Mar 8 23:54:16 2022 +0800

    Extend `strides(::ReshapedArray)` with non-contiguous strided parent

commit 777910d
Author: N5N3 <2642243996@qq.com>
Date:   Tue Mar 8 02:00:08 2022 +0800

    Ignore 1-sized dimension during vector layout check.

    Close JuliaLang#44497
@ViralBShah ViralBShah reopened this Mar 9, 2022
@ViralBShah
Copy link
Member

Will get closed when the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants