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

Dirk Eddelbuettel edd at debian.org
Fri Mar 15 01:57:56 CET 2013


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  


More information about the Rcpp-devel mailing list