|
| 1 | +mutable struct BlockPreconditioner <: AbstractPreconditioner |
| 2 | + A::ExtendableSparseMatrix |
| 3 | + factorization |
| 4 | + phash::UInt64 |
| 5 | + partitioning::Union{Nothing,Vector{AbstractVector}} |
| 6 | + facts::Vector |
| 7 | + function BlockPreconditioner(;partitioning=nothing, factorization=ExtendableSparse.LUFactorization) |
| 8 | + p = new() |
| 9 | + p.phash = 0 |
| 10 | + p.partitioning=partitioning |
| 11 | + p.factorization=factorization |
| 12 | + p |
| 13 | + end |
| 14 | +end |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +""" |
| 19 | + BlockPreconditioner(;partitioning, factorization=LUFactorization) |
| 20 | + |
| 21 | +Create a block preconditioner from partition of unknowns given by `partitioning`, a vector of AbstractVectors describing the |
| 22 | +indices of the partitions of the matrix. For a matrix of size `n x n`, e.g. partitioning could be `[ 1:n÷2, (n÷2+1):n]` |
| 23 | +or [ 1:2:n, 2:2:n]. |
| 24 | +Factorization is a callable (Function or struct) which allows to create a factorization (with `ldiv!` methods) from a submatrix of A. |
| 25 | +""" |
| 26 | +function BlockPreconditioner end |
| 27 | + |
| 28 | +""" |
| 29 | + allow_views(::preconditioner_type) |
| 30 | +
|
| 31 | +Factorizations on matrix partitions within a block preconditioner may or may not work with array views. |
| 32 | +E.g. the umfpack factorization cannot work with views, while ILUZeroPreconditioner can. |
| 33 | +Implementing a method for `allow_views` returning `false` resp. `true` allows to dispatch to the proper case. |
| 34 | +""" |
| 35 | +allow_views(::Any)=false |
| 36 | + |
| 37 | + |
| 38 | +function update!(precon::BlockPreconditioner) |
| 39 | + flush!(precon.A) |
| 40 | + nall=sum(length,precon.partitioning) |
| 41 | + n=size(precon.A,1) |
| 42 | + if nall!=n |
| 43 | + @warn "sum(length,partitioning)=$(nall) but n=$(n)" |
| 44 | + end |
| 45 | + |
| 46 | + if isnothing(precon.partitioning) |
| 47 | + partitioning=[1:n] |
| 48 | + end |
| 49 | + |
| 50 | + np=length(precon.partitioning) |
| 51 | + precon.facts=Vector{Any}(undef,np) |
| 52 | + Threads.@threads for ipart=1:np |
| 53 | + factorization=deepcopy(precon.factorization) |
| 54 | + AP=precon.A[precon.partitioning[ipart],precon.partitioning[ipart]] |
| 55 | + FP=factorization(AP) |
| 56 | + precon.facts[ipart]=FP |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +function LinearAlgebra.ldiv!(p::BlockPreconditioner,v) |
| 64 | + partitioning=p.partitioning |
| 65 | + facts=p.facts |
| 66 | + np=length(partitioning) |
| 67 | + |
| 68 | + if allow_views(p.factorization) |
| 69 | + Threads.@threads for ipart=1:np |
| 70 | + ldiv!(facts[ipart],view(v,partitioning[ipart])) |
| 71 | + end |
| 72 | + else |
| 73 | + Threads.@threads for ipart=1:np |
| 74 | + vv=v[partitioning[ipart]] |
| 75 | + ldiv!(facts[ipart],vv) |
| 76 | + view(v,partitioning[ipart]).=vv |
| 77 | + end |
| 78 | + end |
| 79 | + v |
| 80 | +end |
| 81 | + |
| 82 | +function LinearAlgebra.ldiv!(u,p::BlockPreconditioner,v) |
| 83 | + partitioning=p.partitioning |
| 84 | + facts=p.facts |
| 85 | + np=length(partitioning) |
| 86 | + |
| 87 | + if allow_views(p.factorization) |
| 88 | + Threads.@threads for ipart=1:np |
| 89 | + ldiv!(view(u,partitioning[ipart]),facts[ipart],view(v,partitioning[ipart])) |
| 90 | + end |
| 91 | + else |
| 92 | + Threads.@threads for ipart=1:np |
| 93 | + uu=u[partitioning[ipart]] |
| 94 | + ldiv!(uu,facts[ipart],v[partitioning[ipart]]) |
| 95 | + view(u,partitioning[ipart]).=uu |
| 96 | + end |
| 97 | + end |
| 98 | + u |
| 99 | +end |
| 100 | + |
| 101 | +Base.eltype(p::BlockPreconditioner)=eltype(p.facts[1]) |
0 commit comments