[petsc-dev] XXXGetContext() in Fortran

Barry Smith bsmith at mcs.anl.gov
Wed Nov 9 12:52:48 CST 2011


  Jed,

   So what both Satish's and Ethan's email are saying is so long as you tell the Fortran function the interface that shows the type being returned you can "trick" the Fortran into thinking that data type is just coming from another Fortran routine.
 
   I think this should resolve your issues and everything should be straightforward (no need for transfer function). Please let us know if this is problematic.

    Barry

On Nov 9, 2011, at 12:22 PM, Ethan Coon wrote:

> Doesn't the bag construct do just this?  
> 
> http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-dev/src/sys/examples/tutorials/ex5f90.F90.html
> 
> and
> 
> src/sys/bag/f90-custom/zbagf90.c
> 
> Ethan
> 
> On Tue, 2011-11-08 at 22:11 -0600, Satish Balay wrote:
>> 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>
> 
> -- 
> ------------------------------------
> Ethan Coon
> Post-Doctoral Researcher
> Applied Mathematics - T-5
> Los Alamos National Laboratory
> 505-665-8289
> 
> http://www.ldeo.columbia.edu/~ecoon/
> ------------------------------------
> 




More information about the petsc-dev mailing list