Actual source code: viewers.c


  2: #include <petscsys.h>
  3: #include <petsc/private/viewerimpl.h>

  5: struct _n_PetscViewers {
  6:   MPI_Comm    comm;
  7:   PetscViewer *viewer;
  8:   int         n;
  9: };

 11: /*@C
 12:    PetscViewersDestroy - Destroys a set of PetscViewers created with PetscViewersCreate().

 14:    Collective on PetscViewers

 16:    Input Parameters:
 17: .  v - the PetscViewers to be destroyed.

 19:    Level: intermediate

 21: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewersCreate()

 23: @*/
 24: PetscErrorCode  PetscViewersDestroy(PetscViewers *v)
 25: {
 26:   int            i;

 28:   if (!*v) return 0;
 29:   for (i=0; i<(*v)->n; i++) {
 30:     PetscViewerDestroy(&(*v)->viewer[i]);
 31:   }
 32:   PetscFree((*v)->viewer);
 33:   PetscFree(*v);
 34:   return 0;
 35: }

 37: /*@C
 38:    PetscViewersCreate - Creates a container to hold a set of PetscViewers.

 40:    Collective

 42:    Input Parameter:
 43: .   comm - the MPI communicator

 45:    Output Parameter:
 46: .  v - the collection of PetscViewers

 48:    Level: intermediate

 50: .seealso: PetscViewerCreate(), PetscViewersDestroy()

 52: @*/
 53: PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
 54: {
 56:   PetscNew(v);
 57:   (*v)->n    = 64;
 58:   (*v)->comm = comm;

 60:   PetscCalloc1(64,&(*v)->viewer);
 61:   return 0;
 62: }

 64: /*@C
 65:    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection

 67:    Not Collective, but PetscViewer will be collective object on PetscViewers

 69:    Input Parameters:
 70: +   viewers - object created with PetscViewersCreate()
 71: -   n - number of PetscViewer you want

 73:    Output Parameter:
 74: .  viewer - the PetscViewer

 76:    Level: intermediate

 78: .seealso: PetscViewersCreate(), PetscViewersDestroy()

 80: @*/
 81: PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
 82: {
 86:   if (n >= viewers->n) {
 87:     PetscViewer *v;
 88:     int         newn = n + 64; /* add 64 new ones at a time */

 90:     PetscCalloc1(newn,&v);
 91:     PetscArraycpy(v,viewers->viewer,viewers->n);
 92:     PetscFree(viewers->viewer);

 94:     viewers->viewer = v;
 95:   }
 96:   if (!viewers->viewer[n]) {
 97:     PetscViewerCreate(viewers->comm,&viewers->viewer[n]);
 98:   }
 99:   *viewer = viewers->viewer[n];
100:   return 0;
101: }

103: /*
104:   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one

106:   Not collective

108:   Input Parameters:
109: + nmon      - The new monitor
110: . nmctx     - The new monitor context, or NULL
111: . nmdestroy - The new monitor destroy function, or NULL
112: . mon       - The old monitor
113: . mctx      - The old monitor context, or NULL
114: - mdestroy  - The old monitor destroy function, or NULL

116:   Output Parameter:
117: . identical - PETSC_TRUE if the monitors are the same

119:   Level: developer

121: .seealsp: DMMonitorSetFromOptions(), KSPMonitorSetFromOptions(), SNESMonitorSetFromOptions()
122: */
123: PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
124: {
126:   *identical = PETSC_FALSE;
127:   if (nmon == mon && nmdestroy == mdestroy) {
128:     if (nmctx == mctx) *identical = PETSC_TRUE;
129:     else if (nmdestroy == (PetscErrorCode (*)(void**)) PetscViewerAndFormatDestroy) {
130:       PetscViewerAndFormat *old = (PetscViewerAndFormat*)mctx, *newo = (PetscViewerAndFormat*)nmctx;
131:       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
132:     }
133:     if (*identical) {
134:       if (mdestroy) {
135:         (*mdestroy)(&nmctx);
136:       }
137:     }
138:   }
139:   return 0;
140: }