Skip to content

Commit 3ae81dc

Browse files
authored
Merge pull request #116 from j-fu/LinearSolvePrecs
Provide interface to the precs API of LinearSolve
2 parents 84e4a6a + 2000306 commit 3ae81dc

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ p = aspreconditioner(ml)
5656
c = cg(A, A*ones(1000), Pl = p)
5757
```
5858

59+
60+
### As a preconditioner with LinearSolve.jl
61+
62+
`RugeStubenPreconBuilder` and `SmoothedAggregationPreconBuilder` work with the
63+
[`precs` API](https://docs.sciml.ai/LinearSolve/stable/basics/Preconditioners/#Specifying-Preconditioners)
64+
of LinearSolve.jl
65+
66+
```julia
67+
A = poisson( (100,100) )
68+
u0= rand(size(A,1))
69+
b=A*u0
70+
71+
prob = LinearProblem(A, b)
72+
strategy = KrylovJL_CG(precs = RugeStubenPreconBuilder())
73+
sol = solve(prob, strategy, atol=1.0e-14)
74+
75+
strategy = KrylovJL_CG(precs = SmoothedAggregationPreconBuilder())
76+
sol = solve(prob, strategy, atol=1.0e-14)
77+
```
78+
5979
## Features and Roadmap
6080

6181
This package currently supports:

src/AlgebraicMultigrid.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ module AlgebraicMultigrid
33
using Reexport
44
using LinearAlgebra
55
using LinearSolve
6-
using SparseArrays, Printf
6+
using SparseArrays
7+
using SparseArrays: AbstractSparseMatrixCSC
8+
using Printf
79
@reexport import CommonSolve: solve, solve!, init
810
using Reexport
911

@@ -40,4 +42,7 @@ export fit_candidates, smoothed_aggregation
4042
include("preconditioner.jl")
4143
export aspreconditioner
4244

45+
include("precs.jl")
46+
export SmoothedAggregationPreconBuilder, RugeStubenPreconBuilder
47+
4348
end # module

src/precs.jl

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
SmoothedAggregationPreconBuilder(;blocksize=1, kwargs...)
3+
4+
Return callable object constructing a left smoothed aggregation algebraic multigrid preconditioner
5+
to be used with the `precs` API of LinearSolve.
6+
"""
7+
struct SmoothedAggregationPreconBuilder{Tk}
8+
blocksize::Int
9+
kwargs::Tk
10+
end
11+
12+
function SmoothedAggregationPreconBuilder(; blocksize = 1, kwargs...)
13+
return SmoothedAggregationPreconBuilder(blocksize, kwargs)
14+
end
15+
16+
function (b::SmoothedAggregationPreconBuilder)(A::AbstractSparseMatrixCSC, p)
17+
return (aspreconditioner(smoothed_aggregation(SparseMatrixCSC(A), Val{b.blocksize}; b.kwargs...)), I)
18+
end
19+
20+
21+
"""
22+
RugeStubenPreconBuilder(;blocksize=1, kwargs...)
23+
24+
Return callable object constructing a left algebraic multigrid preconditioner after Ruge & Stüben
25+
to be used with the `precs` API of LinearSolve.
26+
"""
27+
struct RugeStubenPreconBuilder{Tk}
28+
blocksize::Int
29+
kwargs::Tk
30+
end
31+
32+
function RugeStubenPreconBuilder(; blocksize = 1, kwargs...)
33+
return RugeStubenPreconBuilder(blocksize, kwargs)
34+
end
35+
36+
function (b::RugeStubenPreconBuilder)(A::AbstractSparseMatrixCSC, p)
37+
return (aspreconditioner(ruge_stuben(SparseMatrixCSC(A), Val{b.blocksize}; b.kwargs...)), I)
38+
end

test/runtests.jl

+24-1
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,27 @@ end
330330
X = poisson(27_000)+24.0*I
331331
ml = ruge_stuben(X)
332332
b = rand(27_000)
333-
@test AlgebraicMultigrid._solve(ml, b, reltol = 1e-10) X \ b rtol = 1e-10
333+
@test AlgebraicMultigrid._solve(ml, b, reltol = 1e-10) X \ b rtol = 1e-10
334+
335+
336+
337+
338+
# LinearSolve precs interface
339+
@testset "LinearSolvePrecs" begin
340+
341+
for sz in [ (10,10), (20,20), (50,50)]
342+
A = poisson(sz)
343+
u0= ones(size(A,1))
344+
b=A*u0
345+
346+
prob = LinearProblem(A, b)
347+
strategy = KrylovJL_CG(precs = RugeStubenPreconBuilder())
348+
@test solve(prob, strategy, atol=1.0e-14) u0 rtol = 1.0e-8
349+
350+
351+
strategy = KrylovJL_CG(precs = SmoothedAggregationPreconBuilder())
352+
@test solve(prob, strategy, atol=1.0e-14) u0 rtol = 1.0e-8
353+
354+
end
355+
356+
end

0 commit comments

Comments
 (0)