[Rcpp-devel] Filling a big.matrix in Rcpp

Shraddha Pai Shraddha.Pai at camh.ca
Thu Mar 14 16:57:14 CET 2013

Hi all,
I'm trying unsuccessfully to process a big.matrix in a c++ function in the
simple example below.

I first tried to pass the object itself ("bigmat") and got an "Error: not
a matrix" message during compile time. So I'm now trying to pass the
matrix descriptor ("matDesc"). From previous posts on rcpp-devel I *think*
I need to pass it as an external pointer, but this isn't working either.
The compile gives me an error about using matrix operations (nrow()) on a

How can I correctly pass a big.matrix reference in a way that the C++
function can dereference it inside the function and use matrix operations?

I've just started using Rcpp (yesterday!), and haven't programmed in C++
for over 10 years now, so my constructs may not be quite right. A related
question: if I wanted to parallel process matrix operations, is it better
to use doMC in the R layer, or something in the C++ layer? Especially if
we're interacting with a big.matrix?
Are there better options (R/Rcpp libraries) for dealing with large vectors
(6M) or matrices (6M x 200), where CPU and memory mgmt are both a concern?

Thanks in advance,
Shraddha Pai
Krembil Family Epigenetic Research Laboratory
Centre for Addiction and Mental Health, Toronto

matFn <- '
Rcpp::XPtr<*NumericMatrix> Am(A);
int nrows = Am.nrow();
int ncolumns = Am.ncol();
for (int j = 0; j < ncolumns; j++){
	for (int i = 1; i < nrows; i++){
		Am(i,j) = Am(i,j) + Am(i-1,j);
return Am;
# same function declaration as in example 2.
funx <- cxxfunction(signature(A="externalptr"), body=matFn, plugin =
"Rcpp" )

# 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,
matDesc <- bigmemory::describe(bigmat)

bigmat2 <- funx(matDesc)

This email has been scanned by the CAMH Email Security System.

More information about the Rcpp-devel mailing list