[Rcpp-devel] Delayed usage of auxiliary memory in RcppArmadillo

romain at r-enthusiasts.com romain at r-enthusiasts.com
Tue Jun 11 00:02:53 CEST 2013


Some C++ 101 insights below.

Le 2013-06-10 23:07, Simon Zehnder a écrit :
> Dear Rcpp::Devels and Rcpp::Users,
>
> I am temporarily experiencing with a delayed assignment of auxiliary
> memory in RcppArmadillo:
>
> Declare a matrix from which you know you may need it
>
> arma::mat maybeM;
>
> (In C++ a declaration makes only known the type and name but 
> allocates
> no storage yet).

SO WRONG.
here the default constructor or arma::mat is called. What it does is 
entirely up to the implementation of the class.

> Now you check a condition and if it is true, you will need the 
> matrix,
> so you initialize it
>
> if(mycondition) {
>  maybeM = arma::mat(aux_mem*, n_rows, n_cols, copy_aux_mem = false,
> strict = true)
> }
>
> (for simplicity I only printed here the default constructor for using
> auxiliary memory; please assume that all values are presented)

wrong again.

what happens here is in two steps.

  - first, an arma::mat is constructed, using the so called advanced 
constructor. this does use the auxiliary memory to create a temporary.
  - second, the assignment operator is used to copy the temporary into 
"maybeM"

So indeed maybeM does not use the auxiliary memory, I hope now you 
understand why.

> The resulting matrix maybeM does not use the auxiliary memory. I 
> tried
> to access in some way the 'mem' member in the Armadillo class, but
> this member is constant and can not be
> manipulated after first construction. A primer declaration and later
> initialization is a very common pattern in C++, but it seems not be
> possible in case of reusing memory.

Everything is possible, it is a decision made by the implementation. 
e.g. for Rcpp classes copy construction means just cheap pointer copies, 
not data copies.
armadillo is implemented with more traditional copy semantics.

> I conclude, that a delayed assignment of auxiliary memory is not
> possible.

wrong conclusion. what you demonstrated here is that you need to study 
basic c++ (assignment operators, copy construction, ...) :)

now, things would be different with move semantics in c++11. Not sure 
armadillo has implemented them already, but I'm pretty sure that is not 
what you were talking about.

Romain

> Best
>
> Simon


More information about the Rcpp-devel mailing list