[R] Converting Fortran or C++ etc to R

Murray Jorgensen maj at stats.waikato.ac.nz
Wed Jan 5 22:01:51 CET 2011


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 Wed, Jan 5, 2011 at 7:33 AM, lcn<lcn918 at gmail.com>  wrote:
>
>> As for your actual requirement to do the "convertion", I guess there'd not
>> exist any quick ways. You have to be both familiar with R and the other
>> language to make the rewrite work.
>
>   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

-- 
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz                                Fax 7 838 4155
Phone  +64 7 838 4773 wk    Home +64 7 825 0441   Mobile 021 0200 8350



More information about the R-help mailing list