POINTER components and mixed-language (AKA can of worms)

Tobias Burnus burnus@net-b.de
Sun Apr 6 13:41:00 GMT 2008


Hi,

Dennis Wassel wrote:
> For the sparse nonlinear optimisation solver I've been working on, I
> created some datastructures with 1D-array pointer components [which I
> might change to allocatables later on, since most compilers now
> support the TR 15581 extension] and the SEQUENCE attribute. There are
> some init/free routines to do the ALLOCATE stuff, so all's well in
> Fortran-world.
> The troublesome bits that I am aware of are a) memory allocation and
> b) array descriptors.
>   

Indeed, although the memory allocation should be a smaller problem. It 
sounds as if you need extended C interoperability support as defined in 
a being-developed Technical Report. In this technical report, array 
descriptors for C interoperability are well defined. The problem is that 
this TR is still in a draft status and to my knowledge not implemented 
in any compiler.

> To sum things up, I'd be very happy to hear your pointers and comments on
> * Using gfortran's (and others') ADs in C data structures, how they
> need to be initialised (not allocated) by MAIN, and maybe how to
> increase portability of this approach,
> * My incredibly C-ish void-pointer approach -- is this any good, or do
> you know better ways to do this?
>   
I would go for a kind of void-pointer approach using the C binding 
feature of Fortran 2003, which is supported by several compilers - 
including gfortran 4.3.0 (or heigher).

I mean something along those lines:


module m
  use iso_c_binding
  implicit none
  real, pointer :: myArray(:)
contains
  ! void assignArray(void *, int)
  subroutine assignArray(cptr, n) bind(C,name="assignArray")
    integer(c_int), value :: n
    type(c_ptr), value    :: cptr
    call C_F_pointer (cptr, myArray, (/ n /))
  end subroutine assignArray

  ! int retrieveArray(void *)
  function retrieveArray(cptr) result(n) bind(C, name="retrieveArray")
    integer(c_int) :: n
    type(c_ptr), value :: cptr
    n = size(myArray)
    ! assumes that myArray is contigious and the first element is 1
    cptr = c_loc(myArray(1))
  end function retrieveArray
end module m


Some short C binding introduction can be found in ftp://ftp.nag.co.uk/sc22wg5/N1601-N1650/N1648.pdf

Tobias



More information about the Fortran mailing list