Peter Green
2022-Feb-04 10:21 UTC
[R] Using .Fortran in R - how can I use file i/o flexibly OR arrays of variable length as arguments?
I have a legacy Fortran code many thousands of lines long, a free-standing program that I now want to adapt with minimum re-writing to drive an R package, using .Fortran to pass control to a version of that legacy code re-written as a subroutine. BUT the output I want to return to R is copious, and variable in length, the length being computed within the Fortran code (it is a variable-dimension simulation). I'm looking for a solution general enough to include in a package that would pass CRAN checks. I can think of two broad approaches (1) to write this output to files in the Fortran code, to be read back later in R.? Of course I know about realpr, etc., but this is pretty ugly - apparently there's no binary i/o and no way to direct to a named file. (2) to? dynamically allocate memory in the Fortran code for this output, and find a way for the address of this memory to be available in the result returned to R by .Fortran. The nearest to a possible solution that I can find in "Writing R extensions" involve use of C concepts, poorly explained, and I am not sure whether and how these could be used in my context. Advice or information please! -- Peter Green, p.j.green at bristol.ac.uk, peter.green at uts.edu.au -- Emeritus Professor of Statistics & Professorial Research Fellow, University of Bristol, Bristol BS8 1UG, UK Phone: +44 117 428 4845; Fax: +44 117 928 7999 -- Distinguished Professor of Statistics, University of Technology, Sydney Room: CB07.05.051; PO Box 123, Broadway NSW 2007, Australia Phone: +61 2 9514 1742; Fax: +61 2 9514 2260
Ivan Krylov
2022-Feb-05 08:09 UTC
[R] Using .Fortran in R - how can I use file i/o flexibly OR arrays of variable length as arguments?
On Fri, 4 Feb 2022 10:21:11 +0000 Peter Green <mapjg at bristol.ac.uk> wrote:> the output I want to return to R is copious, and variable in length, > the length being computed within the Fortran code (it is a > variable-dimension simulation)Is there a way to separate the calculation of the output size from the rest of the subroutine? If yes, the simplest option would be to pass pre-allocated vectors to the .Fortran() call. Unfortunately, .Fortran() is not expressive enough for more complicated use cases. Memory that you intend to return to R must be allocated using its own allocator/garbage collector system, which (like any other call into R from user code) may longjmp() away from your functions on interrupt, error or allocation failure, abandoning any resources the garbage collector doesn't know about. In turn, this requires the use of C and the PROTECT() macro. In theory, you could use Fortran 2003 "C interoperability" to call C entry points, but I expect that to be very inconvenient (at least due to the lack of CPP macros). Since you mention CRAN checks, this might be a better question for <r-package-devel at r-project.org>, not R-help. -- Best regards, Ivan