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