From tk @end|ng |rom tkoen|g@net  Sat May  4 18:42:47 2019
From: tk @end|ng |rom tkoen|g@net (=?UTF-8?Q?Thomas_K=c3=b6nig?=)
Date: Sat, 4 May 2019 18:42:47 +0200
Subject: [Rd] R problems with lapack with gfortran
In-Reply-To: <D521C66E-D67E-4C17-98EF-D651026561ED@gmail.com>
References: <ff60d7f9-e54d-473a-bc85-7989abc887da@tkoenig.net>
 <b24b0ef5-1f45-1a03-4153-f335e4f1f8a5@gmail.com>
 <2e68f333-1d10-02d4-3560-4b1b9d773a52@tkoenig.net>
 <6E812A6F-91B2-4412-A536-2CD672C5F4BA@xs4all.nl>
 <D521C66E-D67E-4C17-98EF-D651026561ED@gmail.com>
Message-ID: <79689a6a-077c-019a-d16a-597d7b60199d@tkoenig.net>

Hi Peter,

we (the gfortran team) are currently discussing this at
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90329 . I
invite everybody who has an interest in this topic to
take part in the discussion there.

> Workarounds/solutions include:
> 
> - disable certain optimizations -- works for now, but doesn't remove the root cause so seems generally fragile

That looks like a short-term solutuion that could work (at least for
x86_64 using the standard Unix ABI). And yes, it is fragile.

And whatever other solution people come up with, it will still be
fragile unless the caller and the callee agree.

The root cause is that the Fortran LAPACK routines are called from C
via an incompatible call signature.

> - "onion-skin" all LAPACK routines to call via a Fortran routine that converts integer arguments to the required character -- possible, but it adds overhead and there are hundreds of routines (and it would be kind of ugly!).

I agree.

> - modify LAPACK itself similarly -- requires naming change of routines as per the license, and there are still hundreds of routines; avoids overhead, but creates maintenance nightmare to keep up with changes in LAPACK

I agree that this is not a preferred option.

> - change all prototypes and calls to follow gfortran calling conventions -- still a lot of work since each char* arguments need to be supplemented by a length going at the end of the arglist. If gfortran was the only compiler around, I'd say this would be the least painful route, but still no fun since it requires changes to a lot of user code (in packages too). It is not clear if this approach works with other Fortrans.

The interesting thing is that this convention goes back to at least f2c,
which was modeled on the very first Unix compiler.

> - figure out Fortran2003 specification for C/Fortran interoperability -- this _sounds_ like the right solution, but I don't think many understand how to use it and what is implied (in particular, will it require making changes to LAPACK itself?)

That would actually be fairly easy.  If you declare the subroutines
BIND(C), as in

       subroutine foo(a,b) BIND(C,name="foo_")
       real a
       character*1 b
       end

you will get the calling signature that you already have in your C
sources.

This also has the advantage of being standards compliant, and would be
probably be the preferred method.

> - move towards the LAPACKE C interface -- but that also adds onionskin overhead and ultimately calls Fortran in essentially the same way as R does, so doesn't really solve anything at all (unless we can shift responsibility for sorting things out onto the LAPACK team, but I kind of expect that they do not want it.)

I suspect that they will hit the issue, too.

> - twist the arms of the gfortran team to do something that keeps old code working. Compiler engineers understandably hate that sort of thing, but I seem to recall some precedent (pointer alignment, back in the dark ages?).

We're willing to do reasonable things :-) but so far all of the options
we have come up with have very serious drawbacks (see the link to the
PR at the top). If you come up with a suggestion, we'd be more than
happy to look at it.

I think the best option would really be to use BIND(C).

Regards

	Thomas



[Rd] R problems with lapack with gfortran
=?UTF-8?Q?Thomas_K=c3=b6nig?=