Skip to content

Commit

Permalink
Merge pull request #1788 from CliMA/dy/field_matrix_wrapper
Browse files Browse the repository at this point in the history
Add wrapper for FieldMatrix and FieldMatrixSolver
  • Loading branch information
Sbozzolo authored Jun 10, 2024
2 parents 25792d4 + d4ae6ef commit 9bb1a73
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 33 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ ClimaCore.jl Release Notes
main
-------

v0.14.8
-------

- ![][badge-✨feature/enhancement] Added `FieldMatrixWithSolver`, a wrapper that helps defining implicit Jacobians. PR [#1788](https://github.com/CliMA/ClimaCore.jl/pull/1788)


v0.14.6
-------

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClimaCore"
uuid = "d414da3d-4745-48bb-8d80-42e94e092884"
authors = ["CliMA Contributors <[email protected]>"]
version = "0.14.7"
version = "0.14.8"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
1 change: 1 addition & 0 deletions docs/src/matrix_fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ operator_matrix
```@docs
FieldMatrixSolverAlgorithm
FieldMatrixSolver
FieldMatrixWithSolver
field_matrix_solve!
BlockDiagonalSolve
BlockLowerTriangularSolve
Expand Down
6 changes: 4 additions & 2 deletions src/MatrixFields/MatrixFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ multiples of `LinearAlgebra.I`. This comes with the following functionality:
"""
module MatrixFields

import LinearAlgebra: I, UniformScaling, Adjoint, AdjointAbsVec, mul!, inv, norm
import LinearAlgebra: I, UniformScaling, Adjoint, AdjointAbsVec
import LinearAlgebra: inv, norm, ldiv!, mul!
import StaticArrays: SMatrix, SVector
import BandedMatrices: BandedMatrix, band, _BandedMatrix
import RecursiveArrayTools: recursive_bottom_eltype
Expand Down Expand Up @@ -73,7 +74,7 @@ export DiagonalMatrixRow,
QuaddiagonalMatrixRow,
PentadiagonalMatrixRow
export FieldVectorKeys, FieldMatrixKeys, FieldVectorView, FieldMatrix
export , FieldMatrixSolver, field_matrix_solve!
export FieldMatrixWithSolver,

# Types that are teated as single values when using matrix fields.
const SingleValue =
Expand Down Expand Up @@ -106,6 +107,7 @@ include("single_field_solver.jl")
include("multiple_field_solver.jl")
include("field_matrix_solver.jl")
include("field_matrix_iterative_solver.jl")
include("field_matrix_with_solver.jl")

function Base.show(io::IO, field::ColumnwiseBandMatrixField)
print(io, eltype(field), "-valued Field")
Expand Down
48 changes: 48 additions & 0 deletions src/MatrixFields/field_matrix_with_solver.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
FieldMatrixWithSolver(A, b, [alg])
A wrapper that combines a `FieldMatrix` `A` with a `FieldMatrixSolver` that can
be used to solve the equation `A * x = b` for `x`, where `x` and `b` are both
`FieldVector`s. Similar to a `LinearAlgebra.Factorization`, this wrapper can be
passed to `ldiv!`, whereas a regular `FieldMatrix` cannot be passed to `ldiv!`.
By default, the `FieldMatrixSolverAlgorithm` `alg` is set to a
[`BlockDiagonalSolve`](@ref), so a custom `alg` must be specified when `A` is
not a block diagonal matrix.
"""
struct FieldMatrixWithSolver{M, S} <: AbstractDict{FieldNamePair, Any}
matrix::M
solver::S
end
FieldMatrixWithSolver(
A::FieldMatrix,
b::Fields.FieldVector,
alg::FieldMatrixSolverAlgorithm = BlockDiagonalSolve(),
) = FieldMatrixWithSolver(A, FieldMatrixSolver(alg, A, b))

# TODO: Find a simple way to make b an optional argument and add a method for
# Base.one(::FieldMatrixWithSolver).

Base.keys(A::FieldMatrixWithSolver) = keys(A.matrix)

Base.values(A::FieldMatrixWithSolver) = values(A.matrix)

Base.pairs(A::FieldMatrixWithSolver) = pairs(A.matrix)

Base.length(A::FieldMatrixWithSolver) = length(A.matrix)

Base.iterate(A::FieldMatrixWithSolver, index = 1) = iterate(A.matrix, index)

Base.getindex(A::FieldMatrixWithSolver, key) = getindex(A.matrix, key)

Base.:(==)(A1::FieldMatrixWithSolver, A2::FieldMatrixWithSolver) =
A1.matrix == A2.matrix && A1.solver.alg == A2.solver.alg

Base.similar(A::FieldMatrixWithSolver) =
FieldMatrixWithSolver(similar(A.matrix), A.solver)

ldiv!(x::Fields.FieldVector, A::FieldMatrixWithSolver, b::Fields.FieldVector) =
field_matrix_solve!(A.solver, x, A.matrix, b)

mul!(b::Fields.FieldVector, A::FieldMatrixWithSolver, x::Fields.FieldVector) =
@. b = A.matrix * x
45 changes: 15 additions & 30 deletions test/MatrixFields/field_matrix_solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Revise; include(joinpath("test", "MatrixFields", "field_matrix_solvers.jl"
=#
import Logging
import Logging: Debug
import LinearAlgebra: I, norm
import LinearAlgebra: I, norm, ldiv!, mul!
import ClimaComms
import ClimaCore.Utilities: half
import ClimaCore.RecursiveApply:
Expand All @@ -14,26 +14,16 @@ import ClimaCore:

include("matrix_field_test_utils.jl")

# This broadcast must be wrapped in a function to be tested with @test_opt.
field_matrix_mul!(b, A, x) = @. b = A * x

function test_field_matrix_solver(; test_name, alg, A, b, use_rel_error = false)
@testset "$test_name" begin
x = similar(b)
b_test = similar(b)
solver = FieldMatrixSolver(alg, A, b)
args = (solver, x, A, b)

A′ = FieldMatrixWithSolver(A, b, alg)
solve_time =
@benchmark ClimaComms.@cuda_sync comms_device field_matrix_solve!(
args...,
)
@benchmark ClimaComms.@cuda_sync comms_device ldiv!(x, A′, b)

b_test = similar(b)
mul_time =
@benchmark ClimaComms.@cuda_sync comms_device field_matrix_mul!(
b_test,
A,
x,
)
@benchmark ClimaComms.@cuda_sync comms_device mul!(b_test, A′, x)

solve_time_rounded = round(solve_time; sigdigits = 2)
mul_time_rounded = round(mul_time; sigdigits = 2)
Expand Down Expand Up @@ -70,14 +60,13 @@ function test_field_matrix_solver(; test_name, alg, A, b, use_rel_error = false)
AnyFrameModule(Base.CoreLogging),
)
using_cuda ||
@test_opt ignored_modules = ignored FieldMatrixSolver(alg, A, b)
using_cuda ||
@test_opt ignored_modules = ignored field_matrix_solve!(args...)
@test_opt ignored_modules = ignored field_matrix_mul!(b, A, x)
@test_opt ignored_modules = ignored FieldMatrixWithSolver(A, b, alg)
using_cuda || @test_opt ignored_modules = ignored ldiv!(x, A′, b)
@test_opt ignored_modules = ignored mul!(b_test, A′, x)

# TODO: fix broken test when Nv is added to the type space
using_cuda || @test @allocated(field_matrix_solve!(args...)) 1536
using_cuda || @test @allocated(field_matrix_mul!(b, A, x)) == 0
using_cuda || @test @allocated(ldiv!(x, A′, b)) 1536
using_cuda || @test @allocated(mul!(b_test, A, x)) == 0
end
end

Expand Down Expand Up @@ -335,8 +324,7 @@ end
b = Fields.FieldVector(; c = ᶜvec, f = ᶠvec)

x = similar(b)
solver = FieldMatrixSolver(alg, A, b)
args = (solver, x, A, b)
A′ = FieldMatrixWithSolver(A, b, alg)

# Compare the debugging logs to RegEx strings. Note that debugging the
# spectral radius is currently not possible on GPUs.
Expand All @@ -348,9 +336,7 @@ end
(:debug, r"||x[2] - x'||₂ ≈"),
)
logs = (spectral_radius_logs..., error_norm_logs...)
@test_logs logs... min_level = Logging.Debug field_matrix_solve!(
args...,
)
@test_logs logs... min_level = Logging.Debug ldiv!(x, A′, b)
end
end
end
Expand Down Expand Up @@ -567,9 +553,8 @@ end
field = Fields.ones(space)
b = Fields.FieldVector(; _ = field)
x = similar(b)
solver =
MatrixFields.FieldMatrixSolver(MatrixFields.BlockDiagonalSolve(), A, b)
A′ = FieldMatrixWithSolver(A, b)

# Run matrix solve
MatrixFields.field_matrix_solve!(solver, x, A, b)
ldiv!(x, A, b)
end

2 comments on commit 9bb1a73

@Sbozzolo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108688

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.8 -m "<description of version>" 9bb1a735a0ed4f70b6d08b4ca35f9044e87a9393
git push origin v0.14.8

Please sign in to comment.