[Rcpp-devel] How many copies of my object do I make?

Douglas Bates bates at stat.wisc.edu
Tue Dec 10 18:48:06 CET 2013


You can avoid one copy operation by declaring X as

 const MSpMat X(as<MSpMat>(xx_));

As it is you are allocating storage for a copy of the sparse matrix which
will not be modified and hence does not need to be copied.


On Tue, Dec 10, 2013 at 11:33 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

>
> On 9 December 2013 at 22:14, Søren Højsgaard wrote:
> | Dear all,
> | Using RcppEigen I've created a function for converting a sparse matrix
> (a dgCMatrix) to a standard dense matrix:
> |
> | typedef Eigen::SparseMatrix<double> SpMatd;
> | typedef Eigen::MappedSparseMatrix<double> MSpMat;
> |
> | // [[Rcpp::export]]
> | SEXP do_dgCMatrix2matrix ( SEXP XX_ ){
> |   S4 DD(wrap(XX_));
> |   List dn = clone(List(DD.slot("Dimnames")));
> |   SpMatd X(as<SpMatd>(XX_));
> |   Eigen::MatrixXd dMat;
> |   dMat = Eigen::MatrixXd( X );
> |   NumericMatrix Xout(wrap(dMat));
> |   Xout.attr("dimnames") = dn;
> |   return Xout;
> | }
> |
> | The function does what I expect it to, but I am curious about how many
> times I really create some sort of "copy" of my input matrix and when all I
> do is put a small layer of "vernish" over my input matrix?
> |
> | 1) dMat is dense matrix, and that must (I guess) refer to new "object"??
> |
> | 2) X is a sparse matrix, and I suppose it is a new object??
> |
> | 2') If I had declared it to be MSpMat, then it would really just be the
> input object with a few bells and whistles??
> |
> | 3) But how about DD and Xout? Are they new objects (in terms of memory
> usage)?
> |
> | Put differently, if my input matrix occupies B bytes of memory, how many
> times B have I then created along the way?
>
> Unsure. You really need to look.
>
> For sure when i) clone is called, ii) each time you pass from sparse to
> dense
> and back.  For the others, you need to dig deeper.
>
> Dirk
>
> --
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131210/58376462/attachment.html>


More information about the Rcpp-devel mailing list