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

Sparse versions of repeat for matrices and vectors #532

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3908,27 +3908,24 @@ function vcat(X::AbstractSparseMatrixCSC...)
ptr_res = colptr[c]
for i = 1 : num
colptrXi = getcolptr(X[i])
col_length = (colptrXi[c + 1] - 1) - colptrXi[c]
rowvalXi = rowvals(X[i])
Copy link
Member

@SobhanMP SobhanMP Apr 20, 2024

Choose a reason for hiding this comment

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

I think at some point we agreed not to create temporary variables for this. Use rowvals(X[i]) directly as it gets optimized away by the compiler

Copy link
Contributor Author

@mjacobse mjacobse Apr 21, 2024

Choose a reason for hiding this comment

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

Addressed by b961a7f. I kept the getcolptr stuff in vcat as it was before, to avoid touching lines unnecessarily. Let me know if you disagree and would like it changed by this PR too.

Copy link
Member

Choose a reason for hiding this comment

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

since it's jus style, let's keep the pull request focused on just repeat

nzvalXi = nonzeros(X[i])
col_length = colptrXi[c + 1] - colptrXi[c]
ptr_Xi = colptrXi[c]

stuffcol!(X[i], colptr, rowval, nzval,
ptr_res, ptr_Xi, col_length, mX_sofar)
stuffcol!(rowval, nzval, ptr_res, rowvalXi, nzvalXi, ptr_Xi, col_length, mX_sofar)

ptr_res += col_length + 1
ptr_res += col_length
mX_sofar += mX[i]
end
colptr[c + 1] = ptr_res
end
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

@inline function stuffcol!(Xi::AbstractSparseMatrixCSC, colptr, rowval, nzval,
ptr_res, ptr_Xi, col_length, mX_sofar)
colptrXi = getcolptr(Xi)
rowvalXi = rowvals(Xi)
nzvalXi = nonzeros(Xi)

for k=ptr_res:(ptr_res + col_length)
@inline function stuffcol!(rowval, nzval, ptr_res, rowvalXi, nzvalXi, ptr_Xi,
SobhanMP marked this conversation as resolved.
Show resolved Hide resolved
col_length, mX_sofar)
for k=ptr_res:(ptr_res + col_length - 1)
@inbounds rowval[k] = rowvalXi[ptr_Xi] + mX_sofar
@inbounds nzval[k] = nzvalXi[ptr_Xi]
ptr_Xi += 1
Expand Down Expand Up @@ -3973,6 +3970,47 @@ function hcat(X::AbstractSparseMatrixCSC...)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end


# Efficient repetition of sparse matrices

function Base.repeat(A::AbstractSparseMatrixCSC, m)
colptr_source = getcolptr(A)
mjacobse marked this conversation as resolved.
Show resolved Hide resolved
rowval_source = rowvals(A)
nzval_source = nonzeros(A)

nnz_new = nnz(A) * m
colptr = similar(colptr_source, length(colptr_source))
rowval = similar(rowval_source, nnz_new)
nzval = similar(nzval_source, nnz_new)

colptr[1] = 1
for c = 1 : size(A, 2)
ptr_res = colptr[c]
ptr_source = colptr_source[c]
col_length = colptr_source[c + 1] - ptr_source
for index_repetition = 0 : (m - 1)
row_offset = index_repetition * size(A, 1)
stuffcol!(rowval, nzval, ptr_res, rowval_source, nzval_source, ptr_source,
mjacobse marked this conversation as resolved.
Show resolved Hide resolved
col_length, row_offset)
ptr_res += col_length
end
colptr[c + 1] = ptr_res
end
@assert colptr[end] == nnz_new + 1

SparseMatrixCSC(size(A, 1) * m, size(A, 2), colptr, rowval, nzval)
end

function Base.repeat(A::AbstractSparseMatrixCSC, m, n)
B = repeat(A, m)
nnz_per_column = diff(getcolptr(B))
colptr = cumsum(vcat(1, repeat(nnz_per_column, n)))
rowval = repeat(rowvals(B), n)
nzval = repeat(nonzeros(B), n)
SparseMatrixCSC(size(B, 1), size(B, 2) * n, colptr, rowval, nzval)
SobhanMP marked this conversation as resolved.
Show resolved Hide resolved
end


"""
blockdiag(A...)

Expand Down
32 changes: 32 additions & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,38 @@ hvcat(rows::Tuple{Vararg{Int}}, n1::Number, ns::Vararg{Number}) = invoke(hvcat,
hvcat(rows::Tuple{Vararg{Int}}, n1::N, ns::Vararg{N}) where {N<:Number} = invoke(hvcat, Tuple{typeof(rows), Vararg{N}}, rows, n1, ns...)


### Efficient repetition of sparse vectors

function Base.repeat(v::AbstractSparseVector, m)
nnz_source = nnz(v)
nnz_new = nnz_source * m

nzind_source, nzval_source = findnz(v)
mjacobse marked this conversation as resolved.
Show resolved Hide resolved
nzind = similar(nzind_source, nnz_new)
nzval = similar(nzval_source, nnz_new)

ptr_res = 1
for index_repetition = 0:(m-1)
row_offset = index_repetition * length(v)
stuffcol!(nzind, nzval, ptr_res, nzind_source, nzval_source, 1, nnz_source, row_offset)
ptr_res += nnz_source
end
@assert ptr_res == nnz_new + 1

SparseVector(length(v) * m, nzind, nzval)
end

function Base.repeat(v::AbstractSparseVector, m, n)
w = repeat(v, m)
nzind_source, nzval_source = findnz(w)

colptr = Vector{eltype(nzind_source)}(1 .+ nnz(w) * (0:n))
mjacobse marked this conversation as resolved.
Show resolved Hide resolved
rowval = repeat(nzind_source, n)
nzval = repeat(nzval_source, n)
SparseMatrixCSC(length(w), n, colptr, rowval, nzval)
end


# make sure UniformScaling objects are converted to sparse matrices for concatenation
promote_to_array_type(A::Tuple{Vararg{Union{_SparseConcatGroup,UniformScaling}}}) = anysparse(A...) ? SparseMatrixCSC : Matrix
promote_to_arrays_(n::Int, ::Type{SparseMatrixCSC}, J::UniformScaling) = sparse(J, n, n)
Expand Down
13 changes: 13 additions & 0 deletions test/sparsematrix_constructors_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ end
end
end

@testset "repeat tests" begin
A = sprand(6, 4, 0.5)
A_full = Matrix(A)
for m = 0:3
@test issparse(repeat(A, m))
@test repeat(A, m) == repeat(A_full, m)
for n = 0:3
@test issparse(repeat(A, m, n))
@test repeat(A, m, n) == repeat(A_full, m, n)
end
end
end

@testset "copyto!" begin
A = sprand(5, 5, 0.2)
B = sprand(5, 5, 0.2)
Expand Down
12 changes: 12 additions & 0 deletions test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,18 @@ end
end
end
end

@testset "repeat" begin
for m = 0:3
@test issparse(repeat(spv_x1, m))
@test repeat(spv_x1, m) == repeat(x1_full, m)
for n = 0:3
@test issparse(repeat(spv_x1, m, n))
@test repeat(spv_x1, m, n) == repeat(x1_full, m, n)
end
end
end

@testset "sparsemat: combinations with sparse matrix" begin
let S = sprand(4, 8, 0.5)
Sf = Array(S)
Expand Down
Loading