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

Romain Francois romain at r-enthusiasts.com
Fri Mar 15 00:51:13 CET 2013



Le 14 mars 2013 à 19:33, Dirk Eddelbuettel <edd at debian.org> a écrit :

> 
> On 14 March 2013 at 12:24, Smith, Dale wrote:
> | Write it up for Rcpp Gallery.
> 
> Yup. Our thoughts too.  Here is a slightly modified version which works as a
> single cpp file thanks to the wonders of sourceCpp() --- bigmemory headers
> are found automagically, and the example is executed too.
> 
> 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]]
> bool 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 Rcpp::wrap(true);
> }

Why do you use wrap here ? Wrap will create a SEXP, not a bool !

Actually why is this not void ?

Romain



> // 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


More information about the Rcpp-devel mailing list