Bugs with Rcpp==1.0.13 (original) (raw)
Problem: update matrix passed by reference from R
Code:
Rcpp::cppFunction(" void test_arma(arma::mat& A) { A.fill(1); return; }", depends = "RcppArmadillo")
Rcpp::cppFunction(" void test_eigen(Eigen::MapEigen::MatrixXd A) { A.fill(1); return; }", depends = "RcppEigen")
Rcpp::cppFunction(" void test_rcpp(Rcpp::NumericMatrix A) { A.fill(1); return; }")
f <- function(M = 1e6, N = 1e4, init = "array", test = "arma") { if (init == "array") { A <- array(0, c(M, N)) } else { A <- matrix(0, nrow = M, ncol = N) } label <- paste0("start, ", init, ", ", test) before <- paste0("A[1, 1] = ", A[1, 1], ", A[M, N] = ", A[M, N]) x <- A[M, N] if (test == "arma") { test_arma(A) } else if (test == "rcpp") { test_rcpp(A) } else if (test == "eigen") { test_eigen(A) } after <- paste0("A[1, 1] = ", A[1, 1], ", A[M, N] = ", A[M, N]) y <- A[M, N] worked <- c("worked", "not worked")[1 + as.integer(x == y)] message(paste0(label, " before ", before, " after ", after, " ", worked)) }
for (init in c("array", "matrix")) { for (test in c("arma", "rcpp", "eigen")) { system.time(f(M = 1e6, N = 1e4, init = init, test = test)) gc() } }
Results and Sessioninfo:
Reproducible conda env:
mamba create -n r44 r-base=4.4.0 r-rcpp=1.0.13 r-rcpparmadillo r-rcppeigen
Btw, with Rcpp=1.0.12, the above code reported:
start, array, arma before A[1, 1] = 0, A[M, N] = 0 after A[1, 1] = 1, A[M, N] = 0 not worked
start, array, rcpp before A[1, 1] = 0, A[M, N] = 0 after A[1, 1] = 1, A[M, N] = 1 worked
start, array, eigen before A[1, 1] = 0, A[M, N] = 0 after A[1, 1] = 1, A[M, N] = 1 worked
start, matrix, arma before A[1, 1] = 0, A[M, N] = 0 after A[1, 1] = 1, A[M, N] = 0 not worked
start, matrix, rcpp before A[1, 1] = 0, A[M, N] = 0 after A[1, 1] = 1, A[M, N] = 1 worked
start, matrix, eigen before A[1, 1] = 0, A[M, N] = 0 after A[1, 1] = 1, A[M, N] = 1 worked
> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
time zone: Europe/Copenhagen
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.4.0 RcppEigen_0.3.4.0.0 tools_4.4.0 RcppArmadillo_0.12.8.4.0 Rcpp_1.0.12