Actual source code: pname.c

petsc-3.8.4 2018-03-24
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:    Level: advanced

 18:    Concepts: object name^setting

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

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

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

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

 40:    Level: developer

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

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

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

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

 61:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg);
 62:   if (!flg) return(0);

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

 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:    Concepts: object name^setting default

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

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



111: .seealso: PetscObjectGetName(), PetscObjectSetName()
112: @*/
113: PetscErrorCode  PetscObjectName(PetscObject obj)
114: {
115:   PetscErrorCode   ierr;
116:   PetscCommCounter *counter;
117:   PetscMPIInt      flg;
118:   char             name[64];

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



140: PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
141: {

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