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 @*/
PetscObjectSetName(PetscObject obj,const char name[])22 PetscErrorCode  PetscObjectSetName(PetscObject obj,const char name[])
23 {
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   PetscValidHeader(obj,1);
28   ierr = PetscFree(obj->name);CHKERRQ(ierr);
29   ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr);
30   PetscFunctionReturn(0);
31 }
32 
33 /*@C
34       PetscObjectPrintClassNamePrefixType - used in the XXXView() methods to display information about the class, name, prefix and type of an object
35 
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
39 
40    Level: developer
41 
42    Notes:
43     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.
45 
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.
49 
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.
52 
53 .seealso: PetscObjectSetName(), PetscObjectName()
54 
55 @*/
PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)56 PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
57 {
58   PetscErrorCode    ierr;
59   MPI_Comm          comm;
60   PetscMPIInt       size;
61   PetscViewerFormat format;
62   PetscBool         flg;
63 
64   PetscFunctionBegin;
65   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
66   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(0);
67   if (!flg) PetscFunctionReturn(0);
68 
69   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
70   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);
71 
72   if (format == PETSC_VIEWER_ASCII_MATLAB) {ierr = PetscViewerASCIIPrintf(viewer,"%%");CHKERRQ(ierr);}
73   ierr = PetscViewerASCIIPrintf(viewer,"%s Object:",obj->class_name);CHKERRQ(ierr);
74   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
75   if (obj->name) {
76     ierr = PetscViewerASCIIPrintf(viewer," %s",obj->name);CHKERRQ(ierr);
77   }
78   if (obj->prefix) {
79     ierr = PetscViewerASCIIPrintf(viewer," (%s)",obj->prefix);CHKERRQ(ierr);
80   }
81   ierr = PetscObjectGetComm(obj,&comm);CHKERRQ(ierr);
82   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
83   ierr = PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size);CHKERRQ(ierr);
84   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
85   if (format == PETSC_VIEWER_ASCII_MATLAB) {ierr = PetscViewerASCIIPrintf(viewer,"%%");CHKERRQ(ierr);}
86   if (obj->type_name) {
87     ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name);CHKERRQ(ierr);
88   } else {
89     ierr = PetscViewerASCIIPrintf(viewer,"  type not yet set\n");CHKERRQ(ierr);
90   }
91   PetscFunctionReturn(0);
92 }
93 
94 /*@C
95    PetscObjectName - Gives an object a name if it does not have one
96 
97    Collective
98 
99    Input Parameters:
100 .  obj - the Petsc variable
101          Thus must be cast with a (PetscObject), for example,
102          PetscObjectName((PetscObject)mat,name);
103 
104    Level: developer
105 
106    Notes:
107     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.
108           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
109           share the same name.
110 
111    Developer Note: this needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.
112 
113 
114 
115 .seealso: PetscObjectGetName(), PetscObjectSetName()
116 @*/
PetscObjectName(PetscObject obj)117 PetscErrorCode  PetscObjectName(PetscObject obj)
118 {
119   PetscErrorCode   ierr;
120   PetscCommCounter *counter;
121   PetscMPIInt      flg;
122   char             name[64];
123 
124   PetscFunctionBegin;
125   PetscValidHeader(obj,1);
126   if (!obj->name) {
127     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
128     ierr = MPI_Comm_get_attr(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg);CHKERRQ(ierr);
129     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
130     ucomm.ptr = NULL;
131     ucomm.comm = obj->comm;
132     ierr = MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm);CHKERRQ(ierr);
133     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
134      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
135      * abuse acceptable. */
136     ierr = PetscSNPrintf(name,64,"%s_%p_%D",obj->class_name,ucomm.ptr,counter->namecount++);CHKERRQ(ierr);
137     ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr);
138   }
139   PetscFunctionReturn(0);
140 }
141 
142 
143 
PetscObjectChangeTypeName(PetscObject obj,const char type_name[])144 PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
145 {
146   PetscErrorCode ierr;
147 
148   PetscFunctionBegin;
149   PetscValidHeader(obj,1);
150   ierr = PetscFree(obj->type_name);CHKERRQ(ierr);
151   ierr = PetscStrallocpy(type_name,&obj->type_name);CHKERRQ(ierr);
152   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
153   ierr = PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback));CHKERRQ(ierr);
154   PetscFunctionReturn(0);
155 }
156 
157