Skip to content

Commit e0b9684

Browse files
committed
allow preallocating x
1 parent 4d3929f commit e0b9684

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/multilevel.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ function MultiLevelWorkspace(::Type{Val{bs}}, ::Type{T}) where {bs, T<:Number}
2626
end
2727
MultiLevelWorkspace{TX, bs}(TX[], TX[], TX[])
2828
end
29+
Base.eltype(w::MultiLevelWorkspace{TX}) where TX = eltype(TX)
30+
blocksize(w::MultiLevelWorkspace{TX, bs}) where {TX, bs} = bs
31+
2932
function residual!(m::MultiLevelWorkspace{TX, bs}, n) where {TX, bs}
3033
if bs === 1
3134
push!(m.res_vecs, TX(undef, n))
@@ -129,16 +132,22 @@ Keyword Arguments
129132
* log::Bool - return vector of residuals along with solution
130133
131134
"""
132-
function solve(ml::MultiLevel, b::AbstractVector{T},
135+
function solve(ml::MultiLevel, b::AbstractVector, args...; kwargs...)
136+
n = length(ml) == 1 ? size(ml.final_A, 1) : size(ml.levels[1].A, 1)
137+
V = promote_type(eltype(ml.workspace), eltype(b))
138+
bs = blocksize(ml.workspace)
139+
x = bs === 1 ? zeros(V, n) : zeros(V, n, bs)
140+
return solve!(x, ml, b, args...; kwargs...)
141+
end
142+
function solve!(x, ml::MultiLevel, b::AbstractVector{T},
133143
cycle::Cycle = V();
134144
maxiter::Int = 100,
135145
tol::Float64 = 1e-5,
136146
verbose::Bool = false,
137147
log::Bool = false) where {T}
138148

139-
A = length(ml) == 1 ? ml.final_A : ml.levels[1].A
149+
A = length(ml) == 1 ? ml.final_A : ml.levels[1].A
140150
V = promote_type(eltype(A), eltype(b))
141-
x = zeros(V, size(b))
142151
tol = eltype(b)(tol)
143152
log && (residuals = Vector{V}())
144153
normres = normb = norm(b)

0 commit comments

Comments
 (0)