[petsc-users] PETSc MatMFFDSetFunction Function
Barry Smith
bsmith at mcs.anl.gov
Wed Feb 9 18:07:12 CST 2011
Gaetan,
This is not normally used by users since most people work with SNES and SNES provides a simple wrapper that uses it with the nonlinear function your provide to SNES. If you are writing your own Newton's method and using KSP we recommend instead that you use SNES to handle the Newton's method for you.
Anyways from Fortran the calling sequence is myfunction(void *ctx,Vec x,Vec y, integer ierr) where ierr is where you put 0 or an error code if you detect an error in your routine. You can pass PETSC_OBJECT_NULL as ctx and just no use it or you can pass an array or other Fortran thing that contains information that you wish to use in your function evaluation.
If it still crashes after you get the right calling sequence you can use -start_in_debugger to see why it is crashing and quickly resolve the problem.
Barry
On Feb 9, 2011, at 5:43 PM, Gaetan Kenway wrote:
> Hello
>
> I was wondering what the user supplied function is supposed to look like for setting the function in MatMFFDSetFunction. I am trying to use a Matrix-Free Matrix for a linear Krylov Solver. The website says:
> PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunction(Mat mat,PetscErrorCode (*func)(void*,Vec,Vec),void *funcctx)
>
> This indicates the function should have the calling sequence: (void *,Vec,Vec). Since there are zero examples of actually using this function, what exactly is the sequence? I gather that the second and third arguments are Vec x and Vec y where x is the input and y is the output, but what is the void * supposed to be.
>
> I'm doing this in Fortran, so I really don't know what argument "void *" should correspond to?
>
> Currently my code looks like this:
>
> ! Setup Matrix-Free dRdw matrix
> call MatCreateMFFD(sumb_comm_world,nDimW,nDimW,&
> PETSC_DETERMINE,PETSC_DETERMINE,dRdw,ierr)
>
> call MatMFFDSetFunction(dRdw,FormFunction2,ctx,ierr)
> call MatAssemblyBegin(dRdw,MAT_FINAL_ASSEMBLY,ierr)
> call MatAssemblyEnd(dRdw,MAT_FINAL_ASSEMBLY,ierr)
>
> The function prototype for FormFunction2 is:
>
> subroutine FormFunction2(mfmat,wVec,rVec,ierr)
> Mat mfmat
> Vec wVec, rVec
> PetscInt ierr
> end subroutine FormFunction2
>
> When I try to use this in a KSP linear solve I get the following tracsback:
>
> [0]PETSC ERROR: ------------------------------------------------------------------------
> [0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
> [0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
> [0]PETSC ERROR: or see http://www.mcs.anl.gov/petsc/petsc-as/documentation/troubleshooting.html#Signal[0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple Mac OS X to find memory corruption errors
> [0]PETSC ERROR: likely location of problem given in stack below
> [0]PETSC ERROR: --------------------- Stack Frames ------------------------------------
> [0]PETSC ERROR: Note: The EXACT line numbers in the stack are not available,
> [0]PETSC ERROR: INSTEAD the line number of the start of the function
> [0]PETSC ERROR: is given.
> [0]PETSC ERROR: [0] MatMult line 1877 src/mat/interface/matrix.c
> [0]PETSC ERROR: [0] PCApplyBAorAB line 540 src/ksp/pc/interface/precon.c
> [0]PETSC ERROR: [0] GMREScycle line 132 src/ksp/ksp/impls/gmres/gmres.c
> [0]PETSC ERROR: [0] KSPSolve_GMRES line 227 src/ksp/ksp/impls/gmres/gmres.c
>
> If I use KSPPREONLY it works fine.
>
> Thank you,
>
> Gaetan
More information about the petsc-users
mailing list