Re: gfortran and the family of fortran array types
- From: relaxmike <michael.baudin@xxxxxxxxx>
- Date: Wed, 27 Aug 2008 00:41:59 -0700 (PDT)
The following is mainly hypothetical, since I had no
opportunity to experiment C/Fortran interoperability
with respect to fortran 90 arrays.
My reference is the fortran 2003 pdf written by
Patrick Corde, Hervé Delouis from IDRIS :
http://www.idris.fr/data/cours/lang/fortran/f2003/Fortran_2003.pdf
There is also "The New Features of Fortran 2003" by John Reid,
especially p36 and p37 :
ftp://ftp.nag.co.uk/sc22wg5/N1551-N1600/N1579.pdf
I look at the chasm file GNU_dv.h which describes the gfortran
implementation of a fortran 90 array, based on a C struct :
http://chasm-interop.cvs.sourceforge.net/chasm-interop/chasm/include/compilers/GNU_dv.h?revision=1.2&view=markup
typedef struct dope_vec_GNU_ {
void* base_addr; /* base address of the array */
void* base; /* base offset */
size_t dtype; /* elem_size, type (3 bits) and rank (3 bits)
*/
struct {
size_t stride_mult; /* distance between successive elements
(elements) */
size_t lower_bound; /* first array index for a given dimension */
size_t upper_bound; /* last array index for a given dimension */
} dim[7];
} dope_vec_GNU;
If I understand it well, only the field base_addr corresponds to
a C array, as a pointer to the first element of the array.
Of course, the implementation may depend on the compiler,
since the fortran 90 standard define only the features of the
array, not the real implementation. In fact, the fortran 90
standard introduced a problem for interoperability for
fortran 90 arrays with C (there was no problem for fortran 77
arrays). The fortran 2003 standard solves that problem, by
introducing
several operators, such as C_LOC.
Suppose that a fortran 90 program allocates an array and
want to pass that array to a C routine, as it is done on
page 33/174 of the Corde&Delouis pdf.
The fortran 90 array is declared as :
real(kind=C_FLOAT), dimension(:,:), allocatable, target :: mat
To be manageable by the C routine, a conversion must be done so that
only the base_addr field is passed to the C routine. The fortran 2003
introduces the C_LOC operator for that purpose :
"C_LOC(X) is an inquiry function that returns the C address of an
object."
On the fortran side, the C function is passed C_LOC(mat). On the C
side,
one receives a float *vec, from which the matrix can be
accessed with some index algebra.
In the "Example of C calling Fortran" of the Reid pdf, p37, on the C
side, the array delta is declared with :
double delta[]
On the fortran side, the array is declared as :
REAL (C_DOUBLE),DIMENSION(*),INTENT(IN) :: DELTA
that is, with the fortran 77 syntax which corresponds to a
"assumed-size dummy array".
I did not understand these problems before I was able to
experiment with the "-fdump-tree-original" option
in gfortran. But what I just wrote may still contain several
confusions, which other experts in this forum may want to
fix.
Regards,
Michaël
.
- Follow-Ups:
- Re: gfortran and the family of fortran array types
- From: Richard Maine
- Re: gfortran and the family of fortran array types
- References:
- Re: gfortran and the family of fortran array types
- From: relaxmike
- Re: gfortran and the family of fortran array types
- Prev by Date: Re: busting sp datatypes
- Next by Date: Re: gfortran and the family of fortran array types
- Previous by thread: Re: gfortran and the family of fortran array types
- Next by thread: Re: gfortran and the family of fortran array types
- Index(es):
Relevant Pages
|