-
Notifications
You must be signed in to change notification settings - Fork 92
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
SparseMatrixCSR extension #864
Open
termi-official
wants to merge
30
commits into
master
Choose a base branch
from
do/sparsematrixcsr-assembly-extension
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
7edd47a
Some refactoring and trying to get the extension working.
termi-official 7c8e7b1
Almost. Imhomogeneities are broken.
termi-official a29dbb0
revert faulty refactoring.
termi-official d8589ee
Refactor SpMSpV kernels and add tests
termi-official 26c0f58
Revert heat example.
termi-official 056f5c3
Devdocs.
termi-official f021d01
Symmetric assembler.
termi-official 46f6428
Derp
termi-official 4a47a3f
Revert constraints test battery.
termi-official d227ef7
Add sparsity pattern generator for CSR.
termi-official 0c3efc8
Derp
termi-official a2160e0
Convenience function for sparsity pattern.
termi-official 8102774
Test coverage for sparsity pattern, assembly and Dirichlet elimination.
termi-official cfabf75
Devdocs for add_inhomogeneities
termi-official 0328f24
Add Knut's suggestion.
termi-official d20389a
Merge master
termi-official ed365f1
Trim ws
termi-official 29888f6
Trim ws again
termi-official 60bf45f
Update to new API
termi-official 0b5a861
[skip ci] Comments.
termi-official d653475
Merge master
termi-official 896fee1
Try to fix docs CI
termi-official 3976701
Add matrix alloc to ext
termi-official 43b755f
Docs
termi-official debdf9c
Merge master.
termi-official d7f2cc5
Runic
termi-official 512eaac
Docs oopsie
termi-official cdc828e
Make CodeCov happy.
termi-official 6f80dfd
More happy.
termi-official 0f42594
Derp
termi-official File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
module FerriteSparseMatrixCSR | ||
|
||
using Ferrite, SparseArrays, SparseMatricesCSR | ||
import Ferrite: AbstractSparsityPattern, CSRAssembler | ||
import Base: @propagate_inbounds | ||
|
||
#FIXME https://github.com/JuliaSparse/SparseArrays.jl/pull/546 | ||
function Ferrite.start_assemble(K::SparseMatrixCSR{<:Any,T}, f::Vector=T[]; fillzero::Bool=true, maxcelldofs_hint::Int=0) where {T} | ||
fillzero && (Ferrite.fillzero!(K); Ferrite.fillzero!(f)) | ||
return CSRAssembler(K, f, zeros(Int,maxcelldofs_hint), zeros(Int,maxcelldofs_hint)) | ||
end | ||
|
||
@propagate_inbounds function Ferrite._assemble_inner!(K::SparseMatrixCSR, Ke::AbstractMatrix, dofs::AbstractVector, sorteddofs::AbstractVector, permutation::AbstractVector, sym::Bool) | ||
current_row = 1 | ||
ld = length(dofs) | ||
@inbounds for Krow in sorteddofs | ||
maxlookups = sym ? current_row : ld | ||
Kerow = permutation[current_row] | ||
ci = 1 # col index pointer for the local matrix | ||
Ci = 1 # col index pointer for the global matrix | ||
nzr = nzrange(K, Krow) | ||
while Ci <= length(nzr) && ci <= maxlookups | ||
C = nzr[Ci] | ||
Kcol = K.colval[C] | ||
Kecol = permutation[ci] | ||
val = Ke[Kerow, Kecol] | ||
if Kcol == dofs[Kecol] | ||
# Match: add the value (if non-zero) and advance the pointers | ||
if !iszero(val) | ||
K.nzval[C] += val | ||
end | ||
ci += 1 | ||
Ci += 1 | ||
elseif Kcol < dofs[Kecol] | ||
# No match yet: advance the global matrix row pointer | ||
Ci += 1 | ||
else # Kcol > dofs[Kecol] | ||
# No match: no entry exist in the global matrix for this row. This is | ||
# allowed as long as the value which would have been inserted is zero. | ||
iszero(val) || Ferrite._missing_sparsity_pattern_error(Krow, Kcol) | ||
# Advance the local matrix row pointer | ||
ci += 1 | ||
end | ||
end | ||
# Make sure that remaining entries in this column of the local matrix are all zero | ||
for i in ci:maxlookups | ||
if !iszero(Ke[Kerow, permutation[i]]) | ||
Ferrite._missing_sparsity_pattern_error(Krow, sorteddofs[i]) | ||
end | ||
end | ||
current_row += 1 | ||
end | ||
end | ||
|
||
function Ferrite.zero_out_rows!(K::SparseMatrixCSR, ch::ConstraintHandler) # can be removed in 0.7 with #24711 merged | ||
@debug @assert issorted(ch.prescribed_dofs) | ||
for row in ch.prescribed_dofs | ||
r = nzrange(K, row) | ||
K.nzval[r] .= 0.0 | ||
end | ||
end | ||
|
||
function Ferrite.zero_out_columns!(K::SparseMatrixCSR, ch::ConstraintHandler) | ||
colval = K.colval | ||
nzval = K.nzval | ||
@inbounds for i in eachindex(colval, nzval) | ||
if haskey(ch.dofmapping, colval[i]) | ||
nzval[i] = 0 | ||
end | ||
end | ||
end | ||
|
||
function Ferrite.allocate_matrix(::Type{SparseMatrixCSR}, sp::AbstractSparsityPattern) | ||
_allocate_matrix(SparseMatrixCSR{1, Float64, Int64}, sp, false) | ||
end | ||
|
||
function Ferrite.allocate_matrix(::Type{SparseMatrixCSR{1, Tv, Ti}}, sp::AbstractSparsityPattern) where {Tv, Ti} | ||
_allocate_matrix(SparseMatrixCSR{1, Tv, Ti}, sp, false) | ||
end | ||
|
||
function _allocate_matrix(::Type{SparseMatrixCSR{1, Tv, Ti}}, sp::AbstractSparsityPattern, sym::Bool) where {Tv, Ti} | ||
# 1. Setup rowptr | ||
rowptr = zeros(Ti, Ferrite.getnrows(sp) + 1) | ||
rowptr[1] = 1 | ||
for (row, colidxs) in enumerate(Ferrite.eachrow(sp)) | ||
for col in colidxs | ||
sym && row > col && continue | ||
rowptr[row+1] += 1 | ||
end | ||
end | ||
cumsum!(rowptr, rowptr) | ||
nnz = rowptr[end] - 1 | ||
# 2. Allocate colval and nzval now that nnz is known | ||
colval = Vector{Ti}(undef, nnz) | ||
nzval = zeros(Tv, nnz) | ||
# 3. Populate colval. | ||
k = 1 | ||
for (row, colidxs) in zip(1:Ferrite.getnrows(sp), Ferrite.eachrow(sp)) # pairs(eachrow(sp)) | ||
for col in colidxs | ||
sym && row > col && continue | ||
colval[k] = col | ||
k += 1 | ||
end | ||
end | ||
S = SparseMatrixCSR{1}(Ferrite.getnrows(sp), Ferrite.getncols(sp), rowptr, colval, nzval) | ||
return S | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the correct way to communicate this? Adding
@docs
breaks the CI, because the doc string is already listed in references.