xref: /petsc/src/sys/objects/pname.c (revision 9bc64357eeacb0dff7eaaae52926ecac7d8eacd5)
1 
2 #include <petsc-private/petscimpl.h>        /*I    "petscsys.h"   I*/
3 #include <petscviewer.h>
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "PetscObjectSetName"
7 /*@C
8    PetscObjectSetName - Sets a string name associated with a PETSc object.
9 
10    Not Collective
11 
12    Input Parameters:
13 +  obj - the Petsc variable
14          Thus must be cast with a (PetscObject), for example,
15          PetscObjectSetName((PetscObject)mat,name);
16 -  name - the name to give obj
17 
18    Level: advanced
19 
20    Concepts: object name^setting
21 
22 .seealso: PetscObjectGetName()
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 #undef __FUNCT__
36 #define __FUNCT__ "PetscObjectPrintClassNamePrefixType"
37 /*@C
38       PetscObjectPrintTypeNamePrefix - used in the XXXView() methods to display information about the class, name, prefix and type of an object
39 
40    Input Parameters:
41 +     obj - the PETSc object
42 -     viewer - ASCII viewer where the information is printed, function does nothing if the viewer is not PETSCVIEWERASCII type
43 
44    Level: developer
45 
46    Notes: If the viewer format is PETSC_VIEWER_ASCII_MATLAB then the information is printed after a % symbol
47           so that MATLAB will treat it as a comment.
48 
49           If the viewer format is PETSC_VIEWER_ASCII_VTK* or
50           PETSC_VIEWER_ASCII_MATRIXMARKET then don't print header information
51           as these formats can't process it.
52 
53 .seealso: PetscObjectSetName(), PetscObjectName()
54 
55 @*/
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 (!flg) PetscFunctionReturn(0);
67 
68   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
69   if (format == PETSC_VIEWER_ASCII_VTK
70       || format == PETSC_VIEWER_ASCII_VTK_CELL
71       || format == PETSC_VIEWER_ASCII_VTK_COORDS
72       || format == PETSC_VIEWER_ASCII_MATRIXMARKET) 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   if (obj->name) {
77     ierr = PetscViewerASCIIPrintf(viewer,"%s",obj->name);CHKERRQ(ierr);
78   }
79   if (obj->prefix) {
80     ierr = PetscViewerASCIIPrintf(viewer,"(%s)",obj->prefix);CHKERRQ(ierr);
81   }
82   ierr = PetscObjectGetComm(obj,&comm);CHKERRQ(ierr);
83   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
84   ierr = PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size);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 #undef __FUNCT__
95 #define __FUNCT__ "PetscObjectName"
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: 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.
111           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
112           share the same name.
113 
114    Developer Note: this needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.
115 
116 
117 
118 .seealso: PetscObjectGetName(), PetscObjectSetName()
119 @*/
120 PetscErrorCode  PetscObjectName(PetscObject obj)
121 {
122   PetscErrorCode   ierr;
123   PetscCommCounter *counter;
124   PetscMPIInt      flg;
125   char             name[64];
126 
127   PetscFunctionBegin;
128   PetscValidHeader(obj,1);
129   if (!obj->name) {
130     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
131     ierr = MPI_Attr_get(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg);CHKERRQ(ierr);
132     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
133     ucomm.ptr = NULL;
134     ucomm.comm = obj->comm;
135     ierr = MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm);CHKERRQ(ierr);
136     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
137      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
138      * abuse acceptable. */
139     ierr = PetscSNPrintf(name,64,"%s_%p_%D",obj->class_name,ucomm.ptr,counter->namecount++);CHKERRQ(ierr);
140     ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr);
141   }
142   PetscFunctionReturn(0);
143 }
144 
145 
146 
147 #undef __FUNCT__
148 #define __FUNCT__ "PetscObjectChangeTypeName"
149 PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
150 {
151   PetscErrorCode ierr;
152 
153   PetscFunctionBegin;
154   PetscValidHeader(obj,1);
155   ierr = PetscFree(obj->type_name);CHKERRQ(ierr);
156   ierr = PetscStrallocpy(type_name,&obj->type_name);CHKERRQ(ierr);
157   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
158   ierr = PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback));CHKERRQ(ierr);
159   PetscFunctionReturn(0);
160 }
161 
162