mclapply with RcppArmadillo

I've run into an issue for which I can't seem to find an answer. I have been developing a package and I recently incorporated some functions that rely on RcppArmadillo. These functions work great for sequential problems, but there seems to be an issue when running in parallel. Within RStudio and at the terminal, calls to lapply work fine, but mclapply just hangs. Replacing the RcppArmadillo functions with R implementations allows mclapply to evaluate. I have been hesitant to use doFuture for this task due to the time associated with spawning short lived child sessions on shared resources and the large size of exported objects. Below is an example that exhibits the same behavior as my package.

n <- 1e5
d <- 10
modelMat <- matrix(rnorm(n*d), ncol = d)
y <- rnorm(n, 1, 1)

fun <- function(x, y, col) {
    as.numeric(RcppArmadillo::fastLmPure(x[,c(1,col)], y)$coefficients)
}

# Works 
lapply(
    2:d,
    fun,
    x = modelMat,
    y = y
)

# Does not work
library(parallel)
mclapply(
    2:d,
    fun,
    x = modelMat,
    y = y,
    mc.cores = 2
)
sessionInfo()

R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

Matrix products: default
BLAS: /opt/revr/ropen/3.5.3/lib64/R/lib/libRblas.so
LAPACK: /opt/revr/ropen/3.5.3/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
[1] compiler_3.5.3            RevoUtils_11.0.3
[3] RcppArmadillo_0.9.900.1.0 Rcpp_1.0.5

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.