Let me also add a minimal example (relying only on petsc), which leads to the same error message on my machine. Again, what I'm trying to do is very simple:

1. MatCreateShell => Initialize Mat :: F
2. MatShellSetContext => set the context of F to ctxF (looking at the
   petsc source code, this call actually seems to be superfluous, but
   nevermind)
3. MatShellGetContext => get the pointer ctxF_pt to point to the matrix
   context

I'm getting an error message in the third step.

[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
[0]PETSC ERROR: Null argument, when expecting valid pointer
[0]PETSC ERROR: Null Pointer: Parameter # 2

Again, thanks for your help!

Cheers,
Samuel


On 12/11/2017 07:41 PM, Samuel Lanthaler wrote:
Dear petsc-/slepc-users,

I have been trying to understand matrix-free/shell matrices in PETSc for eventual use in solving a non-linear eigenvalue problem using SLEPC. But I seem to be having trouble with calls to MatShellGetContext. As far as I understand, this function should initialize a pointer (second argument) so that the subroutine output will point to the context associated with my shell-matrix (let's say of TYPE(MatCtx))? When calling that subroutine, I get the following error message:

[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
[0]PETSC ERROR: Null argument, when expecting valid pointer
[0]PETSC ERROR: Null Pointer: Parameter # 2

In my code, the second input argument to the routine is a null-pointer of TYPE(MatCtx),POINTER :: arg2. Which the error message appears to be unhappy with. I noticed that there is no error message if I instead pass an object TYPE(MatCtx) :: arg2 to the routine... which doesn't really make sense to me? Could someone maybe explain to me what is going on, here?

Just in case, let me also attach my concrete example code (it is supposed to be a Fortran version of the slepc-example in slepc-3.8.1/src/nep/examples/tutorials/ex21.c). I have added an extra call to MatShellGetContext on line 138, after the function and jacobian should supposedly have been set up.

Thanks a lot for your help!

Cheers,

Samuel


! ----------------------------------------------------
!                user-defined context
! ----------------------------------------------------
MODULE solver_context
#include "petsc/finclude/petsc.h"
  ! ------
  USE petscsys
  USE petscmat
  ! ------
  IMPLICIT NONE

  TYPE :: MatCtx
     PetscScalar :: lambda,kappa
     PetscReal :: h
  END TYPE MatCtx

END MODULE solver_context

MODULE solver_context_interfaces
  USE solver_context
  IMPLICIT NONE

  ! ----------------------------------------------------
  INTERFACE MatCreateShell
     ! --------------
     SUBROUTINE MatCreateShell(comm,mloc,nloc,m,n,ctx,mat,ierr)
       USE solver_context
       TYPE(MPI_COMM) :: comm
       PetscInt :: mloc,nloc,m,n
       TYPE(MatCtx) :: ctx
       Mat :: mat
       PetscErrorCode :: ierr
     END SUBROUTINE MatCreateShell
  END INTERFACE MatCreateShell
  ! ----------------------------------------------------

  ! ----------------------------------------------------
  INTERFACE MatShellSetContext
     ! --------------
     SUBROUTINE MatShellSetContext(mat,ctx,ierr)
       USE solver_context
       Mat :: mat
       TYPE(MatCtx) :: ctx
       PetscErrorCode :: ierr
     END SUBROUTINE MatShellSetContext
  END INTERFACE MatShellSetContext
  ! ----------------------------------------------------

  ! ----------------------------------------------------
  INTERFACE MatShellGetContext
     ! --------------
     SUBROUTINE MatShellGetContext(mat,ctx,ierr)
       USE solver_context
       Mat :: mat
       TYPE(MatCtx), POINTER :: ctx
       PetscErrorCode :: ierr
     END SUBROUTINE MatShellGetContext
  END INTERFACE MatShellGetContext
  ! ----------------------------------------------------

END MODULE solver_context_interfaces

! ----------------------------------------------------
!                    main program
! ----------------------------------------------------
PROGRAM main
#include "petsc/finclude/petsc.h"
  ! ------
  USE petscsys
  USE petscmat
  ! ------
  USE solver_context
  IMPLICIT NONE
  Mat :: F
  TYPE(MatCtx) :: ctxF
  TYPE(MatCtx),POINTER :: ctxF_pt
  PetscErrorCode :: ierr
  PetscInt :: n=128
  
  !
  CALL PetscInitialize(PETSC_NULL_CHARACTER,ierr)
  
  !
  ctxF%lambda = 0.0d0
  CALL MatCreateShell(PETSC_COMM_WORLD,n,n,n,n,ctxF,F,ierr)
  CALL MatShellSetContext(F,ctxF,ierr)
  PRINT*,'ctxF%lambda = ',ctxF%lambda

  CALL MatShellGetContext(F,ctxF_pt,ierr)
  PRINT*,'ctxF_pt%lambda = ',ctxF_pt%lambda 

  !
  CALL PetscFinalize(ierr)

END PROGRAM main

Reply via email to