[Rcpp-devel] Beginner Question: Two misc. questions on usage of Rcpp

Dirk Eddelbuettel edd at debian.org
Sun Jan 30 22:16:20 CET 2011


Howdy,

Thanks for posting here, and the kind words below.

On 30 January 2011 at 15:49, Sunny Srivastava wrote:
| Dear Rcpp-List:
| 
| I have been working on speeding my R code. A while ago, I realized that no
| further vectorization would help and interacting R with C/C++/.. was absolutely
| necessary. I don't program in C/C++ extensively, so the transition was a
| difficult process. I tried to use the R API but the error messages were
| intimidating. A few weeks ago I saw Professor Chambers presentation (pdf from
| David Smith's website) about R in Stanford. I saw him mentioning Rcpp which
| motivated me to try Rcpp (I confess, I am lazy to try new stuff). And I haven't
| been dissapointed. Thank you very much Dirk and Romain (and other developers,
| including Professor Chambers and Professor Bates).
| 
| Rcpp reduced the length of my code considerably (like: I didn't need to bother
| about PROTECT .. ) and the informative syntax (like: Rcpp::IntegerMatrix ..)
| made my code more readable. These are great advantages, but the best part was
| testing and compiling with the Inline package. I am a statistician; writing a C
| program and then using dyn.load was the way I learned to do it in grad school.
| Rcpp + inline makes this process way easier.
| 
| Thank you for the great documentation on your webpage Dirk. It helped me a lot.
| 
| I am still a beginner in using Rcpp and friends (2 weeks old) and have a few
| miscellaneous questions. The answer to these questions may be very basic or
| obvious, so please bear with me. (Dirk showed me a simple way to test things
| using Rcpp + inline here -- http://stats.stackexchange.com/questions/6690/
| what-is-the-difference-between-rf-dpois-in-rmath-h-and-the-dpois-that-i-use-direc
| )
| 
| 1. I have been using Rcpp + inline to test if the faster implementation gives
| the right results (which is obtained from R code). I was wondering if there are
| other strategies which people use to test their Rcpp code? (sorry, if this
| doesn't make sense.)

No, that is precisely the best way about it: write code, and then test and
profile it.  

A few times in the past users have posted things here that lead us to improve
things down the road.  One only finds bottlenecks by testing for them.

| 2. I have a MCMC simulation (say: foo) which uses several functions (say: fun1,
| fun2). These functions (fun1, fun2) are themselves computation intensive and I
| used Rcpp to speed them. Until now I do something like this:
| 
| In R file:
| 
| foo <- function (...){
| 
|     src1 <- paste(readLines("fun1-rcpp.cpp"), collapse="\n")
|     RcppFun1 <- cxxfunction( .., plugin="Rcpp",body=src1)

You probably want to write a package. That way the 'compilation cost' is
borne just once when you install the package, rather than each time its run.

Doing a package may be a new step, but we try to help with documentation and
existing packages. Give it a try!

|    ## similar cpp code for fun2
| 
|     for (i in 1:10000){
|           ## use RcppFun1, RcppFun2 here
|     }
| }

It's a bit hard to say based on just this, but yes, you may be able to
'refactor' this and regroup functions.  But no need to obsess over it.
 
| I was wondering if this is OK (it already speeds up my code by zillion times)
| or is there a more elegant way of doing this. I am thinking about using Rcpp to
| increase the speed of foo (for more speed) but I am not too sure on how to do
| this. Towards this end, I checked a few packages mentioned on Dirk's website
| namely, pcaMethods. I am thinking RcppExport and .Call is the way to do this?

Yes. Give it try, look at some small packages and try to copy whatever
package structure you may find useful.

Cheers, Dirk

 
| Thank you for your answers or pointers.
| 
| S.   
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list