Actual source code: kspsaws.c
petsc-3.5.4 2015-05-23
1: #include <petsc-private/kspimpl.h> /*I "petscksp.h" I*/
2: #include <petscviewersaws.h>
4: typedef struct {
5: PetscViewer viewer;
6: PetscInt neigs;
7: PetscReal *eigi;
8: PetscReal *eigr;
9: } KSPMonitor_SAWs;
13: /*@C
14: KSPMonitorSAWsCreate - create an SAWs monitor context
16: Collective
18: Input Arguments:
19: . ksp - KSP to monitor
21: Output Arguments:
22: . ctx - context for monitor
24: Level: developer
26: .seealso: KSPMonitorSAWs(), KSPMonitorSAWsDestroy()
27: @*/
28: PetscErrorCode KSPMonitorSAWsCreate(KSP ksp,void **ctx)
29: {
30: PetscErrorCode ierr;
31: KSPMonitor_SAWs *mon;
34: PetscNewLog(ksp,&mon);
35: mon->viewer = PETSC_VIEWER_SAWS_(PetscObjectComm((PetscObject)ksp));
36: if (!mon->viewer) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"Cannot create SAWs default viewer");
37: *ctx = (void*)mon;
38: return(0);
39: }
43: /*@C
44: KSPMonitorSAWsDestroy - destroy a monitor context created with KSPMonitorSAWsCreate()
46: Collective
48: Input Arguments:
49: . ctx - monitor context
51: Level: developer
53: .seealso: KSPMonitorSAWsCreate()
54: @*/
55: PetscErrorCode KSPMonitorSAWsDestroy(void **ctx)
56: {
57: KSPMonitor_SAWs *mon = (KSPMonitor_SAWs*)*ctx;
58: PetscErrorCode ierr;
61: PetscFree2(mon->eigr,mon->eigi);
62: PetscFree(*ctx);
63: return(0);
64: }
68: /*@C
69: KSPMonitorSAWs - monitor solution using SAWs
71: Logically Collective on KSP
73: Input Parameters:
74: + ksp - iterative context
75: . n - iteration number
76: . rnorm - 2-norm (preconditioned) residual value (may be estimated).
77: - ctx - PetscViewer of type SAWs
79: Level: advanced
81: .keywords: KSP, CG, monitor, SAWs, singular values
83: .seealso: KSPMonitorSingularValue(), KSPComputeExtremeSingularValues(), PetscViewerSAWsOpen()
84: @*/
85: PetscErrorCode KSPMonitorSAWs(KSP ksp,PetscInt n,PetscReal rnorm,void *ctx)
86: {
87: PetscErrorCode ierr;
88: KSPMonitor_SAWs *mon = (KSPMonitor_SAWs*)ctx;
89: PetscViewer viewer = mon->viewer;
90: PetscReal emax,emin;
91: PetscMPIInt rank;
96: KSPComputeExtremeSingularValues(ksp,&emax,&emin);
98: PetscFree2(mon->eigr,mon->eigi);
99: PetscMalloc2(n,&mon->eigr,n,&mon->eigi);
100: if (n) {
101: KSPComputeEigenvalues(ksp,n,mon->eigr,mon->eigi,&mon->neigs);
103: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
104: if (!rank) {
105: SAWs_Delete("/PETSc/ksp_monitor_saws/eigr");
106: SAWs_Delete("/PETSc/ksp_monitor_saws/eigi");
108: PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/rnorm",&ksp->rnorm,1,SAWs_READ,SAWs_DOUBLE));
109: PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/neigs",&mon->neigs,1,SAWs_READ,SAWs_INT));
110: if (mon->neigs > 0) {
111: PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigr",mon->eigr,mon->neigs,SAWs_READ,SAWs_DOUBLE));
112: PetscStackCallSAWs(SAWs_Register,("/PETSc/ksp_monitor_saws/eigi",mon->eigi,mon->neigs,SAWs_READ,SAWs_DOUBLE));
113: }
114: PetscInfo2(ksp,"KSP extreme singular values min=%g max=%g\n",(double)emin,(double)emax);
115: PetscSAWsBlock();
116: }
117: }
118: return(0);
119: }