Skip to content

Commit

Permalink
Merge pull request #162 from fverdugo/spmv
Browse files Browse the repository at this point in the history
Additional performance improvements in sparse matrix-vector product
  • Loading branch information
fverdugo authored Jul 26, 2024
2 parents e47b5ec + 79e395a commit 37e60ca
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.1] - 2024-07-26

### Added

- Function `spmv!`.

### Fixed

- Performance improvements in sparse matrix-vector multiplication.

## [0.5.0] - 2024-07-26

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PartitionedArrays"
uuid = "5a9dfac6-5c52-46f7-8278-5e2210713be9"
authors = ["Francesc Verdugo <f.verdugo.rojano@vu.nl> and contributors"]
version = "0.5.0"
version = "0.5.1"

[deps]
CircularArrays = "7a955b69-7140-5f4e-a0ed-f168c5e2e749"
Expand Down
2 changes: 2 additions & 0 deletions src/PartitionedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export dense_diag
export dense_diag!
export rap
export rap!
export spmv!
export spmtv!
export spmm
export spmm!
export spmtm
Expand Down
2 changes: 1 addition & 1 deletion src/p_sparse_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ function LinearAlgebra.mul!(c::PVector,a::PSparseMatrix,b::PVector)
return mul!(c,a,b,1,0)
end
t = consistent!(b)
foreach(mul!,own_values(c),own_own_values(a),own_values(b))
foreach(spmv!,own_values(c),own_own_values(a),own_values(b))
wait(t)
foreach(muladd!,own_values(c),own_ghost_values(a),ghost_values(b))
c
Expand Down
83 changes: 83 additions & 0 deletions src/sparse_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,86 @@ function csrr_to_csc_step_2(
nothing
end

@inline function spmv!(b,A,x)
mul!(b,A,x)
end

@inline function spmtv!(b,A,x)
mul!(b,transpose(A),x)
end

function spmv!(b,A::SparseMatrixCSR{1},x)
@boundscheck begin
@assert length(b) == size(A,1)
@assert length(x) == size(A,2)
end
spmv_csr!(b,x,A.rowptr,A.colval,A.nzval)
end

function spmtv!(b,A::SparseMatrixCSR{1},x)
@boundscheck begin
@assert length(b) == size(A,2)
@assert length(x) == size(A,1)
end
spmv_csc!(b,x,A.rowptr,A.colval,A.nzval)
end

function spmv!(b,A::SparseMatrixCSC,x)
@boundscheck begin
@assert length(b) == size(A,1)
@assert length(x) == size(A,2)
end
spmv_csc!(b,x,A.colptr,A.rowval,A.nzval)
end

function spmtv!(b,A::SparseMatrixCSC,x)
@boundscheck begin
@assert length(b) == size(A,2)
@assert length(x) == size(A,1)
end
spmv_csr!(b,x,A.colptr,A.rowval,A.nzval)
end

function spmv_csr!(b,x,rowptr_A,colval_A,nzval_A)
ncols = length(x)
nrows = length(b)
u = one(eltype(rowptr_A))
z = zero(eltype(b))
@inbounds for row in 1:nrows
pini = rowptr_A[row]
pend = rowptr_A[row+1]
bi = z
p = pini
while p < pend
aij = nzval_A[p]
col = colval_A[p]
xj = x[col]
bi += aij*xj
p += u
end
b[row] = bi
end
b
end

function spmv_csc!(b,x,colptr_A,rowval_A,nzval_A)
ncols = length(x)
nrows = length(b)
u = one(eltype(colptr_A))
z = zero(eltype(b))
fill!(b,z)
@inbounds for col in 1:ncols
pini = colptr_A[col]
pend = colptr_A[col+1]
p = pini
xj = x[col]
while p < pend
aij = nzval_A[p]
row = rowval_A[p]
b[row] += aij*xj
p += u
end
end
b
end

15 changes: 15 additions & 0 deletions test/sparse_utils_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ function test_mat(T)
B = compresscoo(T,I,J,V,m,n)
@test typeof(B) == T
@test A == B

b1 = ones(Tv,size(B,1))
b2 = ones(Tv,size(B,1))
x = collect(Tv,1:size(B,2))
mul!(b1,B,x)
spmv!(b2,B,x)
@test norm(b1-b2)/norm(b1) + 1 1

b1 = ones(Tv,size(B,2))
b2 = ones(Tv,size(B,2))
x = collect(Tv,1:size(B,1))
mul!(b1,transpose(B),x)
spmtv!(b2,B,x)
@test norm(b1-b2)/norm(b1) + 1 1


i,j,v = findnz(B)
for (k,(ki,kj,kv)) in enumerate(nziterator(B))
Expand Down

2 comments on commit 37e60ca

@fverdugo
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/111829

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" 37e60cae139ccc312d12acf2d059a52fd5daeece
git push origin v0.5.1

Please sign in to comment.