xref: /petsc/src/sys/objects/gcomm.c (revision 750b007cd8d816cecd9de99077bb0a703b4cf61a)
1 
2 /*
3      Provides utility routines for manulating any type of PETSc object.
4 */
5 #include <petsc/private/petscimpl.h> /*I   "petscsys.h"    I*/
6 
7 /*@C
8    PetscObjectComm - Gets the MPI communicator for any `PetscObject` regardless of the type.
9 
10    Not Collective
11 
12    Input Parameter:
13 .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. Thus must be
14          cast with a (`PetscObject`), for example,
15          `PetscObjectComm`((`PetscObjec`t)mat,...);
16 
17    Output Parameter:
18 .  comm - the MPI communicator or `MPI_COMM_NULL` if object is not valid
19 
20    Level: advanced
21 
22 .seealso: `PetscObject`, `PetscObjectGetComm()`
23 @*/
24 MPI_Comm PetscObjectComm(PetscObject obj) {
25   return obj ? obj->comm : MPI_COMM_NULL;
26 }
27 
28 /*@C
29    PetscObjectGetComm - Gets the MPI communicator for any `PetscObject`,
30    regardless of the type.
31 
32    Not Collective
33 
34    Input Parameter:
35 .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. Thus must be
36          cast with a (`PetscObject`), for example,
37          `PetscObjectGetComm`((`PetscObject`)mat,&comm);
38 
39    Output Parameter:
40 .  comm - the MPI communicator
41 
42    Level: advanced
43 
44 .seealso: `PetscObjectComm()`
45 @*/
46 PetscErrorCode PetscObjectGetComm(PetscObject obj, MPI_Comm *comm) {
47   PetscFunctionBegin;
48   PetscValidHeader(obj, 1);
49   PetscValidPointer(comm, 2);
50   *comm = obj->comm;
51   PetscFunctionReturn(0);
52 }
53 
54 /*@
55    PetscObjectGetTabLevel - Gets the number of tabs that `PETSCVIEWERASCII` output for that object uses
56 
57    Not Collective
58 
59    Input Parameter:
60 .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. Thus must be
61          cast with a (`PetscObject`), for example,
62          `PetscObjectGetTabLevel`((`PetscObject`)mat,&tab);
63 
64    Output Parameter:
65 .   tab - the number of tabs
66 
67    Level: developer
68 
69     Note:
70     This is used to manage the output from options that are embedded in other objects. For example
71       the `KSP` object inside a `SNES` object. By indenting each lower level further the hierarchy of objects
72       is very clear.
73 
74 .seealso: `PetscObjectIncrementTabLevel()`, `PETSCVIEWERASCII`
75 @*/
76 PetscErrorCode PetscObjectGetTabLevel(PetscObject obj, PetscInt *tab) {
77   PetscFunctionBegin;
78   PetscValidHeader(obj, 1);
79   PetscValidIntPointer(tab, 2);
80   *tab = obj->tablevel;
81   PetscFunctionReturn(0);
82 }
83 
84 /*@
85    PetscObjectSetTabLevel - Sets the number of tabs that `PETSCVIEWERASCII` output for that object uses
86 
87    Not Collective
88 
89    Input Parameters:
90 +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. Thus must be
91          cast with a (`PetscObject`), for example,
92          `PetscObjectSetTabLevel`((`PetscObject`)mat,tab;
93 -   tab - the number of tabs
94 
95    Level: developer
96 
97     Notes:
98     this is used to manage the output from options that are embedded in other objects. For example
99       the `KSP` object inside a `SNES` object. By indenting each lower level further the hierarchy of objects
100       is very clear.
101 
102     `PetscObjectIncrementTabLevel()` is the preferred API
103 
104 .seealso: `PetscObjectIncrementTabLevel()`
105 @*/
106 PetscErrorCode PetscObjectSetTabLevel(PetscObject obj, PetscInt tab) {
107   PetscFunctionBegin;
108   PetscValidHeader(obj, 1);
109   obj->tablevel = tab;
110   PetscFunctionReturn(0);
111 }
112 
113 /*@
114    PetscObjectIncrementTabLevel - Increments the number of tabs that `PETSCVIEWERASCII` output for that object use based on
115          the tablevel of another object. This should be called immediately after the object is created.
116 
117    Not Collective
118 
119    Input Parameters:
120 +  obj - any PETSc object where we are changing the tab
121 .  oldobj - the object providing the tab
122 -  tab - the increment that is added to the old objects tab
123 
124    Level: developer
125 
126     Note:
127     this is used to manage the output from options that are embedded in other objects. For example
128       the `KSP` object inside a `SNES` object. By indenting each lower level further the hierarchy of objects
129       is very clear.
130 
131 .seealso: `PETSCVIEWERASCII`, `PetscObjectSetTabLevel()`, `PetscObjectGetTabLevel()`
132 @*/
133 PetscErrorCode PetscObjectIncrementTabLevel(PetscObject obj, PetscObject oldobj, PetscInt tab) {
134   PetscFunctionBegin;
135   PetscValidHeader(obj, 1);
136   if (oldobj) PetscValidHeader(oldobj, 2);
137   obj->tablevel = (oldobj ? oldobj->tablevel : 0) + tab;
138   PetscFunctionReturn(0);
139 }
140