Dear PETSc developers,
I have a few questions about building a non-trivial custom SNES and attaching
data to it. In particular, I want to do this in such a way that my SNES can be
used as a smoother in FAS. The user-spectic data is level dependent, but can be
systematically constructed from the fine level data.
Apologies for the long email, but I thought the best way of explaining this is
to write it all down very explicitly. Let me also try to split this into three
parts:
==== Part I: SNES ====
My non-linear problem is defined by a vector event_counts and two matrices
Q_prec (size N x N) and B_meas (size N x m). These three quantities are
collected in a single struct:
typedef struct {
Vec event_counts;
Mat Q_prec;
Mat B_meas;
} PoissonGibbsCtx;
There is also a right hand side which is represented by a nested vector b =
(f_rhs,nu) where f_rhs is of size N and nu of size m.
In addition to this, the SNES has a struct which is used as a workspace:
typedef struct {
PetscRandom prand; // Random numbers
Vec random_workspace;
PetscInt random_work_ptr;
PetscInt sample_index;
PetscInt its;
} SNES_PoissonGibbs;
The function F(y) associated with the SNES is always the same (but it depends
on the user-defined Q_prec, B_meas). More specifically, F takes in a vector y
and returns a nested vector (Q_prec y, B_meas^T y), which has the same shape as
b, so it can be used to form a residual, which is also a nested vector. For the
moment, please don’t be surprised that the function is linear - I’m not
actually solving F(x)=b, I will just need exactly this function in the FAS
later to construct the coarse level RHS. I implemented F(theta) as a function
SNESPoissonGibbs_Function(), see appendix at the very end of this email for
details. I set the function F when setting up the SNES with
SNESCreate_PoissonGibbs(), again see appendix for details.
I also have a function SNESSample_PoissonGibbs(SNES snes) which implements the
snes->ops->solve operation, namely the update x^{n} -> x^{n+1}. This function
also uses the user context, i.e. it does something like this:
PetscCall(SNESGetApplicationContext(snes, &ctx));
Q_prec = ctx->Q_prec;
B_meas = ctx->B_meas;
// Use Q_prec and B_meas
In my main program I then set the user-context: after constructing the snes,
the vector event_counts and the matrices Q_prec and B_meas I do something like
this:
PoissonGibbsCtx ctx;
ctx.event_counts = event_counts;
ctx.Q_prec = Q_prec;
ctx.B_meas = B_meas;
PetscCall(SNESSetApplicationContext(snes, &ctx));
Then I call solve with my custom SNES and a user-defined RHS (see
https://urldefense.us/v3/__https://github.com/nilsfriess/ParMGMC/blob/poisson_gibbs/examples/ex14.c__;!!G_uCfscf7eWS!aW6fnnzRG0fOIvOKu8s-q5TkbUE19gUhp04srllobrxsMJpyOmBvUlHzXMwUoSG7nrkDMim3At_qzuNO7DXVyg$
for the full main program).. My first questions are:
* Is this a sensible way of associating user-data with my SNES?
* Is separating this into two structs PoissonGibbsCtx (user data) and
SNES_PoissonGibbs (workspace) the right approach?
I got this all working, including Python binding for easy setting of the user
context, and also have done some basic tests, so in principle the code does
run, but I want to make sure that I’m using a sensible architecture consistent
with the PETSc design philosophy, not some strange hack.
==== Part II: SNES as a FAS smoother ====
Now, what I really want to do is use my SNES as a smoother inside a FAS (see
Part III for the why). For this I need:
1. Intergrid operators P (prolongation), R (restriction) and hat(R)
(interpolation).
2. Q_prec, B_meas and event_counts on all levels.
Mathematically, I want to construct these as follows:
Start with Q_prec and B_meas on the finest level. Then use AMG (e.g. GAMS) with
the fine stiffness matrix Q_prec to obtain the prolongation P on all levels.
The restriction is given by the nested matrix R = [[P^T, 0],[0,Id]] (recall
that my RHS and therefore also the residual is a nested vector_. If I then set
A = [Q_prec, B_meas^T] I have A^c = [Q_prec^c, B_meas^{c,T}] = R A P with
Q_prec^c = P^T.Q_prec.P, B_meas^c = P^T B_meas, which is exactly what I need,
and this is also consistent with the function F(y), i.e. it gives me exactly
the coarse level RHS I need for the FAS.
The vector event_counts is the same on all levels, so this is easy.
My questions are now:
* How do I tell SNESFAS about all this?
* My plan was to set up a linear solver with Q_prec as stillness matrix with
GAMG as a PC, and then extract the prolongation P from this somehow. Is this
sensible or is there a better way?
* How do I then construct and pass the restriction, which is a nested matrix,
to the SNESFAS?
* How do I set the SNES interpolation, and is there a way of constructing a
“natural” interpolation which is somehow consistent with the
prolongation/restriction? (I know the different between interpolation and
restriction is that the first acts on primal vectors, the latter on dual
vectors).
* The smoothers on each level of my hierarchy need the correct PoissonGibbsCtx,
how do I set these? After constructing the SNESFAS, should I manually create a
list of PoissonGibbsCtx objects, loop over the smoothers of the SNESFAS and set
the context with SNESGetApplicationContext(snes, ctx)? Or is there a way of
doing this more naturally, for example by defining a “coarsen” function for
each smoother?
Again, I mainly want to make sure that I’m not hacking something together but
use the appropriate PETSc data structures.
==== Part III: Motivation ====
I’m doing all this to implement Multigrid Monte Carlo (MGMC) [1] for a
non-Gaussian posterior distribution, namely the Cox process with a log-
Gaussian prior and Poisson noise [2]. Nils wrote a PETSc implementation for
sampling from Gaussian distributions, and he did this by turning PC’s into
samplers. Initially, I also tried to do this (by writing a custom PC with
bespoke ApplyRichardson for the update y^n -> y^{n+1}), but for non-Gaussian
processes the correct approach is to use non-linear solvers i.e. SNES. It turns
out that mathematically drawing one new MGMC sample is exactly equivalent to a
SNESFAS V-cycle, so really I just need to plug it all together in PETSc. All I
need to do is inject some random noise in the smoother on each level, and this
is exactly what my custom SNES (see above) does.
Thank you very much for any help or suggestions with this.
Eike
==== References ====
[1] Goodman, J. and Sokal, A.D., 1989. Multigrid monte carlo method. conceptual
foundations. Physical Review D, 40(6), p.2035.
[2] Diggle, P.J., Tawn, J.A. and Moyeed, R.A., 1998. Model-based geostatistics.
Journal of the Royal Statistical Society Series C: Applied Statistics, 47(3),
pp.299-350.
==== Appendix: detailed function definitions ====
See full code in
https://urldefense.us/v3/__https://github.com/nilsfriess/ParMGMC/blob/poisson_gibbs/src/snes_poissongibbs.c__;!!G_uCfscf7eWS!aW6fnnzRG0fOIvOKu8s-q5TkbUE19gUhp04srllobrxsMJpyOmBvUlHzXMwUoSG7nrkDMim3At_qzuOUVFFCJQ$
static PetscErrorCode SNESPoissonGibbs_Function(SNES snes, Vec y, Vec b, void*
ctx)
{
Vec f_rhs;
Vec nu;
PoissonGibbsCtx* poissongibbs = (PoissonGibbsCtx*)ctx;
PetscFunctionBeginUser;
PetscCall(VecNestGetSubVec(b, 0, &f_rhs));
PetscCall(VecNestGetSubVec(b, 1, &nu));
PetscCall(MatMult(poissongibbs->Q_prec,y,f_rhs));
PetscCall(MatMultTranspose(poissongibbs->B_meas,y,nu));
PetscFunctionReturn(PETSC_SUCCESS);
}
PetscErrorCode SNESCreate_PoissonGibbs(SNES snes)
{
SNES_PoissonGibbs* poissongibbs;
PoissonGibbsCtx* ctx;
PetscFunctionBeginUser;
PetscCall(PetscNew(&poissongibbs));
snes->data = (void*)poissongibbs;
snes->ops->solve = SNESSample_PoissonGibbs;
snes->ops->destroy = SNESDestroy_PoissonGibbs;
snes->ops->reset = SNESReset_PoissonGibbs;
snes->ops->setup = SNESSetUp_PoissonGibbs;
snes->ops->setfromoptions = SNESSetFromOptions_PoissonGibbs;
snes->ops->view = SNESView_PoissonGibbs;
snes->usesksp = PETSC_FALSE;
snes->usesnpc = PETSC_FALSE;
PetscCall(SNESGetApplicationContext(snes, &ctx));
PetscCall(SNESSetFunction(snes, NULL, SNESPoissonGibbs_Function, ctx));
PetscFunctionReturn(PETSC_SUCCESS);
}