Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fortran90/C Interface for complex data structures

6 views
Skip to first unread message

Arne

unread,
Jun 20, 2001, 11:55:31 AM6/20/01
to
I am working on a programme in Fortran90 (Solaris) and need to call a C routine
from my Fortran90. Unfortenunately, I have to tranfer a huge data structure from
Fortran90 to this C-routine.

The data structure (Fortran90) looks similar to the following, but larger:

type typ_hub

integer :: &
n_nnodes, &
n_narcs

integer, dimension (:), pointer :: &
pt_fromnode, &
pt_tonode

double precision, dimension (:), pointer :: &
pt_supply, &
pt_low, &
pt_up, &
pt_nwfobj

character, dimension (:), pointer :: &
cpx_sense

end type typ_hub

type (hb_typ_hub), dimension(:), pointer :: &
hb_hub

My problem is to transfer this structure to C. I declarated a equivalent type
in C, but the whole thing isn't working. I managed to export single arrays to
C, but not this structure. Is it possible?

My question is if there is someone who has experience to transfer complex data
structures from Fortran90 to C. I would be glad if someone can help me.

Thanks a lot.

Arne

Richard Maine

unread,
Jun 20, 2001, 1:04:53 PM6/20/01
to
arne.zi...@gmx.de (Arne) writes:

> The data structure (Fortran90) looks similar to the following, but larger:
>
> type typ_hub

...


> integer, dimension (:), pointer :: &

...

> My problem is to transfer this structure to C. I declarated a equivalent type
> in C,

I seriously doubt that you declared an equivalent type in C. Fortran pointers
don't *HAVE* an equivalent in C. That is probably most of your problem. If
you tried to declare a C structure based on the assumption that C pointers
are equivalent to Fortran pointers, then that won't even be close.

It is best to avoid trying to transfer such structures between Fortran
and C at all. If you really have to, then it is going to be *HIGHLY*
compiler-dependent what the Fortran pointers look like in terms of C.
I don't know the answers for the compiler you mentioned.

--
Richard Maine | Good judgment comes from experience;
email: my last name at host.domain | experience comes from bad judgment.
host: altair, domain: dfrc.nasa.gov | -- Mark Twain

Jugoslav Dujic

unread,
Jun 20, 2001, 12:38:14 PM6/20/01
to
Hi Arne,

Fortran pointers are _not_ the same as C pointers. There's
a recent similar thread "Using Fortran95 modules in C
(nag compiler / unix)", here's a quote:

<quote>
"A.I. van Berkel" <vanb...@utwbtw81.wb.utwente.nl> wrote in message
news:slrn9cmhed....@utwbtw81.wb.utwente.nl...
| Hello,
|
| Right now, the program compiles without any problems. The link step is
| also ok, but I get a segmentation fault when I try to run the
| executable.
|
| I suspect this is due to the derived types.
|
| In the module, the following type is defined:
|
| TYPE,public :: dbase
| private
| integer, dimension(:), pointer :: index
| real(kind=ps) , dimension(:), pointer :: data
| END TYPE dbase
|
| typedef struct {
| int* index;
| double* data;
| } dbase;
|
| I also tried other definitions, but there was no noticable difference
| in behaviour.
|
| Does anyone have suggestions how to do this properly ?

I'm afraid that the two type declarations above are _not_
equivalent, since fortran's "safe" POINTER is not the
same as C's "integer" pointer. The representation of
"index" variable in memory is not a single address;
it consists of array descriptor, which is compiler-specific
(typically containing smt. like starting address, #of dimensions,
lower and upper bound for each dimension and stride (step)).
You'll either have to drop out structures containing pointers
and replace them with pure arrays, or dig into documentation
(or try a deep-debug of working fortran code to find out
yourself) to find the exact format of NAG's array descriptor and
then write the equivalent C structure, smt. like:

typedef struct {
void* address;
int lbound;
int ubound;
int stride;
} FDESCRIPTOR;
typedef struct {
FDESCRIPTOR index;
FDESCRIPTOR data;
} dbase;

however, to repeat, this is non-portable. Also, if NAG
supports "Cray" pointers (non-standard again), they may
be of some help.
</quote>

Regards
Jugoslav

Arnaud Desitter

unread,
Jun 20, 2001, 12:07:25 PM6/20/01
to Arne

If portability is important, your best bet is to write a routine that
copies the
data in some C arrays. Otherwise, you can read the manual of your
compiler which
may describe the data layout it uses. Some extensions such as "LOC" may
help to
return the pointer of arrays. Quite a pain in any case.

Cheers,
Arnaud

0 new messages