# [petsc-users] Question about residue norm in PETSc

Matthew Knepley knepley at gmail.com
Sun Jul 2 08:20:41 CDT 2023

```On Sun, Jul 2, 2023 at 8:45 AM 王赫萌 <wanghemeng at 163.com> wrote:

> Thank so much for your patience! I'm really grateful for that!
>
> Could you explain the calculation of "1.31278e+06"
> It appears that my calculation of this value is "18.0468"
>

100 KSP unpreconditioned resid norm 1.312782529439e+06 true resid norm
1.312782529439e+06 ||r(i)||/||b|| 1.804684612214e+01
norm_delta 1312782.529439, norm_b 72743.044439
Norm of error 18.0468 iterations 100
Residue of error 1.31278e+06 iterations 100

Here norm_delta matches true resid norm, because they are both A x - b.
What you are calling
"Norm of error" is actually the "Relative Residual" ||r_100|| / ||b||
= 18.0468, which is correct since
that is 1312782.529439 / 72743.044439. What you call "Residue of error" is
the "Residual" just
as reported in the KSP monitor output.

Thanks,

Matt

> Please forgive my ignorance. I just begin my learning on iterative method.
>
> Thank you so much for the reply!
>
> Welcome to China if you have time! ^_^
>
>
> <https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=%E7%8E%8B%E8%B5%AB%E8%90%8C&uid=wanghemeng%40163.com&iconUrl=https%3A%2F%2Fmail-online.nosdn.127.net%2Fqiyelogo%2FdefaultAvatar.png&items=%5B%22wanghemeng%40163.com%22%5D>
> ---- Replied Message ----
> From Matthew Knepley<knepley at gmail.com> <knepley at gmail.com>
> Date 7/2/2023 20:32
> To 王赫萌<wanghemeng at 163.com> <wanghemeng at 163.com>
> Cc PETSc<petsc-users at mcs.anl.gov> <petsc-users at mcs.anl.gov>
> Subject Re: [petsc-users] Question about residue norm in PETSc
> On Sun, Jul 2, 2023 at 8:19 AM 王赫萌 <wanghemeng at 163.com> wrote:
>
>> Here is the mat and rhs used in code! (May need to change the data path)
>>
>> mat:
>>
>> https://studentcupeducn-my.sharepoint.com/:u:/g/personal/wanghemeng_student_cup_edu_cn/Ed76oGtC1ttDriZsObbPR74BCnDPUP8aicVXQEL4sO1AyQ?e=zeszik
>> rhs:
>>
>> https://studentcupeducn-my.sharepoint.com/:u:/g/personal/wanghemeng_student_cup_edu_cn/EdHRqWbzVmtIkAppOLL1UMIBM7tK7ws0gEASESGHuGC3yw?e=SMQSmY
>>
>> I tried using
>> PetscCall(VecAXPY(u, -1.0, b));
>> but is just as same as
>> PetscCall(VecAXPY(b, -1.0, u));
>>
>> Thank you so much for that!!!
>>
>
>  90 KSP unpreconditioned resid norm 9.749157899195e+05 true resid norm
> 9.749157899195e+05 ||r(i)||/||b|| 1.340218569960e+01
>  91 KSP unpreconditioned resid norm 1.073123446417e+06 true resid norm
> 1.073123446417e+06 ||r(i)||/||b|| 1.475224820050e+01
>  92 KSP unpreconditioned resid norm 1.170251286554e+06 true resid norm
> 1.170251286554e+06 ||r(i)||/||b|| 1.608746644557e+01
>  93 KSP unpreconditioned resid norm 1.264719067990e+06 true resid norm
> 1.264719067990e+06 ||r(i)||/||b|| 1.738611681365e+01
>  94 KSP unpreconditioned resid norm 1.329446257320e+06 true resid norm
> 1.329446257320e+06 ||r(i)||/||b|| 1.827592270272e+01
>  95 KSP unpreconditioned resid norm 1.365944956372e+06 true resid norm
> 1.365944956372e+06 ||r(i)||/||b|| 1.877767100504e+01
>  96 KSP unpreconditioned resid norm 1.369513563400e+06 true resid norm
> 1.369513563400e+06 ||r(i)||/||b|| 1.882672871297e+01
>  97 KSP unpreconditioned resid norm 1.364905651654e+06 true resid norm
> 1.364905651654e+06 ||r(i)||/||b|| 1.876338366353e+01
>  98 KSP unpreconditioned resid norm 1.352584030803e+06 true resid norm
> 1.352584030803e+06 ||r(i)||/||b|| 1.859399810996e+01
>  99 KSP unpreconditioned resid norm 1.330589478009e+06 true resid norm
> 1.330589478009e+06 ||r(i)||/||b|| 1.829163857903e+01
> 100 KSP unpreconditioned resid norm 1.312782529439e+06 true resid norm
> 1.312782529439e+06 ||r(i)||/||b|| 1.804684612214e+01
> Linear solve did not converge due to DIVERGED_ITS iterations 100
> KSPSolve Time: 7579.364000 ms
> norm_delta 1312782.529439, norm_b 72743.044439
> Norm of error 18.0468 iterations 100
> Residue of error 1.31278e+06 iterations 100
>
> I ran with
>
>   -pc_type jacobi -ksp_max_it 100
>
> because GAMG takes a long time to setup on my laptop. Those numbers match
> exactly.
>
>    THanks,
>
>      Matt
>
> I'm such a beginner T_T
>> ---- Replied Message ----
>> From Matthew Knepley<knepley at gmail.com> <knepley at gmail.com>
>> Date 7/2/2023 20:10
>> To 王赫萌<wanghemeng at 163.com> <wanghemeng at 163.com>
>> Cc PETSc<petsc-users at mcs.anl.gov> <petsc-users at mcs.anl.gov>
>> Subject Re: [petsc-users] Question about residue norm in PETSc
>> On Sun, Jul 2, 2023 at 8:05 AM Matthew Knepley <knepley at gmail.com> wrote:
>>
>>> On Sun, Jul 2, 2023 at 7:53 AM 王赫萌 <wanghemeng at 163.com> wrote:
>>>
>>>> So sorry that I made a mistake in the description.
>>>> I set the tolerances by:
>>>> PetscCall(KSPSetTolerances(ksp, 1e-12, DBL_MIN, PETSC_DEFAULT,
>>>> PETSC_DEFAULT));
>>>> and got (by passing `-ksp_norm_type unpreconditioned
>>>> -ksp_monitor_true_residual`)
>>>> 74 KSP unpreconditioned resid norm 7.256655641876e-08 true resid norm
>>>> 7.256655641876e-08 ||r(i)||/||b|| 9.975738158726e-13
>>>> I'm wondering why the ` ||r(i)||/||b||` is different with mine which
>>>> calculated by:
>>>> ```
>>>>   PetscCall(VecNorm(b, NORM_2, &norm_b)); // (main.c, line 74) (which
>>>> is 72743.044439)
>>>>   PetscCall(VecDuplicate(b, &u)); // (main.c, line 105)
>>>>   PetscCall(MatMult(A, x, u));
>>>>   PetscCall(VecAXPY(b, -1.0, u));
>>>>   PetscCall(VecNorm(b, NORM_2, &norm_delta)); // (which is 0.039608)
>>>> ```
>>>> and (norm_delta) / (norm_b) = 5.44496e-07 which is higher and
>>>> different with the rtol I set (1e-12).
>>>> Sorry again for the waste of your time. I would really appreciated if
>>>> you could help me again!
>>>>
>>>
>>> 1) 7.256655641876e-08 / 72743.044439 = 9.975738158726e-13 so
>>> ||r_i||/||b|| is correct in the output
>>>
>>> 2) You are asking why you calculate a different residual? I will have to
>>>
>>
>> I built your code, but you did not send the matrix and rhs.
>>
>> I suggest using
>>
>> PetscCall(VecAXPY(u, -1.0, b));
>>
>> instead so that you do not change b, and keep the residual in u.
>>
>>   Thanks,
>>
>>      Matt
>>
>>   Thanks,
>>
>>      Matt
>>
>>
>>>   Thanks,
>>>
>>>      Matt
>>>
>>>
>>>> Best regards!
>>>> Hemeng Wang
>>>>
>>>>
>>>> ---- Replied Message ----
>>>> From Matthew Knepley<knepley at gmail.com> <knepley at gmail.com>
>>>> Date 7/2/2023 18:51
>>>> To 王赫萌<wanghemeng at 163.com> <wanghemeng at 163.com>
>>>> Cc petsc-users at mcs.anl.gov<petsc-users at mcs.anl.gov>
>>>> <petsc-users at mcs.anl.gov>
>>>> Subject Re: [petsc-users] Question about residue norm in PETSc
>>>> On Sun, Jul 2, 2023 at 2:24 AM 王赫萌 <wanghemeng at 163.com> wrote:
>>>>
>>>>> Dear PETSc Team,
>>>>>
>>>>> Sorry to bother! My name is Hemeng Wang, and I am currently learning
>>>>> the use of PETSc software package. I am confused while calculating the norm
>>>>> of residue.
>>>>>
>>>>> I calculated residue norm by myself with:
>>>>> ```
>>>>>   PetscCall(VecNorm(b, NORM_2, &norm_b)); // (main.c, line 74)
>>>>>
>>>>>   PetscCall(VecDuplicate(b, &u)); // (main.c, line 105)
>>>>>   PetscCall(MatMult(A, x, u));
>>>>>   PetscCall(VecAXPY(b, -1.0, u));
>>>>>   PetscCall(VecNorm(b, NORM_2, &norm_delta));
>>>>> ```
>>>>> and check the (norm_delta) / (norm_b). It seems not the `atol` which
>>>>> set by `KSPSetTolerances()`.
>>>>> (I set atol as 1e-12, but got 5e-7 finally)
>>>>> (options:  -ksp_type cg -pc_type gamg -ksp_converged_reason
>>>>> -ksp_norm_type unpreconditioned -ksp_monitor_true_residual)
>>>>>
>>>>
>>>> If you are using the default convergence test, there is an absolute
>>>> tolerance (atol) _and_ a relative tolerance (rtol). It seems likely you hit
>>>> the relative tolerance. You can check this using
>>>>
>>>>   -ksp_converged_reason
>>>>
>>>> You could make rtol really small if you want to just see the atol
>>>>
>>>>   -ksp_rtol 1e-20
>>>>
>>>>   Thanks,
>>>>
>>>>      Matt
>>>>
>>>>
>>>>> I also check the soure code of `KSPSolve_CG` in
>>>>> `petsc/src/ksp/ksp/impls/cg/cg.c`. And could not figure out where is the
>>>>> difference.
>>>>>
>>>>> I will really really appreciated if someone can explain the
>>>>> calculation of `resid norm` in petsc. And where is my mistake.
>>>>>
>>>>> To provide you with more context, here are the source code about my
>>>>> implementation. And the output of my test.
>>>>>
>>>>> main.c
>>>>> Main code of my program
>>>>>
>>>>>
>>>>> Makefile
>>>>> For compiling, same as sample provided
>>>>>
>>>>> A script for running program in `slurm`
>>>>>
>>>>> slurm-4803840.out
>>>>> Output of my test
>>>>>
>>>>> Thank you very much for your time and attention. I greatly appreciate
>>>>> your support and look forward to hearing from you soon.
>>>>> Best regards,
>>>>> Hemeng Wang
>>>>>
>>>>>
>>>>
>>>> --
>>>> What most experimenters take for granted before they begin their
>>>> experiments is infinitely more interesting than any results to which their
>>>> -- Norbert Wiener
>>>>
>>>> https://www.cse.buffalo.edu/~knepley/
>>>> <http://www.cse.buffalo.edu/~knepley/>
>>>>
>>>
>>>
>>> --
>>> What most experimenters take for granted before they begin their
>>> experiments is infinitely more interesting than any results to which their
>>> -- Norbert Wiener
>>>
>>> https://www.cse.buffalo.edu/~knepley/
>>> <http://www.cse.buffalo.edu/~knepley/>
>>>
>>
>>
>> --
>> What most experimenters take for granted before they begin their
>> experiments is infinitely more interesting than any results to which their
>> -- Norbert Wiener
>>
>> https://www.cse.buffalo.edu/~knepley/
>> <http://www.cse.buffalo.edu/~knepley/>
>>
>
>
> --
> What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which their
> -- Norbert Wiener
>
> https://www.cse.buffalo.edu/~knepley/
> <http://www.cse.buffalo.edu/~knepley/>
>

--
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their