xref: /petsc/src/sys/objects/pname.c (revision 3307d110e72ee4e6d2468971620073eb5ff93529)
1 
2 #include <petsc/private/petscimpl.h>        /*I    "petscsys.h"   I*/
3 #include <petscviewer.h>
4 
5 /*@C
6    PetscObjectSetName - Sets a string name associated with a PETSc object.
7 
8    Not Collective
9 
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
15 
16    Notes:
17     If this routine is not called then the object may end up being name by PetscObjectName().
18    Level: advanced
19 
20 .seealso: PetscObjectGetName(), PetscObjectName()
21 @*/
22 PetscErrorCode  PetscObjectSetName(PetscObject obj,const char name[])
23 {
24   PetscFunctionBegin;
25   PetscValidHeader(obj,1);
26   PetscCall(PetscFree(obj->name));
27   PetscCall(PetscStrallocpy(name,&obj->name));
28   PetscFunctionReturn(0);
29 }
30 
31 /*@C
32       PetscObjectPrintClassNamePrefixType - used in the XXXView() methods to display information about the class, name, prefix and type of an object
33 
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
37 
38    Level: developer
39 
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.
43 
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.
47 
48    Developer Note: The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used
49                    to actually print the object.
50 
51 .seealso: PetscObjectSetName(), PetscObjectName()
52 
53 @*/
54 PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
55 {
56   MPI_Comm          comm;
57   PetscMPIInt       size;
58   PetscViewerFormat format;
59   PetscBool         flg;
60 
61   PetscFunctionBegin;
62   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg));
63   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(0);
64   if (!flg) PetscFunctionReturn(0);
65 
66   PetscCall(PetscViewerGetFormat(viewer,&format));
67   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) PetscFunctionReturn(0);
68 
69   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer,"%%"));
70   PetscCall(PetscViewerASCIIPrintf(viewer,"%s Object:",obj->class_name));
71   PetscCall(PetscViewerASCIIUseTabs(viewer,PETSC_FALSE));
72   if (obj->name) {
73     PetscCall(PetscViewerASCIIPrintf(viewer," %s",obj->name));
74   }
75   if (obj->prefix) {
76     PetscCall(PetscViewerASCIIPrintf(viewer," (%s)",obj->prefix));
77   }
78   PetscCall(PetscObjectGetComm(obj,&comm));
79   PetscCallMPI(MPI_Comm_size(comm,&size));
80   PetscCall(PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size));
81   PetscCall(PetscViewerASCIIUseTabs(viewer,PETSC_TRUE));
82   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer,"%%"));
83   if (obj->type_name) {
84     PetscCall(PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name));
85   } else {
86     PetscCall(PetscViewerASCIIPrintf(viewer,"  type not yet set\n"));
87   }
88   PetscFunctionReturn(0);
89 }
90 
91 /*@C
92    PetscObjectName - Gives an object a name if it does not have one
93 
94    Collective
95 
96    Input Parameters:
97 .  obj - the Petsc variable
98          Thus must be cast with a (PetscObject), for example,
99          PetscObjectName((PetscObject)mat,name);
100 
101    Level: developer
102 
103    Notes:
104     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.
105           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
106           share the same name.
107 
108    Developer Note: this needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.
109 
110 .seealso: PetscObjectGetName(), PetscObjectSetName()
111 @*/
112 PetscErrorCode  PetscObjectName(PetscObject obj)
113 {
114   PetscCommCounter *counter;
115   PetscMPIInt      flg;
116   char             name[64];
117 
118   PetscFunctionBegin;
119   PetscValidHeader(obj,1);
120   if (!obj->name) {
121     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
122     PetscCallMPI(MPI_Comm_get_attr(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg));
123     PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
124     ucomm.ptr = NULL;
125     ucomm.comm = obj->comm;
126     PetscCallMPI(MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm));
127     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
128      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
129      * abuse acceptable. */
130     PetscCall(PetscSNPrintf(name,64,"%s_%p_%" PetscInt_FMT,obj->class_name,ucomm.ptr,counter->namecount++));
131     PetscCall(PetscStrallocpy(name,&obj->name));
132   }
133   PetscFunctionReturn(0);
134 }
135 
136 PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
137 {
138   PetscFunctionBegin;
139   PetscValidHeader(obj,1);
140   PetscCall(PetscFree(obj->type_name));
141   PetscCall(PetscStrallocpy(type_name,&obj->type_name));
142   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
143   PetscCall(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback)));
144   PetscFunctionReturn(0);
145 }
146