Skip to content

Converting Fortran or C++ etc to R

7 messages · Murray Jorgensen, lcn, Michael Sumner +2 more

#
I'm going to try my hand at converting some Fortran programs to R. Does 
anyone know of any good articles giving hints at such tasks? I will post 
a selective summary of my gleanings.

Cheers,  Murray
#
Hi Murray, at first I thought you meant compiling existing Fortran or
C++ for use in R with .Fortran() and so on, but do you mean literal
conversion from Fortran  to just pure R code? I'm assuming pure R code
for the rest of this:

I've tried with some fairly simple C++ and C code, and that's been
fairly easy - there are a lot of details you can ignore and just try
to figure out the algorithm. It's nice if you have running software so
you can compare outputs, but I did once eventually figure out some
Pascal code from an old text book - it had enough actual example data
printed in the book that allowed me eventually to figure it out. There
were people around me who had once compiled Pascal, but it didn't
sound like it was going to be much fun.

Sometimes C and C++ chunks can be copied over directly and used with
very few changes, but it will just depend. Good luck, and I would just
jump in the deep end and send in questions if you get stuck.

Cheers, Mike.

On Wed, Jan 5, 2011 at 11:02 AM, Murray Jorgensen
<maj at stats.waikato.ac.nz> wrote:

  
    
#
On Wed, Jan 5, 2011 at 7:33 AM, lcn <lcn918 at gmail.com> wrote:

            
To make the rewrite work _well_ is the bigger problem! The easiest
way to big performance wins is going to be spotting vectorisation
possibilities in the Fortran code. Any time you see a DO K=1,N loop
then look to see if its just a single vector operation in R.

 Another way to big wins is to write test code, so you can check if
your R code gives the same results as the Fortran (C/C++) code at
every stage of the rewrite. Don't just write it all in one go and then
hope it works! Small steps....

Barry
#
Thanks Barry and thanks to others who applied off-list. I can see that I 
should have given more details about my motives for wanting to replace a 
Fortran program by an R one.

At this stage I want to get something working in pure R because it is 
easier to fool around with and tweak with than Fortran and I have a few 
things that I want to try out that will involve perturbing the original 
code and I think I'd rather be doing them in R than in a 3GL.

Now that I have publicly asked the question I find that the answer to it 
occurs to me:

The program that I want to port to R is an ML estimation by the EM 
algorithm. The iterative steps are fairly simple except they need to be 
repeated a large number of times. What I have noticed is that I can 
replace (maybe) the within-step loops by matrix multiplications. This 
means that I will, by using %*%, be effectively handing a lot of the 
work to external Fortran (or similar) routines without calling .Fortran().

OK, I know that you can see though me and I accept that I am just 
rationalising my reluctance to get into package-writing. I will bite the 
bullet on that in due course but for the meantime I'm just going to fool 
around with straight R.

Barry came closest to answering my real question and I will formulate a 
follow-up question as follows:

Does anyone know of a helpful set of examples of the vectorization of code?

Cheers,  Murray
On 6/01/2011 12:32 a.m., Barry Rowlingson wrote:

  
    
#
On Tue, Jan 4, 2011 at 4:02 PM, Murray Jorgensen
<maj at stats.waikato.ac.nz> wrote:
If the code uses functions/subroutines, keep in mind that Fortran
passes arguments by reference, whereas R passes arguments by value.

Peter
1 day later
#
I will wind this thread up with some happy comments. I have indeed 
succeeded in constructing an R program to do the same thing as my 
Fortran program for an EM algorithm. I have not done timings yet but it 
seems to run acceptably fast for my purposes.

The key code to be replaced was the E and the M steps of the algorithm. 
I decided to try to replace all the loops with matrix operations such as 
%*%, t(), crossprod(), tcrossprod(). Other operations that I used were 
of the form
                    A + v

where dim(A) = c(a, b) and length(v) = a. Here the vector v operates 
term by term down columns, recycling for each new column. [ *, - and / 
also work similarly.]

I was relived that matrices were as far as I needed to go, and I had 
visions of having to use tensor products of higher dimensioned arrays. 
Fortunately it did not come to that.

I didn't actually translate from F to R. The original is itself a 
translation of my underlying maths, and it was easier to translate the 
maths into R directly.

I preserved the form of my Fortran input and output files so that I will 
be able to run either version on the same files.

As I mentioned earlier the main point of doing all this is so that I may 
try out some variants of the program. I expect this will be much easier 
to do in R!

Thanks to all who replied.

Murray