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