Skip to content

Commit

Permalink
Separate out the type unstable part into stuffcol!.
Browse files Browse the repository at this point in the history
Avoid the extra convert step.
  • Loading branch information
ViralBShah committed Feb 16, 2015
1 parent 3e1d2f2 commit 11b9872
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1883,17 +1883,9 @@ function setindex!{Tv,Ti,T<:Real}(A::SparseMatrixCSC{Tv,Ti}, x, I::AbstractVecto
A
end


# Sparse concatenation

function vcat(X::SparseMatrixCSC...)
Tv = promote_type(map(x->eltype(x.nzval), X)...)
Ti = promote_type(map(x->eltype(x.rowval), X)...)

vcat(map(x->convert(SparseMatrixCSC{Tv,Ti}, x), X)...)
end

function vcat{Tv,Ti<:Integer}(X::SparseMatrixCSC{Tv,Ti}...)
num = length(X)
mX = [ size(x, 1) for x in X ]
nX = [ size(x, 2) for x in X ]
Expand All @@ -1906,6 +1898,13 @@ function vcat{Tv,Ti<:Integer}(X::SparseMatrixCSC{Tv,Ti}...)
end
end

Tv = eltype(X[1].nzval)
Ti = eltype(X[1].rowval)
for i = 2:length(X)
Tv = promote_type(Tv, eltype(X[i].nzval))
Ti = promote_type(Ti, eltype(X[i].rowval))
end

nnzX = [ nnz(x) for x in X ]
nnz_res = sum(nnzX)
colptr = Array(Ti, n + 1)
Expand All @@ -1917,18 +1916,12 @@ function vcat{Tv,Ti<:Integer}(X::SparseMatrixCSC{Tv,Ti}...)
mX_sofar = 0
ptr_res = colptr[c]
for i = 1 : num
Xi = X[i]
colptrXi = Xi.colptr
rowvalXi = Xi.rowval
nzvalXi = Xi.nzval

colptrXi = X[i].colptr
col_length = (colptrXi[c + 1] - 1) - colptrXi[c]
ptrXi = colptrXi[c]
for k=ptr_res:(ptr_res + col_length)
@inbounds rowval[k] = rowvalXi[ptrXi] + mX_sofar
@inbounds nzval[k] = nzvalXi[ptrXi]
ptrXi += 1
end
ptr_Xi = colptrXi[c]

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

ptr_res += col_length + 1
mX_sofar += mX[i]
Expand All @@ -1938,6 +1931,19 @@ function vcat{Tv,Ti<:Integer}(X::SparseMatrixCSC{Tv,Ti}...)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

@inline function stuffcol!(Xi::SparseMatrixCSC, colptr, rowval, nzval,

This comment has been minimized.

Copy link
@tkelman

tkelman Feb 16, 2015

Contributor

I'd prefer a better name

This comment has been minimized.

Copy link
@ViralBShah

ViralBShah Feb 16, 2015

Author Member

Open to suggestions - not my favourtite either. I thought for a bit, and went with this, thinking it was ok even though not ideal, since it was internal.

This comment has been minimized.

Copy link
@tkelman

tkelman Feb 16, 2015

Contributor

It's not exported, but my (any?) sparse matrix code tends to have a bunch of pieces that look like this (blkdiag below could probably be refactored to use this too). appendcol! or copycol! would be a little more descriptive

This comment has been minimized.

Copy link
@timholy

timholy Feb 16, 2015

Member

Also, I think the @inline would destroy the advantage of the function barrier---if anything you might want @noinline here. Have you tested sparse matrices of different types?

This comment has been minimized.

Copy link
@ViralBShah

ViralBShah Feb 16, 2015

Author Member

I think copycol! is an appropriate name, but the current routine is not written in a generally reusable form. I'll make it a bit more general, so that it can become an internal API and rename.

ptr_res, ptr_Xi, col_length, mX_sofar)
colptrXi = Xi.colptr
rowvalXi = Xi.rowval
nzvalXi = Xi.nzval

for k=ptr_res:(ptr_res + col_length)
@inbounds rowval[k] = rowvalXi[ptr_Xi] + mX_sofar
@inbounds nzval[k] = nzvalXi[ptr_Xi]
ptr_Xi += 1
end
end


function hcat(X::SparseMatrixCSC...)
num = length(X)
Expand Down

0 comments on commit 11b9872

Please sign in to comment.