Re: Allocatable components + mixed-language
- From: Dennis Wassel <dennis.wassel@xxxxxxxxxxxxxx>
- Date: Mon, 16 Jun 2008 06:47:44 -0700 (PDT)
Steve,
thanks for your example, this will help!
On 13 Jun., 05:52, Steve Lionel <steve.lio...@xxxxxxxxxxxxx> wrote:
Here's an example illustrating this. I note that I erred earlier in
referring to "C_F_PTR" - the procedure is actually C_F_POINTER (I keep
making this mistake). This example shows how a derived type could be
passed from C and the array reconstructed in Fortran using the C Interop
features.
subroutine called_from_c (arg) bind(C)
use, intrinsic :: iso_c_binding
implicit none
! Declare an interoperable derived type
! containing the data address (C pointer)
! and number of elements
type, bind(C) :: argtype
type(c_ptr) :: p
integer(c_int) :: nelements
end type argtype
! Declare the argument as that type
type(argtype), intent(in) :: arg
! Declare a local POINTER to an array of floats.
! We will use this to access the data passed from
! C
real(c_float), pointer, dimension(:) :: x
Can someone think of a way to wrap up those pointers nicely? (there
are currently ~10, although this number may grow a lot larger)
I'd really like to put those into a separate, Fortran-only type, say
"Foo", that I sneak past the C interop somehow; however, passing Foo
to called_from_c is just shifting the problem and will prohibit C
interop. Using a module to make it accessible globally is a Bad Idea
(TM). I'm out of ideas here!
! Convert the array information in the passed argument
! to something we can use.
call c_f_pointer (arg%p, x, [arg%nelements])
! Now x is an array of bounds 1:nelements
While this is cool, won't the pointer assignment have some performance
impact? Our central Fortran routine is called quite often. This is one
of the reasons (besides general tidyness) why I'd like these pointers
wrapped up into a Fortran-only type, so I assign them once and for
good.
--.
Steve Lionel
- Follow-Ups:
- Re: Allocatable components + mixed-language
- From: Steve Lionel
- Re: Allocatable components + mixed-language
- References:
- Allocatable components + mixed-language
- From: dennis . wassel
- Re: Allocatable components + mixed-language
- From: Steve Lionel
- Re: Allocatable components + mixed-language
- From: Dennis Wassel
- Re: Allocatable components + mixed-language
- From: Steve Lionel
- Allocatable components + mixed-language
- Prev by Date: Re: J3 request for interpretation, f.p. semantics of the REAL intrinsic function
- Next by Date: Re: better method
- Previous by thread: Re: Allocatable components + mixed-language
- Next by thread: Re: Allocatable components + mixed-language
- Index(es):
Relevant Pages
|
|