Print

Print


----- Original Message -----
From: "Aleksandar Donev" <[log in to unmask]>
To: <[log in to unmask]>
Sent: Wednesday, October 31, 2001 12:11 PM
Subject: PETSc library --- C dynamic arrays and Fortran


> Hello,
>
> I am wondering if someone has used the parallel linear algebra package
> PETSc from Fortran and what the experience has been. The package does
> provide a Fortran interface to its distinctly C-like routines, but this
> interface is somewhat ``scary'' and not legal.
>
> For example, a lot of the routines there return a C pointer to an array
> allocated internally by the library. To use these in Fortran, they
> imploy a dirty trick of passing an array of 1 element and returning the
> integer offset of the allocated array from this array, so one can index
> the array in Fortran (but passing beyond the array bounds):
>
> Here is a sligtly tweaked example from their page:
>
>    INTEGER(C_pointer)   Mapping
>    integer     Array(1)
>    INTEGER offset, error_status
>
>    call ISGetIndices(Mapping, Array, offset, error_status)
>
> The allocated array is then Array(offset: offset+array_size-1) in
> Fortran, though this is illegal (maybe the wrong word, but you get the
> point) since the array is only of size 1.
>
If I understand you correctly, this is the old pre-77 trick, which, if it
works, is considered to be the same as

integer     Array(*)

which means no bounds checking on many compilers.  Since there is no
standard yet on interfacing C to Fortran, you could hardly call it
"legal," or expect any guarantee about portability.  It appears to assume
call by reference, a fairly common but not universal type of
implementation.