Actual source code: pname.c

petsc-3.9.0 2018-04-07
Report Typos and Errors

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

 19:    Concepts: object name^setting

 21: .seealso: PetscObjectGetName(), PetscObjectName()
 22: @*/
 23: PetscErrorCode  PetscObjectSetName(PetscObject obj,const char name[])
 24: {

 29:   PetscFree(obj->name);
 30:   PetscStrallocpy(name,&obj->name);
 31:   return(0);
 32: }

 34: /*@C
 35:       PetscObjectPrintTypeNamePrefix - used in the XXXView() methods to display information about the class, name, prefix and type of an object

 37:    Input Parameters:
 38: +     obj - the PETSc object
 39: -     viewer - ASCII viewer where the information is printed, function does nothing if the viewer is not PETSCVIEWERASCII type

 41:    Level: developer

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

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

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

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

 55: @*/
 56: PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
 57: {
 58:   PetscErrorCode    ierr;
 59:   MPI_Comm          comm;
 60:   PetscMPIInt       size;
 61:   PetscViewerFormat format;
 62:   PetscBool         flg;

 65:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg);
 66:   if (obj->donotPetscObjectPrintClassNamePrefixType) return(0);
 67:   if (!flg) return(0);

 69:   PetscViewerGetFormat(viewer,&format);
 70:   if (format == PETSC_VIEWER_ASCII_VTK || format == PETSC_VIEWER_ASCII_VTK_CELL || format == PETSC_VIEWER_ASCII_VTK_COORDS || format == PETSC_VIEWER_ASCII_MATRIXMARKET || format == PETSC_VIEWER_ASCII_LATEX || format == PETSC_VIEWER_ASCII_GLVIS) return(0);

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

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

 97:    Collective

 99:    Input Parameters:
100: .  obj - the Petsc variable
101:          Thus must be cast with a (PetscObject), for example,
102:          PetscObjectName((PetscObject)mat,name);

104:    Level: developer

106:    Concepts: object name^setting default

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

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



116: .seealso: PetscObjectGetName(), PetscObjectSetName()
117: @*/
118: PetscErrorCode  PetscObjectName(PetscObject obj)
119: {
120:   PetscErrorCode   ierr;
121:   PetscCommCounter *counter;
122:   PetscMPIInt      flg;
123:   char             name[64];

127:   if (!obj->name) {
128:     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
129:     MPI_Comm_get_attr(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg);
130:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
131:     ucomm.ptr = NULL;
132:     ucomm.comm = obj->comm;
133:     MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm);
134:     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
135:      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
136:      * abuse acceptable. */
137:     PetscSNPrintf(name,64,"%s_%p_%D",obj->class_name,ucomm.ptr,counter->namecount++);
138:     PetscStrallocpy(name,&obj->name);
139:   }
140:   return(0);
141: }



145: PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
146: {

151:   PetscFree(obj->type_name);
152:   PetscStrallocpy(type_name,&obj->type_name);
153:   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
154:   PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback));
155:   return(0);
156: }