Actual source code: kspsaws.c

petsc-3.11.3 2019-06-26
Report Typos and Errors
  1:  #include <petsc/private/kspimpl.h>
  2:  #include <petscviewersaws.h>

  4: typedef struct {
  5:   PetscViewer    viewer;
  6:   PetscInt       neigs;
  7:   PetscReal      *eigi;
  8:   PetscReal      *eigr;
  9: } KSPMonitor_SAWs;

 11: /*@C
 12:    KSPMonitorSAWsCreate - create an SAWs monitor context

 14:    Collective

 16:    Input Arguments:
 17: .  ksp - KSP to monitor

 19:    Output Arguments:
 20: .  ctx - context for monitor

 22:    Level: developer

 24: .seealso: KSPMonitorSAWs(), KSPMonitorSAWsDestroy()
 25: @*/
 26: PetscErrorCode KSPMonitorSAWsCreate(KSP ksp,void **ctx)
 27: {
 28:   PetscErrorCode  ierr;
 29:   KSPMonitor_SAWs *mon;

 32:   PetscNewLog(ksp,&mon);
 33:   mon->viewer = PETSC_VIEWER_SAWS_(PetscObjectComm((PetscObject)ksp));
 34:   if (!mon->viewer) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"Cannot create SAWs default viewer");
 35:   *ctx = (void*)mon;
 36:   return(0);
 37: }

 39: /*@C
 40:    KSPMonitorSAWsDestroy - destroy a monitor context created with KSPMonitorSAWsCreate()

 42:    Collective

 44:    Input Arguments:
 45: .  ctx - monitor context

 47:    Level: developer

 49: .seealso: KSPMonitorSAWsCreate()
 50: @*/
 51: PetscErrorCode KSPMonitorSAWsDestroy(void **ctx)
 52: {
 53:   KSPMonitor_SAWs *mon = (KSPMonitor_SAWs*)*ctx;
 54:   PetscErrorCode  ierr;

 57:   PetscFree2(mon->eigr,mon->eigi);
 58:   PetscFree(*ctx);
 59:   return(0);
 60: }

 62: /*@C
 63:    KSPMonitorSAWs - monitor solution using SAWs

 65:    Logically Collective on KSP

 67:    Input Parameters:
 68: +  ksp   - iterative context
 69: .  n     - iteration number
 70: .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
 71: -  ctx -  PetscViewer of type SAWs

 73:    Level: advanced

 75: .keywords: KSP, CG, monitor, SAWs, singular values

 77: .seealso: KSPMonitorSingularValue(), KSPComputeExtremeSingularValues(), PetscViewerSAWsOpen()
 78: @*/
 79: PetscErrorCode KSPMonitorSAWs(KSP ksp,PetscInt n,PetscReal rnorm,void *ctx)
 80: {
 81:   PetscErrorCode  ierr;
 82:   KSPMonitor_SAWs *mon   = (KSPMonitor_SAWs*)ctx;
 83:   PetscReal       emax,emin;
 84:   PetscMPIInt     rank;

 88:   KSPComputeExtremeSingularValues(ksp,&emax,&emin);

 90:   PetscFree2(mon->eigr,mon->eigi);
 91:   PetscMalloc2(n,&mon->eigr,n,&mon->eigi);
 92:   if (n) {
 93:     KSPComputeEigenvalues(ksp,n,mon->eigr,mon->eigi,&mon->neigs);

 95:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 96:     if (!rank) {
 97:       SAWs_Delete("/PETSc/ksp_monitor_saws/eigr");
 98:       SAWs_Delete("/PETSc/ksp_monitor_saws/eigi");

100:       PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/rnorm",&ksp->rnorm,1,SAWs_READ,SAWs_DOUBLE));
101:       PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/neigs",&mon->neigs,1,SAWs_READ,SAWs_INT));
102:       if (mon->neigs > 0) {
103:         PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigr",mon->eigr,mon->neigs,SAWs_READ,SAWs_DOUBLE));
104:         PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigi",mon->eigi,mon->neigs,SAWs_READ,SAWs_DOUBLE));
105:       }
106:       PetscInfo2(ksp,"KSP extreme singular values min=%g max=%g\n",(double)emin,(double)emax);
107:       PetscSAWsBlock();
108:     }
109:   }
110:   return(0);
111: }