xref: /petsc/src/sys/objects/state.c (revision 77d968b72e8e27b79bcc994c018975de390644ed)
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    PetscObjectStateGet - Gets the state of any PetscObject,
9    regardless of the type.
10 
11    Not Collective
12 
13    Input Parameter:
14 .  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
15          cast with a (PetscObject), for example,
16          PetscObjectStateGet((PetscObject)mat,&state);
17 
18    Output Parameter:
19 .  state - the object state
20 
21    Notes:
22     object state is an integer which gets increased every time
23    the object is changed. By saving and later querying the object state
24    one can determine whether information about the object is still current.
25    Currently, state is maintained for Vec and Mat objects.
26 
27    Level: advanced
28 
29 .seealso: `PetscObjectStateIncrease()`, `PetscObjectStateSet()`
30 
31 @*/
32 PetscErrorCode PetscObjectStateGet(PetscObject obj,PetscObjectState *state)
33 {
34   PetscFunctionBegin;
35   PetscValidHeader(obj,1);
36   PetscValidIntPointer(state,2);
37   *state = obj->state;
38   PetscFunctionReturn(0);
39 }
40 
41 /*@C
42    PetscObjectStateSet - Sets the state of any PetscObject,
43    regardless of the type.
44 
45    Logically Collective
46 
47    Input Parameters:
48 +  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
49          cast with a (PetscObject), for example,
50          PetscObjectStateSet((PetscObject)mat,state);
51 -  state - the object state
52 
53    Notes:
54     This function should be used with extreme caution. There is
55    essentially only one use for it: if the user calls Mat(Vec)GetRow(Array),
56    which increases the state, but does not alter the data, then this
57    routine can be used to reset the state.  Such a reset must be collective.
58 
59    Level: advanced
60 
61 .seealso: `PetscObjectStateGet()`, `PetscObjectStateIncrease()`
62 
63 @*/
64 PetscErrorCode PetscObjectStateSet(PetscObject obj,PetscObjectState state)
65 {
66   PetscFunctionBegin;
67   PetscValidHeader(obj,1);
68   obj->state = state;
69   PetscFunctionReturn(0);
70 }
71 
72 PetscInt PetscObjectComposedDataMax = 10;
73 
74 /*@C
75    PetscObjectComposedDataRegister - Get an available id for composed data
76 
77    Not Collective
78 
79    Output parameter:
80 .  id - an identifier under which data can be stored
81 
82    Level: developer
83 
84    Notes:
85    You must keep this value (for example in a global variable) in order to attach the data to an object or access in an object.
86 
87    `PetscObjectCompose()` and  `PetscObjectQuery()` provide a way to attach any data to an object
88 
89 .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
90           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
91           `PetscObjectCompose()`,  `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataGetScalarstar()`,
92           `PetscObjectComposedDataSetScalarstar()`, `PetscObjectComposedDataSetScalarstar()`
93 @*/
94 PetscErrorCode PetscObjectComposedDataRegister(PetscInt *id)
95 {
96   static PetscInt globalcurrentstate = 0;
97 
98   PetscFunctionBegin;
99   *id = globalcurrentstate++;
100   if (globalcurrentstate > PetscObjectComposedDataMax) PetscObjectComposedDataMax += 10;
101   PetscFunctionReturn(0);
102 }
103 
104 PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject obj)
105 {
106   PetscInt         *ar = obj->intcomposeddata,*new_ar,n = obj->int_idmax,new_n;
107   PetscObjectState *ir = obj->intcomposedstate,*new_ir;
108 
109   PetscFunctionBegin;
110   new_n = PetscObjectComposedDataMax;
111   PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir));
112   PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscInt)));
113   PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState)));
114   PetscCall(PetscFree2(ar,ir));
115   obj->int_idmax       = new_n;
116   obj->intcomposeddata = new_ar; obj->intcomposedstate = new_ir;
117   PetscFunctionReturn(0);
118 }
119 
120 PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject obj)
121 {
122   PetscInt         **ar = obj->intstarcomposeddata,**new_ar,n = obj->intstar_idmax,new_n;
123   PetscObjectState *ir  = obj->intstarcomposedstate,*new_ir;
124 
125   PetscFunctionBegin;
126   new_n = PetscObjectComposedDataMax;
127   PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir));
128   PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscInt*)));
129   PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState)));
130   PetscCall(PetscFree2(ar,ir));
131   obj->intstar_idmax        = new_n;
132   obj->intstarcomposeddata  = new_ar;
133   obj->intstarcomposedstate = new_ir;
134   PetscFunctionReturn(0);
135 }
136 
137 PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject obj)
138 {
139   PetscReal        *ar = obj->realcomposeddata,*new_ar;
140   PetscObjectState *ir = obj->realcomposedstate,*new_ir;
141   PetscInt         n   = obj->real_idmax,new_n;
142 
143   PetscFunctionBegin;
144   new_n = PetscObjectComposedDataMax;
145   PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir));
146   PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscReal)));
147   PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState)));
148   PetscCall(PetscFree2(ar,ir));
149   obj->real_idmax       = new_n;
150   obj->realcomposeddata = new_ar; obj->realcomposedstate = new_ir;
151   PetscFunctionReturn(0);
152 }
153 
154 PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject obj)
155 {
156   PetscReal        **ar = obj->realstarcomposeddata,**new_ar;
157   PetscObjectState *ir  = obj->realstarcomposedstate,*new_ir;
158   PetscInt         n    = obj->realstar_idmax,new_n;
159 
160   PetscFunctionBegin;
161   new_n = PetscObjectComposedDataMax;
162   PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir));
163   PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscReal*)));
164   PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState)));
165   PetscCall(PetscFree2(ar,ir));
166   obj->realstar_idmax       = new_n;
167   obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir;
168   PetscFunctionReturn(0);
169 }
170 
171 PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject obj)
172 {
173   PetscScalar      *ar = obj->scalarcomposeddata,*new_ar;
174   PetscObjectState *ir = obj->scalarcomposedstate,*new_ir;
175   PetscInt         n   = obj->scalar_idmax,new_n;
176 
177   PetscFunctionBegin;
178   new_n = PetscObjectComposedDataMax;
179   PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir));
180   PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar)));
181   PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState)));
182   PetscCall(PetscFree2(ar,ir));
183   obj->scalar_idmax       = new_n;
184   obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir;
185   PetscFunctionReturn(0);
186 }
187 
188 PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject obj)
189 {
190   PetscScalar      **ar = obj->scalarstarcomposeddata,**new_ar;
191   PetscObjectState *ir  = obj->scalarstarcomposedstate,*new_ir;
192   PetscInt         n    = obj->scalarstar_idmax,new_n;
193 
194   PetscFunctionBegin;
195   new_n = PetscObjectComposedDataMax;
196   PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir));
197   PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar*)));
198   PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState)));
199   PetscCall(PetscFree2(ar,ir));
200   obj->scalarstar_idmax       = new_n;
201   obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir;
202   PetscFunctionReturn(0);
203 }
204 
205 /*@
206    PetscObjectGetId - get unique object ID
207 
208    Not Collective
209 
210    Input Parameter:
211 .  obj - object
212 
213    Output Parameter:
214 .  id - integer ID
215 
216    Level: developer
217 
218    Notes:
219    The object ID may be different on different processes, but object IDs are never reused so local equality implies global equality.
220 
221 .seealso: `PetscObjectStateGet()`, `PetscObjectCompareId()`
222 @*/
223 PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id)
224 {
225   PetscFunctionBegin;
226   *id = obj->id;
227   PetscFunctionReturn(0);
228 }
229 
230 /*@
231    PetscObjectCompareId - compares the objects ID with a given id
232 
233    Not Collective
234 
235    Input Parameters:
236 +  obj - object
237 -  id - integer ID
238 
239    Output Parameter;
240 .  eq - the ids are equal
241 
242    Level: developer
243 
244    Notes:
245    The object ID may be different on different processes, but object IDs are never reused so local equality implies global equality.
246 
247 .seealso: `PetscObjectStateGet()`, `PetscObjectGetId()`
248 @*/
249 PetscErrorCode PetscObjectCompareId(PetscObject obj,PetscObjectId id,PetscBool *eq)
250 {
251   PetscFunctionBegin;
252   *eq = (id == obj->id) ? PETSC_TRUE : PETSC_FALSE;
253   PetscFunctionReturn(0);
254 }
255