-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeremy E Kozdon
committed
Aug 4, 2021
1 parent
969b287
commit 1bd216c
Showing
6 changed files
with
155 additions
and
75 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
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,76 @@ | ||
using Test | ||
using MPI | ||
MPI.Initialized() || MPI.Init() | ||
using PETSc | ||
using LinearAlgebra: mul! | ||
|
||
@testset "KSP" begin | ||
comm = MPI.COMM_WORLD | ||
mpisize = MPI.Comm_size(comm) | ||
mpirank = MPI.Comm_rank(comm) | ||
|
||
for petsclib in PETSc.petsclibs | ||
PETSc.initialize(petsclib) | ||
PetscScalar = petsclib.PetscScalar | ||
PetscInt = petsclib.PetscInt | ||
|
||
loc_num_rows = 10 | ||
loc_num_cols = 10 | ||
diag_nonzeros = 3 | ||
off_diag_non_zeros = 3 | ||
|
||
A = PETSc.MatAIJ( | ||
petsclib, | ||
comm, | ||
loc_num_rows, | ||
loc_num_cols, | ||
diag_nonzeros, | ||
off_diag_non_zeros, | ||
) | ||
|
||
# Get compatible vectors | ||
(x, b) = PETSc.createvecs(A) | ||
|
||
row_rng = PETSc.ownershiprange(A, false) | ||
for i in row_rng | ||
if i == 0 | ||
vals = [-2, 1] | ||
row0idxs = [i] | ||
col0idxs = [i, i + 1] | ||
elseif i == mpisize * loc_num_rows - 1 | ||
vals = [-2, 1] | ||
row0idxs = [i] | ||
col0idxs = [i, i - 1] | ||
else | ||
vals = [1, -2, 1] | ||
row0idxs = [i] | ||
col0idxs = [i - 1, i, i + 1] | ||
end | ||
PETSc.setvalues!( | ||
A, | ||
PetscInt.(row0idxs), | ||
PetscInt.(col0idxs), | ||
PetscScalar.(vals), | ||
) | ||
x[i + 1] = (i + 1)^3 | ||
end | ||
PETSc.assemble!(A) | ||
PETSc.assemble!(x) | ||
|
||
mul!(b, A, x) | ||
y = similar(x) | ||
|
||
ksp = PETSc.KSP(A; ksp_rtol = 1e-16, pc_type = "jacobi") | ||
PETSc.solve!(y, ksp, b) | ||
PETSc.withlocalarray!(x, y) do x, y | ||
@test x ≈ y | ||
end | ||
|
||
# PETSc.destroy(ksp) | ||
PETSc.destroy(A) | ||
PETSc.destroy(x) | ||
PETSc.destroy(y) | ||
PETSc.destroy(b) | ||
PETSc.finalize(petsclib) | ||
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