Re: gfortran and the family of fortran array types



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
.



Relevant Pages

  • Re: Who uses clapack?
    ... > functions like SUM and MAXVAL and the ability to use array sections ... > drastically change the meaning of a Fortran code. ... >>Have you ever seen those bills for a Fortran compiler for an ... engineering applications in the C and C++ languages. ...
    (comp.lang.fortran)
  • Re: Who uses clapack?
    ... > functions like SUM and MAXVAL and the ability to use array sections ... > drastically change the meaning of a Fortran code. ... >>Have you ever seen those bills for a Fortran compiler for an ... engineering applications in the C and C++ languages. ...
    (sci.math.num-analysis)
  • Re: Benchmarking APL
    ... with that of FORTRAN. ... APL a 400% gain in programming efficiency could be obtained with only ... Another good-for-APL array problem, vector convolution, gave these ... Tomcatv, one of the SPEC benchmarks, is more typical of large array ...
    (comp.lang.apl)
  • Re: Mixing C++/Fortran using Two-Dimensional Array
    ... not a subset as in Fortran. ... if you really need it you can get the effect by an array of TYPE ... the lowest-level base element pointer, which is all you actually need ... macros in C, although it can be difficult to find macro names that are ...
    (comp.lang.fortran)
  • Re: how to "translate" dynamic array in FORTRAN 90 to Java code?
    ... I am translating a Fortran program to Java code. ... is an array with size unspecified. ... I use Object array in Java to translate FORTRAN array. ...
    (comp.lang.java.programmer)