[Rcpp-devel] RcppArmadillo matrix with writeable auxiliary memory

Steven Varga steven.varga at gmail.com
Tue Nov 26 05:23:24 CET 2013

Thank you Dirk for the quick response;
and for noting the memory handling.

I am aware of it and there are other ways of obtaining a pointer to some
memory location. I used malloc/free to put focus on arma::mat copying data
during assignment and/or not able to use reference as class member.
Leaving plenty of room for that I am doing it wrong and there is a way to
initialize a class member arma::mat with external ptr.

The documentation tells about arma::mat behaviour when setting ..., false,
strict=true); in my interpretation the last argument prevents changing size
of arma::mat resulting in a 'copy free' instance. The provided c++ only
example demonstrates that; it works as documented.

What I am looking for is setter on arma::mat to flexibly point to some
memory location or a different '=' operator in terms of handling arma::mat
objects when initialized with external pointer.

It is an armadillo question but came accross your post on stackoverflow
where you suggested someone with similar question to post here.

Bigmemory looks great;  but the point here is to able to use matlab like
BLAS level 3 operations on some large vector; small segements at a time.

We use HDF5 for large datastores with flexible STL container like interface.

Thanks again,
 On Nov 25, 2013 10:28 PM, "Dirk Eddelbuettel" <edd at debian.org> wrote:

> Steven,
> In a case like this I would try to set up a fist minimal working example
> using just plain C++, and no R. Once that works, try it with R.
> For Armadillo, the docs at http://arma.sourceforge.net/docs.html#Mat are
> your
> best shot.  I presume you saw what it says about strict=true. Also, in
>    double* p = static_cast<double*>( malloc( 30*sizeof(double) ));
>    arma::mat M(p, 3,10, false, true ); // this works nicely
> you are going against the "Writing R Extensions" rule of mixing system
> malloc/free with R.  That is generally a bad idea.
> If your intent is to keep memory 'fixed' and outside of R, you can use
> Bigmemory objects via external pointers (Rcpp::XPtr). This may be of
> interest: http://gallery.rcpp.org/articles/using-bigmemory-with-rcpp/
> That should extend to wrapping Armadillo around such memory. After all,
> Armadillo behaves just _great_ with the R-allocated objects we pass back
> and
> forth.
> Hope this helps,  Dirk
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131125/1629622d/attachment.html>

More information about the Rcpp-devel mailing list