[Rcpp-devel] Filling a big.matrix in Rcpp
Hi all, Thanks for all the responses and for clarifying what big matrices are. I had actually looked through the rcpp-devel site before attempting to use big.matrix with Rcpp. I probably misunderstood this thread about bigmemory; it sounded like bigmemory objects worked "out-of-the-box" with Rcpp (and maybe Xptr could play a role). http://r.789695.n4.nabble.com/looking-for-adice-on-bigmemory-framework-with -C-and-java-interoperability-td4610333.html I presume the below is consistent with Michael's earlier post suggesting use of a BigMatrix pointer, and that the old thread was just discussed the data structure of big.matrix objects (rather than that they can be used as "just doubles").
bigmemory matrices are simply arrays of native types (typically doubles, but bm supports other types, too) so they are trivially readable/writable from both C++ (just read into memory and cast to the array type)
I'll try the code posted below and confirm. Regards, Shraddha
On 13-03-14 8:57 PM, "Dirk Eddelbuettel" <edd at debian.org> wrote:
On 15 March 2013 at 00:51, Romain Francois wrote:
| Why do you use wrap here ?
Reflex, because I returned an atomistic type.
| Wrap will create a SEXP, not a bool !
Quite right but was in a rush while doing other things too. "Forgot" that
I
was using sourceCpp() which give one additional layer of indirection.
| Actually why is this not void ?
Quite right. Better version below. Thanks!
Ultimately, we do of course want a version that takes an XPtr to a
big.matrix
and does something sensible and returns it.
Dirk
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode:
nil; -*-
#include <Rcpp.h>
// The next line is all it takes to find the bigmemory
// headers -- thanks to the magic of Rcpp attributes
// [[Rcpp::depends(bigmemory)]]
#include <bigmemory/MatrixAccessor.hpp>
// We define a simple function, and pass the incoming XPtr as a SEXP;
// we could also pass a templated XPtr. Function returns only a bool.
// [[Rcpp::export]]
void fun(SEXP A) {
Rcpp::XPtr<BigMatrix> bigMat(A);
MatrixAccessor<int> Am(*bigMat);
int nrows = bigMat->nrow();
int ncolumns = bigMat->ncol();
for (int j = 0; j < ncolumns; j++){
for (int i = 1; i < nrows; i++){
Am[j][i] = Am[j][i] + Am[j][i-1];
}
}
return;
}
// R code for testing below
/*** R
require(bigmemory)
# set up big.matrix
nrows <- 10000
bkFile <- "bigmat.bk"
descFile <- "bigmatk.desc"
bigmat <- filebacked.big.matrix(nrow=nrows, ncol=3,type="integer", init=1,
backingfile=bkFile, backingpath=".",descriptorfile=descFile,
dimnames=c(NULL,NULL))
matDesc <- bigmemory::describe(bigmat)
fun(bigmat at address)
cat("Done\n")
*/
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
_______________________________________________ Rcpp-devel mailing list Rcpp-devel at lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel ______________________________________________________________________ This email has been scanned by the CAMH Email Security System. ______________________________________________________________________
______________________________________________________________________ This email has been scanned by the CAMH Email Security System. ______________________________________________________________________