File: callbacks.rst.txt

package info (click to toggle)
petsc 3.14.5%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 266,472 kB
  • sloc: ansic: 680,898; python: 33,303; cpp: 16,324; makefile: 14,022; f90: 13,731; javascript: 10,713; fortran: 9,581; sh: 1,373; xml: 619; objc: 445; csh: 192; pascal: 148; java: 13
file content (241 lines) | stat: -rw-r--r-- 8,303 bytes parent folder | download
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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.

.. _fig_dmksp:

.. graphviz ::
    :caption: Three levels of KSP/DM share the same DMKSP

    digraph {
        ksp1[label = "KSP 1"]
        ksp2[label = "KSP 2"]
        ksp3[label = "KSP 3"]
        dm1[label = "DM 1"]
        dm2[label = "DM 2"]
        dm3[label = "DM 3"]
        dmksp[label = "DMKSP"]
        ksp1 -> dm1 -> dmksp
        ksp2 -> dm2 -> dmksp
        ksp3 -> dm3 -> 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

.. graphviz ::
    :caption: Two levels of KSP/DM share the same DMKSP; one has its own private copy

    digraph {
        ksp1[label = "KSP 1"]
        ksp2[label = "KSP 2"]
        ksp3[label = "KSP 3"]
        dm1[label = "DM 1"]
        dm2[label = "DM 2"]
        dm3[label = "DM 3"]
        dmksp[label = "DMKSP"]
        dmksp2[label = "DMKSP"]
        ksp1 -> dm1 -> dmksp
        ksp2 -> dm2 -> dmksp2
        ksp3 -> dm3 -> dmksp
     }

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)
    {
      PetscErrorCode ierr;
      DM             dm;

      PetscFunctionBegin;
      PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
      ierr = KSPGetDM(ksp,&dm);CHKERRQ(ierr);
      ierr = DMKSPSetComputeOperators(dm,func,ctx);CHKERRQ(ierr);
      if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX;
      PetscFunctionReturn(0);
    }

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)
    {
      PetscErrorCode ierr;
      DMKSP          kdm;

      PetscFunctionBegin;
      PetscValidHeaderSpecific(dm,DM_CLASSID,1);
      ierr = DMGetDMKSPWrite(dm,&kdm);CHKERRQ(ierr);
      if (func) kdm->ops->computeoperators = func;
      if (ctx) kdm->operatorsctx = ctx;
      PetscFunctionReturn(0);
    }

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)
    {
      PetscErrorCode ierr;
      DMKSP          kdm;

      PetscFunctionBegin;
      PetscValidHeaderSpecific(dm,DM_CLASSID,1);
      ierr = DMGetDMKSP(dm,&kdm);CHKERRQ(ierr);
      if (!kdm->originaldm) kdm->originaldm = dm;
      if (kdm->originaldm != dm) {  /* Copy on write */
        DMKSP oldkdm = kdm;
        ierr      = PetscInfo(dm,"Copying DMKSP due to write\n");CHKERRQ(ierr);
        ierr      = DMKSPCreate(PetscObjectComm((PetscObject)dm),&kdm);CHKERRQ(ierr);
        ierr      = DMKSPCopy(oldkdm,kdm);CHKERRQ(ierr);
        ierr      = DMKSPDestroy((DMKSP*)&dm->dmksp);CHKERRQ(ierr);
        dm->dmksp = (PetscObject)kdm;
        kdm->originaldm = dm;
      }
      *kspdm = kdm;
      PetscFunctionReturn(0);
    }

The routine ``DMGetDMXXX(DM,DMXXX*)`` has the following form.

::

    PetscErrorCode DMGetDMKSP(DM dm,DMKSP *kspdm)
    {
      PetscErrorCode ierr;

      PetscFunctionBegin;
      PetscValidHeaderSpecific(dm,DM_CLASSID,1);
      *kspdm = (DMKSP) dm->dmksp;
      if (!*kspdm) {
        ierr      = PetscInfo(dm,"Creating new DMKSP\n");CHKERRQ(ierr);
        ierr      = DMKSPCreate(PetscObjectComm((PetscObject)dm),kspdm);CHKERRQ(ierr);
        dm->dmksp = (PetscObject) *kspdm;
        (*kspdm)->originaldm = dm;
        ierr      = DMCoarsenHookAdd(dm,DMCoarsenHook_DMKSP,NULL,NULL);CHKERRQ(ierr);
        ierr      = DMRefineHookAdd(dm,DMRefineHook_DMKSP,NULL,NULL);CHKERRQ(ierr);
      }
      PetscFunctionReturn(0);
    }

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)
    {
      PetscErrorCode ierr;

      PetscFunctionBegin;
      ierr = DMCopyDMKSP(dm,dmc);CHKERRQ(ierr);
      PetscFunctionReturn(0);
    }

where

::

    PetscErrorCode DMCopyDMKSP(DM dmsrc,DM dmdest)
    {
      PetscErrorCode ierr;

      PetscFunctionBegin;
      PetscValidHeaderSpecific(dmsrc,DM_CLASSID,1);
      PetscValidHeaderSpecific(dmdest,DM_CLASSID,2);
      ierr          = DMKSPDestroy((DMKSP*)&dmdest->dmksp);CHKERRQ(ierr);
      dmdest->dmksp = dmsrc->dmksp;
      ierr          = PetscObjectReference(dmdest->dmksp);CHKERRQ(ierr);
      ierr          = DMCoarsenHookAdd(dmdest,DMCoarsenHook_DMKSP,NULL,NULL);CHKERRQ(ierr);
      ierr          = DMRefineHookAdd(dmdest,DMRefineHook_DMKSP,NULL,NULL);CHKERRQ(ierr);
      PetscFunctionReturn(0);
    }

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.