Discussion:
[petsc-users] on SNESSetJacobian semantics
Marco Zocca
2015-10-22 14:20:35 UTC
Permalink
The signature of SNESSetJacobian is

PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode
(*J)(SNES,Vec,Mat,Mat,void*),void *ctx)

however it would seem redundant to supply both a constant matrix and a
function to compute the Jacobian.
The manual says of J "(if NULL then SNES retains any previously set value":
this would only apply to linear functions, is this correct?

Why then are there not two monomorphic "SetJacobian" functions, one for
linear maps that wouldn't require recomputation and one for every other
case ?

Coming from a functional background, I find reasoning with "NULL" to be
very error-prone.

Thank you,
Marco
Matthew Knepley
2015-10-22 14:30:59 UTC
Permalink
Post by Marco Zocca
The signature of SNESSetJacobian is
PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode
(*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
however it would seem redundant to supply both a constant matrix and a
function to compute the Jacobian.
The manual says of J "(if NULL then SNES retains any previously set
value": this would only apply to linear functions, is this correct?
It means, if you give NULL, but have previously called SetJacobian() then
it uses the function pointer previously passed
rather than setting it to NULL.

Matt
Post by Marco Zocca
Why then are there not two monomorphic "SetJacobian" functions, one for
linear maps that wouldn't require recomputation and one for every other
case ?
Coming from a functional background, I find reasoning with "NULL" to be
very error-prone.
Thank you,
Marco
--
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
Marco Zocca
2015-10-22 14:38:29 UTC
Permalink
Thank you, but this is not really helping;

why are there two slots to supply the Jacobian of the same function, i.e.
Amat and J ?
Post by Matthew Knepley
Post by Marco Zocca
The signature of SNESSetJacobian is
PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat
Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
however it would seem redundant to supply both a constant matrix and a
function to compute the Jacobian.
The manual says of J "(if NULL then SNES retains any previously set
value": this would only apply to linear functions, is this correct?
It means, if you give NULL, but have previously called SetJacobian() then
it uses the function pointer previously passed
rather than setting it to NULL.
Matthew Knepley
2015-10-22 14:40:04 UTC
Permalink
Post by Marco Zocca
Thank you, but this is not really helping;
why are there two slots to supply the Jacobian of the same function, i.e.
Amat and J ?
Amat is just the storage, not the values. J computes the values.

Matt
Post by Marco Zocca
Marco
Post by Matthew Knepley
Post by Marco Zocca
The signature of SNESSetJacobian is
PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat
Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
however it would seem redundant to supply both a constant matrix and a
function to compute the Jacobian.
The manual says of J "(if NULL then SNES retains any previously set
value": this would only apply to linear functions, is this correct?
It means, if you give NULL, but have previously called SetJacobian() then
it uses the function pointer previously passed
rather than setting it to NULL.
Matt
Post by Marco Zocca
Why then are there not two monomorphic "SetJacobian" functions, one for
linear maps that wouldn't require recomputation and one for every other
case ?
Coming from a functional background, I find reasoning with "NULL" to be
very error-prone.
Thank you,
Marco
--
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
--
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
Jed Brown
2015-10-22 15:46:24 UTC
Permalink
Post by Matthew Knepley
Post by Marco Zocca
Thank you, but this is not really helping;
why are there two slots to supply the Jacobian of the same function, i.e.
Amat and J ?
Amat is just the storage, not the values. J computes the values.
More precisely, Amat provides the action of the Jacobian (possibly
matrix-free).

Pmat provides the data from which to construct the preconditioner. This
is most frequently an assembled matrix (possibly from a simpler
discretization, skipping some terms, etc.), but could hold additional
information or eschew matrix entries entirely.
Barry Smith
2015-10-22 18:33:45 UTC
Permalink
Post by Marco Zocca
The signature of SNESSetJacobian is
PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
however it would seem redundant to supply both a constant matrix and a function to compute the Jacobian.
The manual says of J "(if NULL then SNES retains any previously set value": this would only apply to linear functions, is this correct?
Why then are there not two monomorphic "SetJacobian" functions, one for linear maps that wouldn't require recomputation and one for every other case ?
For the linear case you can just pass in a function J that simply returns without changing the matrices.

Barry
Post by Marco Zocca
Coming from a functional background, I find reasoning with "NULL" to be very error-prone.
Agreed. Limitation of C and the style of API we selected of trying to minimize the number of different functions in the interface.
Post by Marco Zocca
Thank you,
Marco
Loading...