我正在尝试学习和使用 Rcpp 和 RcppArmadillo 用于稀疏线性代数例程。
下面的代码是这里示例的改编:http: //gallery.rcpp.org/articles/armadillo-sparse-matrix/
code <- '
S4 matx(x);
IntegerVector Xd = matx.slot("Dim");
IntegerVector Xi = matx.slot("i");
IntegerVector Xp = matx.slot("p");
NumericVector Xx = matx.slot("x");
arma::sp_mat Xsp(Xd[0], Xd[1]);
// create space for values, and copy
arma::access::rw(Xsp.values) = arma::memory::acquire_chunked<double>(Xx.size() + 1);
arma::arrayops::copy(arma::access::rwp(Xsp.values),
Xx.begin(),
Xx.size() + 1);
// create space for row_indices, and copy -- so far in a lame loop
arma::access::rw(Xsp.row_indices) = arma::memory::acquire_chunked<arma::uword>(Xx.size() + 1);
for (int j=0; j<Xi.size(); j++)
arma::access::rwp(Xsp.row_indices)[j] = Xi[j];
// create space for col_ptrs, and copy -- so far in a lame loop
arma::access::rw(Xsp.col_ptrs) = arma::memory::acquire_chunked<arma::uword>(Xp.size() + 1);
for (int j=0; j<Xp.size(); j++)
arma::access::rwp(Xsp.col_ptrs)[j] = Xp[j];
// important: set the sentinel as well
arma::access::rwp(Xsp.col_ptrs)[Xp.size()+1] = std::numeric_limits<arma::uword>::max();
// set the number of non-zero elements
arma::access::rw(Xsp.n_nonzero) = Xx.size();
Rcout << "SpMat Xsp:\\n" << arma::dot(Xsp,Xsp) << std::endl;
'
norm2 <- cxxfunction(signature(x="Matrix"),
code,plugin="RcppArmadillo")
当我使用 1e4 的向量时,一切正常:
> p <- 10000
> X <- Matrix(rnorm(p),sparse=TRUE)
> norm2(X)
SpMat Xsp:
9997.14
NULL
但是,当我使用长度为 1e5 的向量时,会产生错误
> p <- 100000
> X <- Matrix(rnorm(p),sparse=TRUE)
> norm2(X)
error: SpMat::init(): requested size is too large
Error:
>
我似乎无法弄清楚我做错了什么。任何指针将不胜感激。
============== 更多信息 ==============
问题似乎在于尺寸 >= 2^16=65536
以下作品:
> m <- 1000
> n <- 65535
> nnz <- 10000
> iind <- sample.int(m,nnz,replace=TRUE)
> jind <- sample.int(n,nnz,replace=TRUE)
> xval <- rnorm(nnz)
> X <- sparseMatrix(i=iind,j=jind,x=xval,dims=c(m,n))
> norm2(X)
SpMat Xsp:
10029.8
NULL
以下不起作用:
> m <- 1000
> n <- 65536
> nnz <- 10000
> iind <- sample.int(m,nnz,replace=TRUE)
> jind <- sample.int(n,nnz,replace=TRUE)
> xval <- rnorm(nnz)
> X <- sparseMatrix(i=iind,j=jind,x=xval,dims=c(m,n))
> norm2(X)
error: SpMat::init(): requested size is too large
Error:
>
为什么会这样?