[Rcpp-devel] Example of rcpp usage with Rmpfr, mpfr, gmp.

Dirk Eddelbuettel edd at debian.org
Sun Apr 22 17:49:48 CEST 2012


Hi Thell,

On 22 April 2012 at 10:17, Thell Fowler wrote:
| First, let me say what a great and I truly mean great tool inline and Rcpp have
| been, along with R2.15-devel and the related Rtools ( now, no longer -devel, so
| congrat on that! ).

Thanks!
 
| I'm doing a massive amount of calculations with the hypergeometric
| distribution, that in my original R code would take about 86 seconds to process
| the first 100 tests, and after using Rcpp inline to prototype a better
| solution, I was able to create a package (fairly easily) using the skeleton
| function ( on Windows even ), then used parallel with StarCluster on the Amazon
| AWS to process just shy of 400,000 tests in about 59 hours across 120 cores. 
| Everything was great!  I then got about 40 pages into the writeup of the
| results, theory, and proofs and found an error!

Sounds like a serious scientist at work. And presumably, finding an error now
is better now than later...
 
| The error was caused by floating point calculation 'skewing' and now I need to
| solve that!  Manual tests using Rmpfr at 160 bits, shows the anticipated
| results ( at quad-precision 113 bit accuracy ).
| 
| Needless to say, this is going to really slow things down, but rather than jump
| ship from R to a c++/boost/mpfr solution I'd like to be able to stick with R,
| as that is what all the research has been done on for the last 18 months or
| so...
| 
| Simple and straight forward...
| 
| My two use cases are:
| 
| 1)  Pass input as 'numeric', convert to multiprecision 160-bit, do
| calculations using multiprecision lgamma values, and return an mpfr array
| object.
| 2)  Pass input as mpfr objects (list or array), do calculations, and return
| the modified values.
| 
| Any examples or direction on how (if I can) to get to a Rcpp Rmpfr bridge with
| speed would go a long way...

Sounds fine. Not sure how to return (or pass down) the mpfr arrays. But you may be able to
look at the existing CRAN packages wrapping mpfr for ideas.

As a really simple minded idea, you can

   1) use Rcpp to pass values from R down to a C/C++ layer

   2) "cast" or copy your values into mpfr et al types, do you computations
   and get the result, cast/copy it back to C/C++

   3) use Rcpp to communicate back up

which is pretty standard use of Rcpp to call external libraries or
packages. Many of the CRAN packages using Rcpp do something similar. 

You just need to add the required -I and -L flags to src/Makevars and you
should be fine. [ Higher-end solutions use configure, or pkg-config, or
... to find those values. Harcoded may work on standard Linux systems. ]

[ And if you want to look into doing more of an integration (a la RcppArmadillo,
RcppGSL, RcppEigen, ...), then the Rcpp-extending vignette shows you how to
modify as<>() and wrap() to get automatic converters going.  But you surely
don't need to do this for a first attempt to compute your stuff. I just
mention it as you say 'R mpfr bridge' -- this would be the Rcpp way of doing
it. ]

Cheers, Dirk

-- 
R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com


More information about the Rcpp-devel mailing list