0

As a follow on from this question: Vectorised Rcpp random binomial draws - I would like to try and parallelise the Rcpp code - however it gives me errors. Example:

library(Rcpp)
library(parallel)
library(microbenchmark)

cppFunction(plugins=c("cpp11"), "NumericVector cpprbinom(int n, double size, NumericVector prob) { 
NumericVector v = no_init(n);
std::transform( prob.begin(), prob.end(), v.begin(), [=](double p){ return R::rbinom(size, p); }); 
return(v);}")


a <- runif(1e6)
b <- runif(1e6)
z <- list(a,b)


res1 <- lapply(z, function(z) rbinom(length(z), 1 , z ))
res2 <- lapply(z, function(z) cpprbinom(length(z), 1 , z ))
microbenchmark(rbinom(length(a), 1, a), cpprbinom(length(a), 1, a))

cores<-2
cl <- makeCluster(cores)
    clusterExport(cl, c("cpprbinom"))
    clusterSetRNGStream(cl=cl, 5)
    res3 <- parLapply(cl=cl, z, function(z) cpprbinom(length(z), 1 , z ))
stopCluster(cl)

The parallel version throws the error: Error in checkForRemoteErrors(val) : 2 nodes produced errors; first error: NULL value passed as symbol address

Can anyone help me to parallelise the Rcpp code ? (or is this very complicated ?)

I'm also curious as to whether I could use my GPU to generate random booleans, but I know zero about R-GPU programming and so don't really know how to frame such a question properly.

Edit: This has been marked as a duplicate of this question: snow, inline & Rcpp: however, I do not consider that answer there adequate as it relies on the questioner having alot of specific knowledge and I am unable to follow it.

Community
  • 1
  • 1
user2498193
  • 1,072
  • 2
  • 13
  • 32
  • 2
    Try using `RcppParallel` or putting your code in a package so that it may be accessed from multiple cores. – nrussell Aug 29 '15 at 11:16
  • Thanks I appreciate the answers but I'm really not good enough at this to figure out how to either of those things just from your suggestions. The above is my only ever use of Rcpp and I dont' know how to make a package (nor 100% sure I know what that means).I would veyr much appreciate suggested edits to the code above if you have the time – user2498193 Aug 29 '15 at 11:23
  • 1
    Read R documentation about how to make a package; read Rcpp documentation about how to make a package with Rcpp. Expecting us to re-explain it all to you is simply not a fair demand on our time. – Dirk Eddelbuettel Aug 29 '15 at 12:21
  • I have looked at the documentation - I don't know enough to understand it. I find the Rcpp documentation impenetrable. I read Hadley's book about making packages - but I still can't actually do it yet. I thought the point of producing a reproducible example was so that others could edit your code ? I don't think you guys always appreciate that we are not all developers asking questions. And with respect, I can reach the answer "read the manual" without posting here. I tried that and I'm still lost. – user2498193 Aug 29 '15 at 12:41
  • 2
    I'm sorry that you are having trouble with this, but don't let that - or the fact that your question was marked as a duplicate - discourage you. Yes, you provided a reproducible example, and that's a good thing. However, the overall process of creating an R package involves at least several steps. As Dirk pointed out, this is not something one can briefly explain, nor is it (taken as a whole) on topic for SO. Having said that, I put together a *very* [minimal example](https://github.com/nathan-russell/SmallPackage) to get you started. – nrussell Aug 29 '15 at 15:56
  • 1
    My advice would be to install RStudio if you are not already using it, as it greatly simplifies the R/Rcpp packaging process. [This vignette](https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-package.pdf) is definitely a great starting point for Rcpp-based packages in particular. And, if you get stuck on something and cannot find an answer online, you can always ask *specific* questions here. – nrussell Aug 29 '15 at 16:04
  • 1
    Thanks nrussell much appreciated. I will take some time to go through that example you kindly provided. Thank you for the vignette also – user2498193 Aug 29 '15 at 17:11
  • @nrussell: Good work as usual but are you aware that e.g. `unitTests/testRcppPackage/` provides a complete test package with Rcpp? And that `Rcpp.package.skeleton("foo")` creates a test package `foo`? That is all RStudio does -- call that function ... – Dirk Eddelbuettel Aug 29 '15 at 20:55

0 Answers0