I undertand from this post that the easiest way to use Rcpp
functions with doParallel on window is to build a package. I am currently building an R package and I need to parallelise some tasks in the package. I have an Rcpp function and an R function that performs a loop on the Rcpp function. These functions will be in my package. How can I construct the R function?
According to the post above, It will be easier to build a first package with the Rcpp function an call this package in the second package of the R function. Is there a way to put the both functions in the same package?
1 Answers
Yes, you can put as many functions as you want within the package. The reason for suggesting everything is in an R package is because you would otherwise have to compile the code on every thread or node that you spin your code up on. This is because the Rcpp functions are compiled locally and only have a thread-specific pointer reference. In particular, see the discussion in: Using Rcpp functions inside of R's par*apply functions from the parallel package.
Sample package would be:
https://github.com/r-pkg-examples/rcpp-and-doparallel
In particular, the R function should correctly setup and teardown the parallel backend.
mean_parallel_compute = function(n, mean = 0, sd = 1,
n_sim = 1000,
n_cores = parallel::detectCores()) {
# Construct cluster
cl = parallel::makeCluster(n_cores)
# After the function is run, close the cluster.
on.exit(parallel::stopCluster(cl))
# Register parallel backend
doParallel::registerDoParallel(cl)
# Compute estimates
estimates = foreach::foreach(i = iterators::icount(n_sim), # Perform n simulations
.combine = "rbind", # Combine results
# Self-load
.packages = "Rcpp2doParallel") %dopar% {
random_data = rnorm(n, mean, sd)
result = mean_rcpp(random_data) # or use Rcpp2doParallel::mean_rcpp()
result
}
estimates
}
To pass R CMD check
make sure to have the following roxygen2
import tags:
#' @importFrom foreach %dopar% foreach
#' @importFrom iterators icount
#' @importFrom doParallel registerDoParallel
In addition, make sure DESCRIPTION
has the following:
LinkingTo:
Rcpp
Imports:
doParallel,
Rcpp,
foreach,
iterators,
parallel
Some other examples:

- 20,011
- 13
- 69
- 84
-
Thank you for your reply After installing the package I get this error message Error in foreach::foreach(i = iterators::icount(nphiH0), .combine = "c", : could not find function "%dopar%" – Ari.stat Nov 12 '19 at 23:28
-
That isn't from my package... That is from your package. The `iterators::icount(nphiH0)` gives that away. You need to make sure to generate the `NAMESPACE` file with an `importFrom(foreach, "%dopar%")` or use roxygen2's `#' @importFrom foreach %dopar% foreach` and, then, run `roxygenize()`. – coatless Nov 12 '19 at 23:35
-
Ok I check the package and add `importFrom(doParallel,registerDoParallel)`, `importFrom(foreach,"%dopar%")`, `importFrom(foreach,foreach)`, `importFrom(iterators,icount)` to the namespace and it works well. I am currently on Linux. I can install the package. Will this be compiled on window too? – Ari.stat Nov 13 '19 at 00:52
-
1Yes, if the user has installed Rtools alongside of R on Windows as the package must be compiled due to the C++ components. – coatless Nov 13 '19 at 00:55