RcppEigen 不能 return 矩阵到具有超过 2^31 个元素的 R

RcppEigen cannot return matrix to R with more than 2^31 elements

我一直在使用 Rcpp 和 RcppEigen 包进行一些矩阵计算,并注意到如果返回给 R 的矩阵长度超过 .Machine$integer.max 就会产生错误。这是一个可重现的例子:

test_rcpp.cpp

// [[Rcpp::depends(RcppEigen)]]

#include <RcppEigen.h>

using namespace Rcpp;

// [[Rcpp::export]]
SEXP testM(const Eigen::Map<Eigen::MatrixXd> A) {
    Eigen::MatrixXd C = A * A.transpose();
    return List::create(Rcpp::Named("first") = C.block(0,0,C.rows()/2,C.cols()),
                      Rcpp::Named("second") = C.block(C.rows()/2,0,C.rows()/2+1,C.cols()));
}

// [[Rcpp::export]]
SEXP testM2(const Eigen::Map<Eigen::MatrixXd> A) {
    Eigen::MatrixXd C = A * A.transpose();
    return wrap(C);
}

test_rcpp.R

library(Rcpp)

sourceCpp("./test_rcpp.cpp")

A <- matrix(rep(1, ceiling(sqrt((.Machine$integer.max)))), nrow=ceiling(sqrt(.Machine$integer.max)))

tm <- do.call(rbind, testM(A))

tm2 <- testM2(A)

运行testM2(A)returns一个错误Error in testM2(A) : negative length vectors are not allowed。目前,testM(A) 是我的解决方法,它将矩阵分成两半,returns 是两半的列表。

这是有意为之的行为吗?如果是这样,还有哪些其他解决方法?

link had some information but didn't help me specifically with this problem. A similar 表明当矩阵的维度超过 2^31 时会遇到问题。在这种情况下,我返回的矩阵的维度 c(46341, 46341),远低于矩阵索引的 2^31 限制,并且包含 2147488281 个元素,远低于 long 向量的 2^52 限制.

sessionInfo() 信息的子集:

R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server release 6.3 (Santiago)

注意:我在 R 版本 3.4.2 上遇到同样的问题。

这是当前 RcppEigen 实现的一个限制。示例:

// [[Rcpp::depends(RcppEigen)]]    
#include <RcppEigen.h>

// [[Rcpp::export]]
void get_length_rcpp(Rcpp::IntegerMatrix m){
  Rcpp::Rcout << m.nrow() << ' ' << m.ncol() << ' ' 
              << (m.nrow() * m.ncol()) << ' ' << m.size(); 
}

// [[Rcpp::export]]
void get_length_eigen(Eigen::Map<Eigen::MatrixXi> m){
  Rcpp::Rcout << m.rows() << ' ' << m.cols() << ' ' 
              << (m.rows() * m.cols()) << ' ' << m.size(); 
}

/*** R
N <- 5e4
A <- matrix(1L, ncol = N, nrow = N)
get_length_rcpp(A)
get_length_eigen(A)
*/

输出:

> N <- 50000

> A <- matrix(1, ncol = N, nrow = N)

> get_length_rcpp(A)
50000 50000 -1794967296 2500000000
> get_length_eigen(A)
Error in get_length_eigen(A) : 
  long vectors not supported yet: ../../src/include/Rinlinedfuns.h:519
Calls: <Anonymous> ... withVisible -> eval -> eval -> get_length_eigen -> .Call
Execution halted

为此,我在 github 上开了一个 issue and a pull request