Actual source code: pname.c


  2: #include <petsc/private/petscimpl.h>
  3: #include <petscviewer.h>

  5: /*@C
  6:    PetscObjectSetName - Sets a string name associated with a PETSc object.

  8:    Not Collective

 10:    Input Parameters:
 11: +  obj - the Petsc variable
 12:          Thus must be cast with a (`PetscObject`), for example,
 13:          `PetscObjectSetName`((`PetscObject`)mat,name);
 14: -  name - the name to give obj

 16:    Level: advanced

 18:    Note:
 19:     If this routine is not called then the object may end up being name by `PetscObjectName()`.

 21: .seealso: `PetscObjectGetName()`, `PetscObjectName()`
 22: @*/
 23: PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[])
 24: {
 26:   PetscFree(obj->name);
 27:   PetscStrallocpy(name, &obj->name);
 28:   return 0;
 29: }

 31: /*@C
 32:       PetscObjectPrintClassNamePrefixType - used in the `XXXView()` methods to display information about the class, name, prefix and type of an object

 34:    Input Parameters:
 35: +     obj - the PETSc object
 36: -     viewer - ASCII viewer where the information is printed, function does nothing if the viewer is not `PETSCVIEWERASCII` type

 38:    Level: developer

 40:    Notes:
 41:    If the viewer format is `PETSC_VIEWER_ASCII_MATLAB` then the information is printed after a % symbol
 42:    so that MATLAB will treat it as a comment.

 44:    If the viewer format is `PETSC_VIEWER_ASCII_VTK*`, `PETSC_VIEWER_ASCII_LATEX`, or
 45:    `PETSC_VIEWER_ASCII_MATRIXMARKET` then don't print header information
 46:    as these formats can't process it.

 48:    Developer Note:
 49:    The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used to actually print the object.

 51: .seealso: `PetscObjectSetName()`, `PetscObjectName()`
 52: @*/
 53: PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj, PetscViewer viewer)
 54: {
 55:   PetscMPIInt       size;
 56:   PetscViewerFormat format;
 57:   PetscBool         flg;

 59:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg);
 60:   if (obj->donotPetscObjectPrintClassNamePrefixType) return 0;
 61:   if (!flg) return 0;

 63:   PetscViewerGetFormat(viewer, &format);
 64:   if (format == PETSC_VIEWER_ASCII_VTK_DEPRECATED || format == PETSC_VIEWER_ASCII_VTK_CELL_DEPRECATED || format == PETSC_VIEWER_ASCII_VTK_COORDS_DEPRECATED || format == PETSC_VIEWER_ASCII_MATRIXMARKET || format == PETSC_VIEWER_ASCII_LATEX || format == PETSC_VIEWER_ASCII_GLVIS)
 65:     return 0;

 67:   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscViewerASCIIPrintf(viewer, "%%");
 68:   MPI_Comm_size(PetscObjectComm(obj), &size);
 69:   PetscViewerASCIIPrintf(viewer, "%s Object:%s%s%s%s%s %d MPI process%s\n", obj->class_name, obj->name ? " " : "", obj->name ? obj->name : "", obj->prefix ? " (" : "", obj->prefix ? obj->prefix : "", obj->prefix ? ")" : "", size, size > 1 ? "es" : "");
 70:   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscViewerASCIIPrintf(viewer, "%%");
 71:   if (obj->type_name) {
 72:     PetscViewerASCIIPrintf(viewer, "  type: %s\n", obj->type_name);
 73:   } else {
 74:     PetscViewerASCIIPrintf(viewer, "  type not yet set\n");
 75:   }
 76:   return 0;
 77: }

 79: /*@C
 80:    PetscObjectName - Gives an object a name if it does not have one

 82:    Collective

 84:    Input Parameters:
 85: .  obj - the Petsc variable
 86:          Thus must be cast with a (`PetscObject`), for example,
 87:          `PetscObjectName`((`PetscObject`)mat,name);

 89:    Level: developer

 91:    Notes:
 92:    This is used in a small number of places when an object NEEDS a name, for example when it is saved to MATLAB with that variable name.

 94:    Use `PetscObjectSetName()` to set the name of an object to what you want. The SAWs viewer requires that no two published objects
 95:    share the same name.

 97:    Developer Note:
 98:    This needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.

100: .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
101: @*/
102: PetscErrorCode PetscObjectName(PetscObject obj)
103: {
104:   PetscCommCounter *counter;
105:   PetscMPIInt       flg;
106:   char              name[64];

109:   if (!obj->name) {
110:     union
111:     {
112:       MPI_Comm comm;
113:       void    *ptr;
114:       char     raw[sizeof(MPI_Comm)];
115:     } ucomm;
116:     MPI_Comm_get_attr(obj->comm, Petsc_Counter_keyval, (void *)&counter, &flg);
118:     ucomm.ptr  = NULL;
119:     ucomm.comm = obj->comm;
120:     MPI_Bcast(ucomm.raw, sizeof(MPI_Comm), MPI_BYTE, 0, obj->comm);
121:     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
122:      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
123:      * abuse acceptable. */
124:     PetscSNPrintf(name, 64, "%s_%p_%" PetscInt_FMT, obj->class_name, ucomm.ptr, counter->namecount++);
125:     PetscStrallocpy(name, &obj->name);
126:   }
127:   return 0;
128: }

130: PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[])
131: {
133:   PetscFree(obj->type_name);
134:   PetscStrallocpy(type_name, &obj->type_name);
135:   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
136:   PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE] * sizeof(PetscFortranCallback));
137:   return 0;
138: }