-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Deal with ILU * Refactor tests. Fix ILU
- Loading branch information
1 parent
2d1a718
commit fc34100
Showing
8 changed files
with
177 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[deps] | ||
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" | ||
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
IncompleteLU = "40713840-3770-5561-ab4c-a76e7d0d7895" | ||
Krylov = "ba0b0d4f-ebba-5204-a429-3ac8c609bfb7" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function create_unsymmetric_matrix(n) | ||
# Ensure the size is at least 2 | ||
if n < 2 | ||
throw(ArgumentError("Matrix size should be at least 2x2")) | ||
end | ||
|
||
# Generate a random n x n matrix with entries from a normal distribution | ||
A = randn(n, n) | ||
|
||
# Perform Singular Value Decomposition | ||
U, S, V = svd(A) | ||
|
||
# Modify the singular values to make them close to each other but not too small | ||
# Here we set them all to be between 1 and 2 | ||
S = Diagonal(range(1, stop=2, length=n)) | ||
|
||
# Reconstruct the matrix | ||
well_conditioned_matrix = U * S * V' | ||
|
||
return well_conditioned_matrix | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,36 @@ | ||
using Enzyme | ||
import .EnzymeRules: forward, reverse, augmented_primal | ||
using .EnzymeRules | ||
using DiffKrylov | ||
using LinearAlgebra | ||
using FiniteDifferences | ||
using Krylov | ||
using Random | ||
using SparseArrays | ||
using Test | ||
|
||
@testset "$solver" for solver = (Krylov.cg, Krylov.gmres, Krylov.bicgstab) | ||
Random.seed!(1) | ||
include("create_matrix.jl") | ||
@testset "Enzyme Rules" begin | ||
@testset "$MT" for MT = (Matrix, SparseMatrixCSC) | ||
A, b = sparse_laplacian(4, FC=Float64) | ||
A = MT(A) | ||
fdm = central_fdm(8, 1); | ||
function A_one_one(x) | ||
_A = copy(A) | ||
_A[1,1] = x | ||
solver(_A,b) | ||
@testset "($M, $N)" for (M,N) = ((I,I),) | ||
# Square unsymmetric solvers | ||
@testset "$solver" for solver = (Krylov.gmres, Krylov.bicgstab) | ||
A = [] | ||
if MT == Matrix | ||
A = create_unsymmetric_matrix(10) | ||
b = rand(10) | ||
else | ||
A, b = sparse_laplacian(4, FC=Float64) | ||
end | ||
test_enzyme_with(solver, A, b, M, N) | ||
end | ||
# Square symmetric solvers | ||
@testset "$solver" for solver = (Krylov.cg,) | ||
A, b = sparse_laplacian(4, FC=Float64) | ||
A = MT(A) | ||
test_enzyme_with(solver, A, b, M, N) | ||
end | ||
end | ||
|
||
function b_one(x) | ||
_b = copy(b) | ||
_b[1] = x | ||
solver(A,_b) | ||
end | ||
|
||
fda = FiniteDifferences.jacobian(fdm, a -> A_one_one(a)[1], copy(A[1,1])) | ||
fdb = FiniteDifferences.jacobian(fdm, a -> b_one(a)[1], copy(b[1])) | ||
fd =fda[1] + fdb[1] | ||
# Test forward | ||
function duplicate(A::SparseMatrixCSC) | ||
dA = copy(A) | ||
fill!(dA.nzval, zero(eltype(A))) | ||
return dA | ||
end | ||
duplicate(A::Matrix) = zeros(size(A)) | ||
|
||
dA = Duplicated(A, duplicate(A)) | ||
db = Duplicated(b, zeros(length(b))) | ||
dA.dval[1,1] = 1.0 | ||
db.dval[1] = 1.0 | ||
dx = Enzyme.autodiff( | ||
Forward, | ||
solver, | ||
dA, | ||
db | ||
) | ||
@test isapprox(dx[1][1], fd, atol=1e-4, rtol=1e-4) | ||
# Test reverse | ||
function driver!(x, A, b) | ||
x .= gmres(A,b)[1] | ||
nothing | ||
end | ||
dA = Duplicated(A, duplicate(A)) | ||
db = Duplicated(b, zeros(length(b))) | ||
dx = Duplicated(zeros(length(b)), zeros(length(b))) | ||
dx.dval[1] = 1.0 | ||
Enzyme.autodiff( | ||
Reverse, | ||
driver!, | ||
dx, | ||
dA, | ||
db | ||
) | ||
|
||
@test isapprox(db.dval[1], fdb[1][1], atol=1e-4, rtol=1e-4) | ||
@test isapprox(dA.dval[1,1], fda[1][1], atol=1e-4, rtol=1e-4) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters