Skip to content

Commit b31b1ef

Browse files
committed
Use commonsolve interface
1 parent b40945a commit b31b1ef

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ uuid = "2169fc97-5a83-5252-b627-83903c6c433c"
33
version = "0.4.2"
44

55
[deps]
6+
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
67
CompatHelper = "aa819f21-2bde-4658-8897-bab36330d9b7"
78
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
89
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
910
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1011
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1112
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
13+
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1214
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1315
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1416

src/AlgebraicMultigrid.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
module AlgebraicMultigrid
22

3+
using Reexport
34
import IterativeSolvers: gauss_seidel!
45
using LinearAlgebra
56
using SparseArrays, Printf
67
using Base.Threads
8+
@reexport import CommonSolve: solve, solve!, init
9+
using Reexport
710

811
using LinearAlgebra: rmul!
912

@@ -29,7 +32,7 @@ export GaussSeidel, SymmetricSweep, ForwardSweep, BackwardSweep,
2932
JacobiProlongation
3033

3134
include("multilevel.jl")
32-
export solve
35+
export RugeStubenAMG, SmoothedAggregationAMG
3336

3437
include("classical.jl")
3538
export ruge_stuben

src/multilevel.jl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct F <: Cycle
126126
end
127127

128128
"""
129-
solve(ml::MultiLevel, b::AbstractArray, cycle, kwargs...)
129+
_solve(ml::MultiLevel, b::AbstractArray, cycle, kwargs...)
130130
131131
Execute multigrid cycling.
132132
@@ -145,13 +145,13 @@ Keyword Arguments
145145
* log::Bool - return vector of residuals along with solution
146146
147147
"""
148-
function solve(ml::MultiLevel, b::AbstractArray, args...; kwargs...)
148+
function _solve(ml::MultiLevel, b::AbstractArray, args...; kwargs...)
149149
n = length(ml) == 1 ? size(ml.final_A, 1) : size(ml.levels[1].A, 1)
150150
V = promote_type(eltype(ml.workspace), eltype(b))
151151
x = zeros(V, size(b))
152-
return solve!(x, ml, b, args...; kwargs...)
152+
return _solve!(x, ml, b, args...; kwargs...)
153153
end
154-
function solve!(x, ml::MultiLevel, b::AbstractArray{T},
154+
function _solve!(x, ml::MultiLevel, b::AbstractArray{T},
155155
cycle::Cycle = V();
156156
maxiter::Int = 100,
157157
abstol::Real = zero(real(eltype(b))),
@@ -233,3 +233,29 @@ function __solve!(x, ml, cycle::Cycle, b, lvl)
233233

234234
x
235235
end
236+
237+
238+
### CommonSolve.jl spec
239+
struct AMGSolver{T}
240+
ml::MultiLevel
241+
b::Vector{T}
242+
end
243+
244+
abstract type AMGAlg end
245+
246+
struct RugeStubenAMG <: AMGAlg end
247+
struct SmoothedAggregationAMG <: AMGAlg end
248+
249+
function solve(A::AbstractMatrix, b::Vector, s::AMGAlg, args...; kwargs...)
250+
solt = init(s, A, b, args...; kwargs...)
251+
solve!(solt, args...; kwargs...)
252+
end
253+
function init(::RugeStubenAMG, A, b, args...; kwargs...)
254+
AMGSolver(ruge_stuben(A; kwargs...), b)
255+
end
256+
function init(::SmoothedAggregationAMG, A, b; kwargs...)
257+
AMGSolver(smoothed_aggregation(A; kwargs...), b)
258+
end
259+
function solve!(solt::AMGSolver, args...; kwargs...)
260+
_solve(solt.ml, solt.b, args...; kwargs...)
261+
end

test/cycle_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function test_cycles()
1414
ml = method(A)
1515

1616
for cycle in [AlgebraicMultigrid.V(),AlgebraicMultigrid.W(),AlgebraicMultigrid.F()]
17-
x,convhist = solve(ml, b, cycle; reltol = reltol, log = true)
17+
x,convhist = AlgebraicMultigrid._solve(ml, b, cycle; reltol = reltol, log = true)
1818

1919
@debug "number of iterations for $cycle using $method: $(length(convhist))"
2020
@test norm(b - A*x) < reltol * norm(b)

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,4 @@ end
327327
X = poisson(27_000)+24.0*I
328328
ml = ruge_stuben(X)
329329
b = rand(27_000)
330-
@test solve(ml, b, reltol = 1e-10) X \ b rtol = 1e-10
330+
@test AlgebraicMultigrid._solve(ml, b, reltol = 1e-10) X \ b rtol = 1e-10

0 commit comments

Comments
 (0)