Skip to content

ALLOCATE in a FORTRAN subroutine

10 messages · MAURICE Jean - externe, Duncan Murdoch, Martyn Byng +2 more

#
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

-------------- next part --------------



Ce message et toutes les pi?ces jointes (ci-apr?s le 'Message') sont ?tablis ? l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme ? sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse.

Si vous n'?tes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez re?u ce Message par erreur, merci de le supprimer de votre syst?me, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions ?galement d'en avertir imm?diatement l'exp?diteur par retour du message.

Il est impossible de garantir que les communications par messagerie ?lectronique arrivent en temps utile, sont s?curis?es ou d?nu?es de toute erreur ou virus.
____________________________________________________

This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.

If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message.

E-mail communication cannot be guaranteed to be timely secure, error or virus-free.
#
On 04/03/2016 3:34 AM, MAURICE Jean - externe wrote:
I don't think this is possible in pure Fortran, but it is certainly 
possible in C or C++ code.  You need to call the external routine using 
.Call() instead of .C or .Fortran.  See the section 5.9 of the Writing R 
Extensions manual for details.
No.  R needs to manage allocations of all the objects it uses.  However, 
if you never need to use these arrays in R code (you just want R to keep 
references to them to pass to other external routines), you can (in C or 
C++) use the external pointer type.  I don't think there's any support 
for that in Fortran.
No, for the same reason.

Duncan Murdoch
#
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}}
2 days later
#
Hi Martyn,

Many thanks for your answer. If I make it short : 
we can, once we know how to do it, 'drive' R from within FORTRAN for example to do a 
  Myarray = seq(0, mydimension) 
in R once we have compute mydimension in FORTRAN. Is that correct ?
If yes : it's too 'complicated' for the time I am hired (I mean I have been hired to do FORTRAN code not to learn R !).

Second question : for what I have understood in r_exts, it's more 'efficient' to translate R routines in C rather than in FORTRAN (speed must be the 'same', but there are a lot more possibilities in C for the programmer) : right ? I do not know why my customer (client ?) wanted to translate some R routines in FORTRAN. May be it's by accident (in my humble opinion they even don't know about C). If you confirm this fact, I'll speak about C with my customer because we are at the very beginning of great works (great amounts of work ?) and I don't matter, I am also a skilled C programmer !!

I hope you understand what I want to say, english is not my natural language !

Thanks again and best regards.
Jean
-------------- next part --------------



Ce message et toutes les pi?ces jointes (ci-apr?s le 'Message') sont ?tablis ? l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme ? sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse.

Si vous n'?tes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez re?u ce Message par erreur, merci de le supprimer de votre syst?me, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions ?galement d'en avertir imm?diatement l'exp?diteur par retour du message.

Il est impossible de garantir que les communications par messagerie ?lectronique arrivent en temps utile, sont s?curis?es ou d?nu?es de toute erreur ou virus.
____________________________________________________

This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.

If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message.

E-mail communication cannot be guaranteed to be timely secure, error or virus-free.
#
Hi,

"we can, once we know how to do it, 'drive' R from within FORTRAN"
 I am not sure I understand what you mean by this ...

You can call routines written in Fortran or C from within R - how easy this is depends on the interfaces to those routines.
You can call (some) R functionality from C (see for example http://quantitative-ecology.blogspot.co.uk/2008/11/call-c-from-r-and-r-from-c.html). As you can call C from within Fortran, it is obviously possible also call R from within Fortran, but it is not something I have done so have no idea how much work is required to do that.

"for what I have understood in r_exts, it's more 'efficient' to translate R routines in C rather than in FORTRAN"
As far as I aware the R API for writing extensions etc is only available in C and there isn't a Fortran equivalent, so I guess it would be more "efficient" / easier in that sense as that would be one less level of cross-language programming you would have to worry about.

"I do not know why my customer (client ?) wanted to translate some R routines in FORTRAN."
Which language is preferred really boils down to personal preference and I am going to be slightly biased as I do almost all my numerical computing in Fortran. There is a good argument that Fortran is the most natural fit for numerical computing with its built in syntax to deal with complex numbers, matrices, array slicing, array operations  etc. In theory this gives compilers a good base to optimise from. The more restrictive nature of Fortran does make it more difficult to interface with other languages than C (but conversely makes it much more difficult to shoot yourself in the foot - and the Fortran compilers tend to pick up more problems at compile time than the C ones).

Martyn

-----Original Message-----
From: MAURICE Jean - externe [mailto:jean-externe.maurice at edf.fr]
Sent: 07 March 2016 15:46
To: Martyn Byng <martyn.byng at nag.co.uk>
Cc: r-devel at r-project.org
Subject: RE: ALLOCATE in a FORTRAN subroutine

Hi Martyn,

Many thanks for your answer. If I make it short :
we can, once we know how to do it, 'drive' R from within FORTRAN for example to do a
  Myarray = seq(0, mydimension)
in R once we have compute mydimension in FORTRAN. Is that correct ?
If yes : it's too 'complicated' for the time I am hired (I mean I have been hired to do FORTRAN code not to learn R !).

Second question : for what I have understood in r_exts, it's more 'efficient' to translate R routines in C rather than in FORTRAN (speed must be the 'same', but there are a lot more possibilities in C for the programmer) : right ? I do not know why my customer (client ?) wanted to translate some R routines in FORTRAN. May be it's by accident (in my humble opinion they even don't know about C). If you confirm this fact, I'll speak about C with my customer because we are at the very beginning of great works (great amounts of work ?) and I don't matter, I am also a skilled C programmer !!

I hope you understand what I want to say, english is not my natural language !

Thanks again and best regards.
Jean

________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:16}}
#
I too do not understand what you mean by this.

I told you on the R-help mailing list that what you could do is to 

- write a subroutine that calculates the required dimension given your parameters; you can do this within R code.
- allocate in R (or C) a matrix/vector/.. with the required dimension.
- call your Fortran subroutine(s) with the allocated matrix/vector

There are several packages, as I replied before on the R help list,  that do this (from within an R function or a C function).


Berend
#
Correction: write a subroutine in Fortran that calculates the required dimension(s), call that routine from within R and make it return the required dimension(s).

etc.etc.

Berend
#
When I read about 'R api' I thought that we could call all the functionality of R. I think that in the windows world, this is called OLE automation. From a pure programmer point of view, it is very convenient and my customer is creating a team of programmers. But, if only 'some' functionalities can be called, it's not so useful. So we'll stick with FORTRAN (I mean I don't want to make revolution).

Jean
-------------- next part --------------



Ce message et toutes les pi?ces jointes (ci-apr?s le 'Message') sont ?tablis ? l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme ? sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse.

Si vous n'?tes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez re?u ce Message par erreur, merci de le supprimer de votre syst?me, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions ?galement d'en avertir imm?diatement l'exp?diteur par retour du message.

Il est impossible de garantir que les communications par messagerie ?lectronique arrivent en temps utile, sont s?curis?es ou d?nu?es de toute erreur ou virus.
____________________________________________________

This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.

If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message.

E-mail communication cannot be guaranteed to be timely secure, error or virus-free.
#
On Mar 8, 2016, at 6:57 AM, MAURICE Jean - externe <jean-externe.maurice at edf.fr> wrote:

            
You can call *all* R functionality from C.
FWIW it's not related to OLE which is IPC and much more limited. The R API is direct linking and uses the same code as R implementation itself.

Cheers,
S
#
I'll dig that (I mean I'll read more in the help to be sure)
It was an example of what I was trying to say with "driving R from within FORTRAN". I have done a lot of Word Automation, Excel Automation and even Open Office 'automation' working with Visual Foxpro. I am 'easy' with that.

Thank you a lot for your interest.
Jean
-------------- next part --------------



Ce message et toutes les pi?ces jointes (ci-apr?s le 'Message') sont ?tablis ? l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme ? sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse.

Si vous n'?tes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez re?u ce Message par erreur, merci de le supprimer de votre syst?me, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions ?galement d'en avertir imm?diatement l'exp?diteur par retour du message.

Il est impossible de garantir que les communications par messagerie ?lectronique arrivent en temps utile, sont s?curis?es ou d?nu?es de toute erreur ou virus.
____________________________________________________

This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.

If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message.

E-mail communication cannot be guaranteed to be timely secure, error or virus-free.