[petsc-dev] XXXGetContext() in Fortran

Satish Balay balay at mcs.anl.gov
Tue Nov 8 22:11:31 CST 2011


On Tue, 8 Nov 2011, Jed Brown wrote:

> We have bindings for this, but I can't find a way to make it work. I can
> pass a context into a function, but I can't get one back out with a
> subroutine call. F90 pointers are grotesque creatures that don't seem to be
> up to this task either, at least not with any chance of portability.
> 
> Is there anything we can do to avoid common blocks without losing all hope
> of portability? How does anyone use this sorry excuse for a language?
> 
> Note that having the context passed in as an argument isn't feasible for
> PCShell and MatShell, so it's nigh impossible to use these objects with
> multigrid (or any other case where you have multiple instances of shell
> objects, breaking the common blocks.)

The following is an example on passing in 'ctx' back and forth between
c & F90. Not sure if we can do some bindings on the petsc side to help
with this usage..

Satish

----------
asterix:/home/balay/junk/ctx>cat ctxf.F
      module ctxmod
      type userctx
      integer :: a
      double precision :: b
      end type userctx
      end module ctxmod

      program main
      use ctxmod
      interface
         subroutine setcontext(ctx)
         use ctxmod
         type(userctx) ctx
         end subroutine
         subroutine getcontext(ctx)
         use ctxmod
         type(userctx),pointer:: ctx
         end subroutine
      end interface
      external setctxptr

      type(userctx),target  :: ctxin
      type(userctx),pointer :: ctxout
      ctxin%a = 13
      ctxin%b = 3.14d0
      call setcontext(ctxin)
      call setcontextfunction(setctxptr)
      call getcontext(ctxout)
      write(*,*) ctxout%a, ctxout%b
      end program

      subroutine setctxptr(ctxin,ctxout)
      use ctxmod
      type(userctx),target:: ctxin
      type(userctx),pointer:: ctxout
      ctxout => ctxin
      end 
asterix:/home/balay/junk/ctx>cat ctxc.c
static void* ctxptr = 0;
static  void (*ctxfunction)(void*,void*) = 0;

void setcontext_(void* inptr)
{
  ctxptr = inptr;
  return;
}

void setcontextfunction_(void(*fin)(void*,void*))
{
  ctxfunction = fin;
  return;
}

void getcontext_(void*outptr)
{
  (*ctxfunction)(ctxptr,outptr);
  return;
}
asterix:/home/balay/junk/ctx>gcc -c -g ctxc.c
asterix:/home/balay/junk/ctx>gfortran -g ctxf.F ctxc.o 
asterix:/home/balay/junk/ctx>./a.out 
          13   3.1400000000000001     
asterix:/home/balay/junk/ctx>



More information about the petsc-dev mailing list