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

Michael Kane kaneplusplus at gmail.com
Thu Mar 14 17:33:33 CET 2013


Shradda,

A big.matrix is not a regular R matrix. It is a completely separate type.
The definition can be found in BigMatrix.h. I've rewritten your C++ code
but there is still a problem. To build the function the compiler needs to
know where the bigmemory header files are. Basically, you need to #include
"MatrixAccessor". I'm not sure how to do this but it is almost certainly
somewhere in the documentation or maybe someone else on the list will nice
enough to give you a pointer.

Thanks,
Mike

matFn <- '
Rcpp::XPtr<BigMatrix*> bigMat(A);
MatrixAccessor 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[i,j] = Am[i,j] + Am[i-1,j];
        }
}
return A;
';
# 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"
suppressMessages(library(bigmemory))
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)

bigmat2 <- funx(matDesc)

On Thu, Mar 14, 2013 at 11:57 AM, Shraddha Pai <Shraddha.Pai at camh.ca> wrote:

> 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
> non-matrix.
>
> 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
> -----
> Shraddha Pai
> Krembil Family Epigenetic Research Laboratory
> Centre for Addiction and Mental Health, Toronto
> ---------------------------------------------------------------------------
> ----------
>
>
> Example:
> -----
> 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"
> suppressMessages(library(bigmemory))
> 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)
>
> bigmat2 <- funx(matDesc)
>
>
>
> ______________________________________________________________________
> This email has been scanned by the CAMH Email Security System.
> ______________________________________________________________________
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130314/b0e91d57/attachment.html>


More information about the Rcpp-devel mailing list