[Rd] ALLOCATE in a FORTRAN subroutine

Martyn Byng martyn.byng at nag.co.uk
Fri Mar 4 17:52:44 CET 2016


Hi,

Until you get a more definitive answer, I will make an attempt to give some advice.

When using an assumed sized array (i.e. REAL*8 array1(*)) you still need to allocate the memory prior to calling the Fortran subroutine, so you would still need to know its maximum length.

Arrays created in a Fortran subroutine via the use of the ALLOCATE statements are not simple arrays (in the sense of C like pointers), but are more akin to a C structure, in that they also contain information concerning the size of the array, stride etc. These structures are compiler dependent, their contents often not clearly documented and subject to change. They are therefore not very good for cross-language computing (or even cross-compiler computing).

To try and alleviate this the Fortran standard introduced C interoperability (see https://gcc.gnu.org/onlinedocs/gfortran/Interoperability-with-C.html for example) and I expect you are going to have to take a look at this in order to pass memory allocated in your Fortran program back up to R. Care is going to have be taken to make sure that the memory is deallocated when it has been finished with (which is probably going to have to be done in Fortran).

The easiest way of getting all this to interact nicely with R is probably through the R C API which is described in documents like https://cran.r-project.org/doc/manuals/R-exts.pdf.

Martyn


-----Original Message-----
From: R-devel [mailto:r-devel-bounces at r-project.org] On Behalf Of MAURICE Jean - externe
Sent: 04 March 2016 08:34
To: r-devel at r-project.org
Subject: [Rd] ALLOCATE in a FORTRAN subroutine

Hi,
I am a FORTRAN developer and I am 'translating' R functions in FORTRAN subroutines. I am 'new' to R. It's my first question in this mailing-list and English is not my natural language.

Very often, an R function gives an  'array' as result and you don't have to bother with the dimension of the array : R creates automatically an array with the good length. It's not really the case with FORTRAN. I call FORTRAN subroutines with .fortran().

Until now, I create an array with the 'max' dimensions in R, give it to FORTRAN; FORTRAN updates the array and R retrieves it. But calculating the 'max' before calling the FORTRAN subroutine can be complicated. Is it possible to create a 'new' array in a FORTRAN subroutine and to make it be read by R ?

Or is it possible to have a 'pointer' in R, to give it to the FORTRAN subroutine where an ALLOCATE can create the array and then R works with the array ?
The other solution, is to work with dummies dimension in FORTRAN (REAL*8 array1(*)) but can R work with that ?

TIA
Jean


________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:16}}



More information about the R-devel mailing list