samedi 29 août 2015

How to parallelise Rcpp code that generate random booleans

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.




Aucun commentaire:

Enregistrer un commentaire