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