I tried to run Example 8.8.3 from the book T. A. Driscoll, R. J. Braun Fundamentals of Numerical Computation Julia Edition. Whereas cg without preconditioner seems to work, I receive an error message when using cg with a preconditioner.
using MAT, LinearAlgebra, SparseArrays, IterativeSolvers, Preconditioners
file = matopen("A.mat");
A = read(file,"A");
close(file);
n = size(A,1);
@show n,nnz(A)
b = ones(n);
I tried to run Example 8.8.3 from the book T. A. Driscoll, R. J. Braun Fundamentals of Numerical Computation Julia Edition. Whereas cg without preconditioner seems to work, I receive an error message when using cg with a preconditioner.
using MAT, LinearAlgebra, SparseArrays, IterativeSolvers, Preconditioners
file = matopen("A.mat"); A = read(file,"A"); close(file); n = size(A,1); @show n,nnz(A) b = ones(n);
M = DiagonalPreconditioner(diag(A));
plain(b) = cg(A,b,maxiter=200,reltol=1.e-4,log=true) time_plain = @elapsed x,hist1 = plain(b) prec(b) = cg(A,b,P1=M,maxiter=200,reltol=1.e-4,log=true) time_prec = @elapsed x,hist2 = prec(b) @show time_plain, time_prec
rr = hist1[:resnorm] plot(0:length(rr),rr/rr[1],yscale=:log10,label="plain") rr = hist2[:resnorm] plot!(0:length(rr),rr/rr[1],yscale=:log10,label="preconditioned") title!("Diagonal preconditioning in CG")![2023-02-17_14-53-53](https://user-images.githubusercontent.com/17845732/219675049-d4929a60-064f-4418-abff-d1ad12c2d021.png)