[petsc-users] Fortran, KSPMonitorSet, and KSPMonitorTrueResidual

Randall Mackie rlmackie862 at gmail.com
Fri Jul 18 12:08:16 CDT 2025


Thanks Barry!



> On Jul 17, 2025, at 8:58 PM, Barry Smith <bsmith at petsc.dev> wrote:
> 
> 
>   Randy,
> 
>    Thanks for pointing out this problem. I was generating the wrong C stubs for arguments such as PetscViewerAndFormat, that is arguments that are C structs but treated as FortranAddr in Fortran because the C struct is too complex to be directly represented in Fortran.
> 
>   This is fixed in https://urldefense.us/v3/__https://gitlab.com/petsc/petsc/-/merge_requests/8563__;!!G_uCfscf7eWS!cpFnpAAfsY4pLNBenaae5toN0iiU6tx2ilGtp9nukqV_fjZQXlneIci6mh96yqnVRvaPr38SO2VFxIQnXb8sQdBftA$  and should allow you to do what you previously had done. In the MR you will see I have modified ex2f.F to do essentially what your test code is doing.
> 
>   Barry
> 
> 
>> On Jun 23, 2025, at 2:46 PM, Randall Mackie <rlmackie862 at gmail.com> wrote:
>> 
>> In previous versions of PETSc we use to be able to call KSPMonitorTrueResidual from within our custom KSPMonitor, using an approach that is now commented out in the example found at https://urldefense.us/v3/__https://petsc.org/release/src/ksp/ksp/tutorials/ex2f.F90.html__;!!G_uCfscf7eWS!cpFnpAAfsY4pLNBenaae5toN0iiU6tx2ilGtp9nukqV_fjZQXlneIci6mh96yqnVRvaPr38SO2VFxIQnXb_bSiflJw$ : <https://urldefense.us/v3/__https://petsc.org/release/src/ksp/ksp/tutorials/ex2f.F90.html:__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8twVYS6KDQ$>
>> 
>> !
>> 214:  <>!     Cannot also use the default KSP <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSP/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tx3EWnatA$> monitor routine showing how it may be used from Fortran
>> 215:  <>!     since the Fortran compiler thinks the calling arguments are different in the two cases
>> 216:  <>!
>> 217:  <>!        PetscCallA <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Sys/PetscCallA/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzA4vLgOQ$>(PetscViewerAndFormatCreate <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PetscViewerAndFormatCreate/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tykmBKBfg$>(PETSC_VIEWER_STDOUT_WORLD <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PETSC_VIEWER_STDOUT_WORLD/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tw_EZkgfA$>,PETSC_VIEWER_DEFAULT <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PetscViewerFormat/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tyZ_k-1Uw$>,vf,ierr))
>> 218:  <>!        PetscCallA <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Sys/PetscCallA/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzA4vLgOQ$>(KSPMonitorSet <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSPMonitorSet/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tx6T8FJsA$>(ksp,KSPMonitorResidual <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSPMonitorResidual/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8txdiRZPSg$>,vf,PetscViewerAndFormatDestroy <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PetscViewerAndFormatDestroy/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzN4PpBPA$>,ierr))
>> 
>> Instead, that example uses:
>> 
>> 210:  <>      if (flg) then
>> 211:  <>        vzero = 0
>> 212:  <>        PetscCallA <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Sys/PetscCallA/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzA4vLgOQ$>(KSPMonitorSet <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSPMonitorSet/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tx6T8FJsA$>(ksp,MyKSPMonitor,vzero,PETSC_NULL_FUNCTION,ierr))
>> 213:  <>!
>> Regardless of which of these approaches I try, I cannot use KSPMonitorTrueResidual in the MyKSPMonitor routine.
>> I get the following error:
>> 
>> [0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
>> [0]PETSC ERROR: Null argument, when expecting valid pointer
>> [0]PETSC ERROR: Null Pointer: Parameter # 4
>> [0]PETSC ERROR: See https://urldefense.us/v3/__https://petsc.org/release/faq/__;!!G_uCfscf7eWS!cpFnpAAfsY4pLNBenaae5toN0iiU6tx2ilGtp9nukqV_fjZQXlneIci6mh96yqnVRvaPr38SO2VFxIQnXb8J5-MKxg$  <https://urldefense.us/v3/__https://petsc.org/release/faq/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzxt44McA$> for trouble shooting.
>> [0]PETSC ERROR: PETSc Release Version 3.23.3, May 30, 2025
>> [0]PETSC ERROR: ./test with 2 MPI process(es) and PETSC_ARCH linux-gfortran-complex-debug on rmackie-VirtualBox-2024 by rmackie Mon Jun 23 11:34:04 2025
>> [0]PETSC ERROR: Configure options: --with-clean=1 --with-scalar-type=complex --with-debugging=1 --with-fortran=1 --download-mpich=1
>> [0]PETSC ERROR: #1 KSPMonitorTrueResidual() at /home/rmackie/PETSc/petsc-3.23.3/src/ksp/ksp/interface/iterativ.c:400
>> [0]PETSC ERROR: #2 test.F90:303
>> 
>> 
>> I attach a slightly modified version of the example that demonstrates this behavior.
>> 
>> 
>> Thanks for the help,
>> 
>> Randy
>> 
>> <test.F90>
>>  
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250718/066dd52d/attachment.html>


More information about the petsc-users mailing list