1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
# How the Solvers Handle User Provided Callbacks
The solver objects in PETSc, `KSP` (optionally), `SNES`, and `TS`
require user provided callback functions (and contexts for the
functions) that define the problem to be solved. These functions are
supplied by the user with calls such as `SNESSetFunction(SNES,...)`
and `TSSetRHSFunction(TS,...)`. One would naturally think that the
functions provided would be attached to the appropriate solver object,
that is, that the SNES callbacks would be attached to the `SNES`
object and `TS` callbacks to the `TS` object. This is not the case.
Or possibly one might think the callbacks would be attached to the
`DM` object associated with the solver object. This is also not the
case. Rather, the callback functions are attached to an inner nonpublic
`DMXXX` object (`XXX` is `KSP`, `SNES`, or `TS`) that is
attached to the `DM` that is attached to the `XXX` solver object.
This convoluted design is to support multilevel and multidomain solvers
where different levels and different domains may (or may not) share the
same callback function or callback context. You can control exactly what
`XXX`/`DM` objects share a common `DMXXX` object.
:::{figure} /images/developers/callbacks1.svg
:name: fig_callbacks1
Three levels of KSP/DM share the same DMKSP
:::
In the preceding figure, we depict how three levels of `KSP`
objects share a common `DMKSP` object. The code to access the inner
`DMKSP` object is
```
DM dm_2;
DMKSP dmksp;
KSPGetDM(ksp_2,&dm_2);
DMGetDMKSP(dm_2,&dmksp);
```
To obtain a new DMKSP object for which you can change the callback
functions (or their contexts) without affecting the original DMKSP, call
```
DM dm_2;
DMKSP dmksp;
KSPGetDM(ksp_2,&dm_2);
DMGetDMKSPWrite(dm_2,&dmksp_2);
```
This results in the object organization as indicated in the following figure
:::{figure} /images/developers/callbacks2.svg
:name: fig_callbacks2
Two levels of KSP/DM share the same DMKSP; one has its own private copy
:::
The `DMKSP` object is essentially the list of callback functions and
their contexts, for example,
```
typedef struct _p_DMKSP *DMKSP;
typedef struct _DMKSPOps *DMKSPOps;
struct _DMKSPOps {
PetscErrorCode (*computeoperators)(KSP,Mat,Mat,void*);
PetscErrorCode (*computerhs)(KSP,Vec,void*);
PetscErrorCode (*computeinitialguess)(KSP,Vec,void*);
PetscErrorCode (*destroy)(DMKSP*);
PetscErrorCode (*duplicate)(DMKSP,DMKSP);
};
struct _p_DMKSP {
PETSCHEADER(struct _DMKSPOps);
void *operatorsctx;
void *rhsctx;
void *initialguessctx;
void *data;
DM originaldm;
void (*fortran_func_pointers[3])(void); /* Store our own function pointers so they are associated with the DMKSP instead of the DM */
};
```
We now explore in more detail exactly how the solver calls set by the
user are passed down to the inner `DMKSP` object. For each user level
solver routine for setting a callback a similar routine exists at the
`DM` level. Thus, `XXXSetY(XXX,...)` has a routine
`DMXXXSetY(DM,...)`.
```
PetscErrorCode KSPSetComputeOperators(KSP ksp,PetscErrorCode (*func)(KSP,Mat,Mat,void*),void *ctx)
{
DM dm;
PetscFunctionBegin;
PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
PetscCall(KSPGetDM(ksp,&dm));
PetscCall(DMKSPSetComputeOperators(dm,func,ctx));
if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX;
PetscFunctionReturn(PETSC_SUCCESS);
}
```
The implementation of `DMXXXSetY(DM,...)` gets a “writable” version of
the `DMXXX` object via `DMGetDMXXXWrite(DM,DMXXX*)` and sets the
function callback and its context into the `DMXXX` object.
```
PetscErrorCode DMKSPSetComputeOperators(DM dm,PetscErrorCode (*func)(KSP,Mat,Mat,void*),void *ctx)
{
DMKSP kdm;
PetscFunctionBegin;
PetscValidHeaderSpecific(dm,DM_CLASSID,1);
PetscCall(DMGetDMKSPWrite(dm,&kdm));
if (func) kdm->ops->computeoperators = func;
if (ctx) kdm->operatorsctx = ctx;
PetscFunctionReturn(PETSC_SUCCESS);
}
```
The routine for `DMGetDMXXXWrite(DM,DMXXX*)` entails a duplication of
the object unless the `DM` associated with the `DMXXX` object is the
original `DM` that the `DMXXX` object was created with. This can be
seen in the following code.
```
PetscErrorCode DMGetDMKSPWrite(DM dm,DMKSP *kspdm)
{
DMKSP kdm;
PetscFunctionBegin;
PetscValidHeaderSpecific(dm,DM_CLASSID,1);
PetscCall(DMGetDMKSP(dm,&kdm));
if (!kdm->originaldm) kdm->originaldm = dm;
if (kdm->originaldm != dm) { /* Copy on write */
DMKSP oldkdm = kdm;
PetscCall(PetscInfo(dm,"Copying DMKSP due to write\n"));
PetscCall(DMKSPCreate(PetscObjectComm((PetscObject)dm),&kdm));
PetscCall(DMKSPCopy(oldkdm,kdm));
PetscCall(DMKSPDestroy((DMKSP*)&dm->dmksp));
dm->dmksp = (PetscObject)kdm;
kdm->originaldm = dm;
}
*kspdm = kdm;
PetscFunctionReturn(PETSC_SUCCESS);
}
```
The routine `DMGetDMXXX(DM,DMXXX*)` has the following form.
```
PetscErrorCode DMGetDMKSP(DM dm,DMKSP *kspdm)
{
PetscFunctionBegin;
PetscValidHeaderSpecific(dm,DM_CLASSID,1);
*kspdm = (DMKSP) dm->dmksp;
if (!*kspdm) {
PetscCall(PetscInfo(dm,"Creating new DMKSP\n"));
PetscCall(DMKSPCreate(PetscObjectComm((PetscObject)dm),kspdm));
dm->dmksp = (PetscObject) *kspdm;
(*kspdm)->originaldm = dm;
PetscCall(DMCoarsenHookAdd(dm,DMCoarsenHook_DMKSP,NULL,NULL));
PetscCall(DMRefineHookAdd(dm,DMRefineHook_DMKSP,NULL,NULL));
}
PetscFunctionReturn(PETSC_SUCCESS);
}
```
This routine uses `DMCoarsenHookAdd()` and `DMRefineHookAdd()` to
attach to the `DM` object two functions that are automatically called
when the object is coarsened or refined. The hooks
`DMCoarsenHook_DMXXX()` and `DMRefineHook_DMXXX()` have the same form:
```
static PetscErrorCode DMCoarsenHook_DMKSP(DM dm,DM dmc,void *ctx)
{
PetscFunctionBegin;
PetscCall(DMCopyDMKSP(dm,dmc));
PetscFunctionReturn(PETSC_SUCCESS);
}
```
where
```
PetscErrorCode DMCopyDMKSP(DM dmsrc,DM dmdest)
{
PetscFunctionBegin;
PetscValidHeaderSpecific(dmsrc,DM_CLASSID,1);
PetscValidHeaderSpecific(dmdest,DM_CLASSID,2);
PetscCall(DMKSPDestroy((DMKSP*)&dmdest->dmksp));
dmdest->dmksp = dmsrc->dmksp;
PetscCall(PetscObjectReference(dmdest->dmksp));
PetscCall(DMCoarsenHookAdd(dmdest,DMCoarsenHook_DMKSP,NULL,NULL));
PetscCall(DMRefineHookAdd(dmdest,DMRefineHook_DMKSP,NULL,NULL));
PetscFunctionReturn(PETSC_SUCCESS);
}
```
ensures that the new `DM` shares the same `DMXXX` as the parent
`DM` and also inherits the hooks if it is refined or coarsened.
If you provide callbacks to a solver *after* the `DM` associated with
a solver has been refined or coarsened, those child `DM`s will not
share a common `DMXXX`.
The `TS` object manages its callback functions in a way similar to
`KSP` and `SNES`, although there are no multilevel `TS`
implementations so in theory the `DMTS` object is currently unneeded.
|