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