Actual source code: pname.c

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

      4: /*@
      5:   PetscObjectSetName - Sets a string name for a PETSc object.

      7:   Not Collective

      9:   Input Parameters:
     10: + obj  - the PETSc object. It must be cast with a (`PetscObject`), for example,
     11:          `PetscObjectSetName`((`PetscObject`)mat,name);
     12: - name - the name to give obj

     14:   Level: advanced

     16:   Note:
     17:   If this routine is not called then `obj` may end up being named by `PetscObjectName()`.

     19: .seealso: `PetscObjectGetName()`, `PetscObjectName()`
     20: @*/
     21: PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[])
     22: {
     23:   PetscFunctionBegin;
     25:   PetscCall(PetscFree(obj->name));
     26:   PetscCall(PetscStrallocpy(name, &obj->name));
     27:   PetscFunctionReturn(PETSC_SUCCESS);
     28: }

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

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

     37:   Level: developer

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

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

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

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

     58:   PetscFunctionBegin;
     59:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg));
     60:   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(PETSC_SUCCESS);
     61:   if (!flg) PetscFunctionReturn(PETSC_SUCCESS);

     63:   PetscCall(PetscViewerGetFormat(viewer, &format));
     64:   if (format == PETSC_VIEWER_ASCII_MATRIXMARKET || format == PETSC_VIEWER_ASCII_LATEX || format == PETSC_VIEWER_ASCII_GLVIS) PetscFunctionReturn(PETSC_SUCCESS);

     66:   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
     67:   PetscCallMPI(MPI_Comm_size(PetscObjectComm(obj), &size));
     68:   PetscCall(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" : ""));
     69:   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
     70:   if (obj->type_name) {
     71:     PetscCall(PetscViewerASCIIPrintf(viewer, "  type: %s\n", obj->type_name));
     72:   } else {
     73:     PetscCall(PetscViewerASCIIPrintf(viewer, "  type not yet set\n"));
     74:   }
     75:   PetscFunctionReturn(PETSC_SUCCESS);
     76: }

     78: /*@
     79:   PetscObjectName - Gives `obj` a name if it does not have one

     81:   Collective

     83:   Input Parameter:
     84: . obj - the PETSc object. It must be cast with a (`PetscObject`), for example, `PetscObjectName`((`PetscObject`)mat,name);

     86:   Level: developer

     88:   Notes:
     89:   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.

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

     94:   Developer Note:
     95:   This needs to generate the exact same string on all MPI processes that share `obj`. The current algorithm may not always work.

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

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

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