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