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

Implement vector indexing for SDiagonal with statically sized indices #926

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Jun 16, 2021

On master:

julia> m = SDiagonal(@SVector [11, 12, 13, 14])
4×4 Diagonal{Int64, SVector{4, Int64}}:
 11         
    12      
       13   
          14

julia> axes(m)
(Base.OneTo(4), Base.OneTo(4))

julia> @btime $m[:, 1]
  256.280 ns (4 allocations: 256 bytes)
4-element SparseArrays.SparseVector{Int64, Int64} with 1 stored entry:
  [1]  =  11

julia> @btime $m[:,:]
  714.179 ns (5 allocations: 384 bytes)
4×4 SparseArrays.SparseMatrixCSC{Int64, Int64} with 4 stored entries:
 11         
    12      
       13   
          14

julia> @btime $m[:]
  521.792 ns (4 allocations: 256 bytes)
16-element SparseArrays.SparseVector{Int64, Int64} with 4 stored entries:
  [1 ]  =  11
  [6 ]  =  12
  [11]  =  13
  [16]  =  14

After this PR:

julia> axes(m)
(SOneTo(4), SOneTo(4))

julia> @btime $m[:,1]
  2.676 ns (0 allocations: 0 bytes)
4-element SVector{4, Int64} with indices SOneTo(4):
 11
  0
  0
  0

julia> @btime $m[:,:]
  5.316 ns (0 allocations: 0 bytes)
4×4 SMatrix{4, 4, Int64, 16} with indices SOneTo(4)×SOneTo(4):
 11   0   0   0
  0  12   0   0
  0   0  13   0
  0   0   0  14

julia> @btime $m[:]
  18.406 ns (0 allocations: 0 bytes)
16-element SVector{16, Int64} with indices SOneTo(16):
 11
  0
  0
  0
  0
 12
  0
  0
  0
  0
 13
  0
  0
  0
  0
 14

@jishnub
Copy link
Member Author

jishnub commented Jun 17, 2021

Speialized copy(::SDiagonal) in d1eb08e.

Before:

julia> m = SDiagonal(@SVector [11, 12, 13, 14])
4×4 Diagonal{Int64, SVector{4, Int64}} with indices SOneTo(4)×SOneTo(4):
 11         
    12      
       13   
          14

julia> @btime [copy($m) for i = 1:100];
  1.155 μs (101 allocations: 5.56 KiB)

After:

julia> @btime [copy($m) for i = 1:100];
  809.139 ns (1 allocation: 3.25 KiB)

@jishnub
Copy link
Member Author

jishnub commented Aug 20, 2021

Test failure on nightly seems unrelated (ambiguity in lu). Gentle bump

src/SDiagonal.jl Outdated Show resolved Hide resolved
test/SDiagonal.jl Outdated Show resolved Hide resolved
@@ -17,6 +17,10 @@ SDiagonal(a::StaticMatrix{N,N,T}) where {N,T} = Diagonal(diag(a))
size(::Type{<:SDiagonal{N}}) where {N} = (N,N)
size(::Type{<:SDiagonal{N}}, d::Int) where {N} = d > 2 ? 1 : N

Base.axes(D::SDiagonal, d) = d <= 2 ? axes(D)[d] : SOneTo(1)

Base.reshape(a::SDiagonal, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the same as the generic fallback:

@inline reshape(a::AbstractArray, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
.
Do we really need this method?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Other than this small point, the PR looks nice 👍 .

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

Successfully merging this pull request may close these issues.

2 participants