Skip to content

Commit

Permalink
Add a new constructor for all Krylov solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Jan 14, 2025
1 parent 6248c2e commit 30b1a3f
Show file tree
Hide file tree
Showing 5 changed files with 573 additions and 47 deletions.
22 changes: 15 additions & 7 deletions docs/src/custom_workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ struct HaloVector{FC, D} <: AbstractVector{FC}
end
end
function HaloVector{FC,D}(::UndefInitializer, l::Int64) where {FC,D}
m = n = sqrt(l) |> Int
data = zeros(FC, m + 2, n + 2)
v = OffsetMatrix(data, 0:m + 1, 0:n + 1)
return HaloVector(v)
function Base.similar(v::HaloVector)
data = similar(v.data)
return HaloVector(data)
end
function Base.length(v::HaloVector)
Expand All @@ -103,7 +101,8 @@ function Base.getindex(v::HaloVector, idx)
end
```

The `size` and `getindex` functions support REPL display, aiding interaction, though they are optional for Krylov.jl’s functionality.
The functions `similar` and `length` are mandatory and must be implemented for custom vector types.
The functions `size` and `getindex` support REPL display, aiding interaction, though they are optional for Krylov.jl’s functionality.

### Efficient stencil implementation

Expand Down Expand Up @@ -282,8 +281,14 @@ for i in 1:Nx
end
b = HaloVector(data)
# Allocate the workspace
kc = KrylovConstructor(b)
solver = CgSolver(kc)
# Solve the system with CG
u_sol, stats = Krylov.cg(A, b, atol=1e-12, rtol=0.0, verbose=1)
Krylov.cg!(solver, A, b, atol=1e-12, rtol=0.0, verbose=1)
u_sol = solution(solver)
stats = statistics(solver)
```

```@example halo-regions
Expand All @@ -292,6 +297,9 @@ u_star = [sin(π * i * Δx) * sin(π * j * Δy) for i=1:Nx, j=1:Ny]
norm(u_sol.data[1:Nx, 1:Ny] - u_star, Inf)
```

!!! note
Only the in-place version of the Krylov methods is supported for custom vector types.

### Conclusion

Implementing a 2D Poisson equation solver with `HaloVector` improves code clarity and efficiency.
Expand Down
2 changes: 2 additions & 0 deletions src/block_krylov_solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The outer constructors
solver = BlockMinresSolver(m, n, p, SV, SM)
solver = BlockMinresSolver(A, B)
solver = BlockMinresSolver(kc::BlockKrylovConstructor)
may be used in order to create these vectors.
`memory` is set to `div(n,p)` if the value given is larger than `div(n,p)`.
Expand Down Expand Up @@ -82,6 +83,7 @@ The outer constructors
solver = BlockGmresSolver(m, n, p, memory, SV, SM)
solver = BlockGmresSolver(A, B, memory = 5)
solver = BlockGmresSolver(kc::BlockKrylovConstructor, memory = 5)
may be used in order to create these vectors.
`memory` is set to `div(n,p)` if the value given is larger than `div(n,p)`.
Expand Down
Loading

0 comments on commit 30b1a3f

Please sign in to comment.