Hi Matt,
Yes, I believe this is a hard limitation. We envision a SNES as locally
inverting some square operator (so that it can be iterated). For a non-square
operator, we would have to know what we meant by that inversion, and (I think)
this would naturally make it an optimization problem for Tao. Is this how you
see it?
That makes sense. I can change my code such that the solution and RHS live in
the same space, but they both need to be nested vectors. Is this possible?
Yes, I believe it should be fine having both as nested vectors. VecDuplicate()
should preserve this.
Appears to work now (in the sense that the code compiles and runs), but I need
to check it carefully to make sure it is really doing the right thing.
Thanks for all your help,
Eike
Thanks,
Matt
Thanks,
Eike
Thanks,
Matt
I can work around this, but it means that I have to extend my solution space.
All the best,
Eike
On 7 Sep 2026, at 15:41, Matthew Knepley
<[email protected]<mailto:[email protected]>> wrote:
CAUTION: This email came from outside of the University. To keep your account
safe, only click on links and open attachments if you know the person who sent
the email, or you expected to receive this communication.
On Fri, Sep 4, 2026 at 5:53 AM Eike Mueller via petsc-users
<[email protected]<mailto:[email protected]>> wrote:
Dear PETSc developers,
I now have a more concrete question. I'm trying to set up my FAS (which is the
ispoissongibbsfas->fas member) as follows:
SNES_PoissonGibbsFAS* poissongibbsfas = (SNES_PoissonGibbsFAS*)snes->data;
PoissonGibbsCtx* ctx;
PetscCall(SNESGetApplicationContext(snes, &ctx));
nlevels = poissongibbsfas->nlevels;
// Set up FAS
PetscCall(SNESSetType(poissongibbsfas->fas, SNESFAS));
PetscCall(SNESFASSetType(poissongibbsfas->fas, SNES_FAS_MULTIPLICATIVE));
PetscCall(SNESFASSetLevels(poissongibbsfas->fas, nlevels, NULL));
// Set the smoothers on all levels
for (PetscInt ell=0;ell<nlevels;++ell) {
SNES snes_level;
Vec solution;
if (ell == 0) {
PetscCall(SNESFASGetCoarseSolve(poissongibbsfas->fas, &snes_level));
} else {
PetscCall(SNESFASGetSmoother(poissongibbsfas->fas, ell, &snes_level));
}
PetscCall(SNESSetType(snes_level, SNESPOISSONGIBBS));
PetscCall(SNESSetApplicationContext(snes_level,
&poissongibbsfas->smoother_ctx[ell]));
}
// Set intergrid operators on all levels
Mat Id;
PetscInt ndof, nobs;
PetscCall(MatGetSize(ctx->B_meas, &ndof, &nobs));
PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD, nobs, nobs,
PETSC_DECIDE, PETSC_DECIDE, 1.0, &Id));
PetscCall(PCGetInterpolations(poissongibbsfas->mg, &nlevels, &P));
for (PetscInt ell=1;ell<nlevels;++ell) {
Mat R;
Mat P_T;
PetscCall(SNESFASSetInterpolation(poissongibbsfas->fas, ell, P[ell-1]));
PetscCall(MatTranspose(P[ell-1], MAT_INITIAL_MATRIX, &P_T));
Mat blocks[4] = {P_T, NULL, NULL, Id};
PetscCall(MatCreateNest(PETSC_COMM_WORLD, 2, NULL, 2, NULL, blocks, &R));
PetscCall(SNESFASSetRestriction(poissongibbsfas->fas, ell, R));
}
PetscCall(SNESSetUp(poissongibbsfas->fas));
Here, poissongibbsfas->mg is the multigrid which I setup earlier. I extract the
intergrid operators from this MG and use it to construct the
prolongation/restriction in the SNESFAS and the coarse level
operators/smoother. poissongibbsfas->mg is of type GAMG. The code compiles, but
I get the following error when running it:
[0]PETSC ERROR: --------------------- Error Message
--------------------------------------------------------------
[0]PETSC ERROR: Must call DMShellSetGlobalVector() or
DMShellSetCreateGlobalVector()
[0]PETSC ERROR: See
https://urldefense.us/v3/__https://petsc.org/release/faq/__;!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMdyNL2_EQ$
<https://urldefense.us/v3/__https://petsc.org/release/faq/__;!!G_uCfscf7eWS!b-kkj2vsT4ALzOct9zFGjve_YXVCgBu08zigHrZPNXgGEKQ2LafosqQZUi4hyIJ1IcXEqCxPs_G5ppvEaSEd9A$>
for trouble shooting.
[0]PETSC ERROR: PETSc Development Git Revision: v3.25.4-522-gab02c5218b8 Git
Date: 2026-08-15 17:25:53 +0000
[0]PETSC ERROR: ./build/examples/ex14 with 1 MPI process(es) and PETSC_ARCH
arch-firedrake-default on eike-linux by eike Fri Sep 4 10:39:14 2026
[0]PETSC ERROR: Configure options: --with-c2html=0 --with-debugging=1
--with-fortran-bindings=0 --with-shared-libraries=1
--with-strict-petscerrorcode PETSC_ARCH=arch-firedrake-default --COPTFLAGS="-O3
-march=native -mtune=native -I/usr/include/hdf5/openmpi -I/usr/include/scotch
-I/usr/include/superlu -I/usr/include/superlu-dist
-L/usr/lib/x86_64-linux-gnu/hdf5/openmpi" --CXXOPTFLAGS="-O3 -march=native
-mtune=native -I/usr/include/hdf5/openmpi -I/usr/include/scotch
-I/usr/include/superlu -I/usr/include/superlu-dist
-L/usr/lib/x86_64-linux-gnu/hdf5/openmpi" --FOPTFLAGS="-O3 -march=native
-mtune=native -I/usr/include/hdf5/openmpi -I/usr/include/scotch
-I/usr/include/superlu -I/usr/include/superlu-dist
-L/usr/lib/x86_64-linux-gnu/hdf5/openmpi" --with-bison --with-fftw --with-hdf5
--with-hwloc --with-metis --with-mumps --with-netcdf --with-pnetcdf
--with-ptscotch --with-scalapack-lib=-lscalapack-openmpi --with-suitesparse
--with-superlu_dist --with-zlib --download-hypre --download-triangle
[0]PETSC ERROR: #1 DMCreateGlobalVector_Shell() at
/home/eike/Software/petsc/src/dm/impls/shell/dmshell.c:210
[0]PETSC ERROR: #2 DMCreateGlobalVector() at
/home/eike/Software/petsc/src/dm/interface/dm.c:1043
[0]PETSC ERROR: #3 SNESSetUp() at
/home/eike/Software/petsc/src/snes/interface/snes.c:3460
[0]PETSC ERROR: #4 setup_fas() at
/home/eike/Work/Bath/git_workspace/ParMGMC/src/snes_poissongibbsfas.c:162
[0]PETSC ERROR: #5 SNESSetUp_PoissonGibbsFAS() at
/home/eike/Work/Bath/git_workspace/ParMGMC/src/snes_poissongibbsfas.c:171
[0]PETSC ERROR: #6 SNESSetUp() at
/home/eike/Software/petsc/src/snes/interface/snes.c:3532
[0]PETSC ERROR: #7 main() at
/home/eike/Work/Bath/git_workspace/ParMGMC/examples/ex14.c:126
[0]PETSC ERROR: PETSc Option Table entries:
[0]PETSC ERROR: -pc_type gamg (source: command line)
[0]PETSC ERROR: -snes_type poissongibbsfas (source: command line)
[0]PETSC ERROR: ----------------End of Error Message -------send entire error
message to [email protected]
I believe this is because the FAS does not know how to create vectors on each
level of the hierarchy. I have tried to call SNESSetSolution() on the SNES
smoother on each level of the hierarchy, and also called SNESSetSolution() for
the SNESFAS itself, but then I get a different error: it complains that it
cannot coarsen the DM. I think this is because SNESSetSolution() will attach a
DM to the SNES, but it then attempts to coarsen this itself and this doesn't
work since the levels are constructed by the AMG in a purely algebraic way.
Hi Eike,
Which SNES is being setup here when it fails?
[0]PETSC ERROR: #4 setup_fas() at
/home/eike/Work/Bath/git_workspace/ParMGMC/src/snes_poissongibbsfas.c:162
You are right that it wants to know how to get a work vector. There are two
choices for setting up a solver in PETSc now. First, you can tell the solver
about a DM that describes the solution space for the problem. Second, you can
just provide vectors. In the second scenario, the solver creates an internal
DMShell, and tells it how big the space is (and how it is distributed in
parallel).
If you want to use the second method, I think you should
SNESGetDM(snes, &dm);
DMShellSetGlobalVector(dm, v);
for the failing SNES, so not other processing takes place. If that does not
work, send me the code I can go through it.
Thanks
Matt
Note that in my example the solution vectors are simply vectors, but the
residuals are nested vectors.
Any suggestions for how I can overcome this issue?
Thank you very much,
Eike
________________________________
From: Eike Mueller <[email protected]<mailto:[email protected]>>
Sent: 02 September 2026 14:48
To: [email protected]<mailto:[email protected]>
<[email protected]<mailto:[email protected]>>
Subject: Some questions about bespoke SNES and SNESFAS for Multigrid Monte
Carlo sampling
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!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMfyer8scw$
<https://urldefense.us/v3/__https://github.com/nilsfriess/ParMGMC/blob/poisson_gibbs/examples/ex14.c__;!!G_uCfscf7eWS!b-kkj2vsT4ALzOct9zFGjve_YXVCgBu08zigHrZPNXgGEKQ2LafosqQZUi4hyIJ1IcXEqCxPs_G5ppvImkcGaA$>
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!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMeEeotRkg$
<https://urldefense.us/v3/__https://github.com/nilsfriess/ParMGMC/blob/poisson_gibbs/src/snes_poissongibbs.c__;!!G_uCfscf7eWS!b-kkj2vsT4ALzOct9zFGjve_YXVCgBu08zigHrZPNXgGEKQ2LafosqQZUi4hyIJ1IcXEqCxPs_G5ppusHlRLag$>
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);
}
--
What most experimenters take for granted before they begin their experiments is
infinitely more interesting than any results to which their experiments lead.
-- Norbert Wiener
https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMfO1MQv4A$
<https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMdxenO9yA$
>
--
What most experimenters take for granted before they begin their experiments is
infinitely more interesting than any results to which their experiments lead.
-- Norbert Wiener
https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMfO1MQv4A$
<https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMdxenO9yA$
>
--
What most experimenters take for granted before they begin their experiments is
infinitely more interesting than any results to which their experiments lead.
-- Norbert Wiener
https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMfO1MQv4A$
<https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!YY4zJb61260voidlNoLixXn45o_4ux83exEXCr-eyxmcuDe-7oD0VDW1rrtMBdldPN-Z-sfgQ55QAMdxenO9yA$
>