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