POINTER components and mixed-language (AKA can of worms)

Dennis Wassel dennis.wassel@googlemail.com
Sun Apr 6 13:04:00 GMT 2008


Hi fellow Fortraners,

I've been trying to get some tricky mixed-language stuff for my PhD
project to run with gfortran (without success so far) and would be
glad for some expert help and comments on this. I appreciate that this
list is for gcc-related stuff, and part of my issue goes beyond that,
but please bear with me, I have literally no-one else to turn to at my
institution.

For the sparse nonlinear optimisation solver I've been working on, I
created some datastructures with 1D-array pointer components [which I
might change to allocatables later on, since most compilers now
support the TR 15581 extension] and the SEQUENCE attribute. There are
some init/free routines to do the ALLOCATE stuff, so all's well in
Fortran-world.

However, along comes C: The Fortran code goes in a library to be
called by C (C++, Ada, ..., who-knows-what) and this opens a can of
worms, I'm well aware! One central bit seems to be the fact that MAIN
is not Fortran anymore. If you're now itching to say "don't use those
darned pointer components then" -- sorry, not an option, I really need
them.

The troublesome bits that I am aware of are a) memory allocation and
b) array descriptors.

My first approach is to exactly mimic the Fortran datastructures
(including ADs) by C, which has some obvious advantages, like
transparency, accessibility and memory allocation.
To construct the AD, I've looked at libgfortran.h and found some
essential stuff there - anywhere else to look?
This still causes problems, however, since every AD seems to be
initialised to some non-zero standard values by a Fortran MAIN, which
I would need to replicate in C -- but I can't seem to figure them out.
As a consequence, SIGSEGs and stuff hail down on me ...
Disadvantages of this approach are compiler-dependence (which I think
is evil, even if it is teh gcc) and potential license issues, if I
reuse some gfortran AD code, since my code cannot be made open-source,
let alone GPL.

My second, so far untested, approach is somewhat simpler: Just treat
the data structures as blackbox void-pointers by non-Fortran caller.
For every compiler/system/architecture I would then run some
sophisticated configure script that tries and figures out the size of
each datastructure in some clever and automatted way, has a Fortran
MAIN send the unallocated (!) structures to a C routine and has the
latter dump that many bytes into a binary file.
For every init call, the void-pointers would then be malloc'ed, the
dumped binary data put into them, and then send to the usual Fortran
init routines.
This is inherently compiler-independent and portable (though in a
twisted way), but also incredibly ugly and bug-prone. Also, this would
need special access functions, which incur some overhead.


To sum things up, I'd be very happy to hear your pointers and comments on
* Using gfortran's (and others') ADs in C data structures, how they
need to be initialised (not allocated) by MAIN, and maybe how to
increase portability of this approach,
* My incredibly C-ish void-pointer approach -- is this any good, or do
you know better ways to do this?

Thanks a bunch!
Dennis



More information about the Fortran mailing list