I have issues with mcmapply
that only occures if an Rcpp-function, testfun()
, is first called outside mcmapply
and then called through mcmapply
. The issue only exists if:
testfun()
usesarma::exp()
(orarma::sqrt()
, etc) on a matrix with at least 18×18 elements and stores the result asarma::mat
mcmapply
uses parallelization (mc.cores
>1)
If I call mcmapply
with testfun()
, it runs and finishes without errors. If I first call testfun()
separately and afterwards mcmapply
, then mcmapply
stalls.
MWE:
Rcpp::sourceCpp(code=
" #include <RcppArmadillo.h>;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void testfun(int n) {
arma::mat foo = arma::sqrt(arma::zeros(n,n));
}")
library(parallel)
print("Run mcmapply+testfun: works")
mcmapply(mc.cores=2, function(x) testfun(100), 1:2)
print("Calling testfun separately: works")
testfun(100)
print("Run mcmapply+testfun after having run testfun separately: stalls")
mcmapply(mc.cores=2, function(x) testfun(100), 1:2)
I suspect the issue has to do with storing the matrices, as it only occurs if testfun()
assigns the result of arma::sqrt()
to an arma::mat
.
Setup: linux with R 4.4.0, gcc 14.2.0, and gsl 2.7.1, but issue also replicated on older R versions.