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;

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

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

     42:    Collective

     44:    Input Parameter:
     45: .   comm - the MPI communicator

     47:    Output Parameter:
     48: .  v - the collection of PetscViewers

     50:    Level: intermediate

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

     54: @*/
     55: PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
     56: {

     60:   PetscNew(v);
     61:   (*v)->n    = 64;
     62:   (*v)->comm = comm;

     64:   PetscCalloc1(64,&(*v)->viewer);
     65:   return(0);
     66: }

     68: /*@C
     69:    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection

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

     73:    Input Parameter:
     74: +   viewers - object created with PetscViewersCreate()
     75: -   n - number of PetscViewer you want

     77:    Output Parameter:
     78: .  viewer - the PetscViewer

     80:    Level: intermediate

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

     84: @*/
     85: PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
     86: {

     90:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %d\n",n);
     91:   if (n >= viewers->n) {
     92:     PetscViewer *v;
     93:     int         newn = n + 64; /* add 64 new ones at a time */

     95:     PetscCalloc1(newn,&v);
     96:     PetscArraycpy(v,viewers->viewer,viewers->n);
     97:     PetscFree(viewers->viewer);

     99:     viewers->viewer = v;
    100:   }
    101:   if (!viewers->viewer[n]) {
    102:     PetscViewerCreate(viewers->comm,&viewers->viewer[n]);
    103:   }
    104:   *viewer = viewers->viewer[n];
    105:   return(0);
    106: }

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

    111:   Not collective

    113:   Input Parameters:
    114: + nmon      - The new monitor
    115: . nmctx     - The new monitor context, or NULL
    116: . nmdestroy - The new monitor destroy function, or NULL
    117: . mon       - The old monitor
    118: . mctx      - The old monitor context, or NULL
    119: - mdestroy  - The old monitor destroy function, or NULL

    121:   Output Parameter:
    122: . identical - PETSC_TRUE if the monitors are the same

    124:   Level: developer

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