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:    Notes:
 17:     If this routine is not called then the object may end up being name by PetscObjectName().
 18:    Level: advanced

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

 30: /*@C
 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 - ASCII 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: The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used
 48:                    to actually print the object.

 50: .seealso: PetscObjectSetName(), PetscObjectName()

 52: @*/
 53: PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
 54: {
 55:   MPI_Comm          comm;
 56:   PetscMPIInt       size;
 57:   PetscViewerFormat format;
 58:   PetscBool         flg;

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

 64:   PetscViewerGetFormat(viewer,&format);
 65:   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) return 0;

 67:   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscViewerASCIIPrintf(viewer,"%%");
 68:   PetscViewerASCIIPrintf(viewer,"%s Object:",obj->class_name);
 69:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
 70:   if (obj->name) {
 71:     PetscViewerASCIIPrintf(viewer," %s",obj->name);
 72:   }
 73:   if (obj->prefix) {
 74:     PetscViewerASCIIPrintf(viewer," (%s)",obj->prefix);
 75:   }
 76:   PetscObjectGetComm(obj,&comm);
 77:   MPI_Comm_size(comm,&size);
 78:   PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size);
 79:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
 80:   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscViewerASCIIPrintf(viewer,"%%");
 81:   if (obj->type_name) {
 82:     PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name);
 83:   } else {
 84:     PetscViewerASCIIPrintf(viewer,"  type not yet set\n");
 85:   }
 86:   return 0;
 87: }

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

 92:    Collective

 94:    Input Parameters:
 95: .  obj - the Petsc variable
 96:          Thus must be cast with a (PetscObject), for example,
 97:          PetscObjectName((PetscObject)mat,name);

 99:    Level: developer

101:    Notes:
102:     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.
103:           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
104:           share the same name.

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

108: .seealso: PetscObjectGetName(), PetscObjectSetName()
109: @*/
110: PetscErrorCode  PetscObjectName(PetscObject obj)
111: {
112:   PetscCommCounter *counter;
113:   PetscMPIInt      flg;
114:   char             name[64];

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

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