[Rcpp-devel] can one modify array in R memory from C++ without copying it?

andre zege andre.zege at gmail.com
Wed Nov 2 05:51:57 CET 2011


Christian,

1. in my previous post  i used exactly that same constructor you are
talking about as you can see from the code i posted

2. i am not doing any math in this illustrative example, i am just
modifying a toy matrix and showing that this modification didn't propagate
back to R. Which means i am operating on a different chunk of memory, i.e.
i made a copy somewhere. In real life I need to do a some fairly involved
manipulations on several matrices each couple of gigs big, so it'd be nice
not to copy. I have armadillo code that does the job, but i wanted to call
it from R without copying the matrices. In pure R this stuff takes very
long time.

3. i am not 100% sure but this toy test that i did seem to indicate to me
that a copy is done regardless by NumericalMatrix. I need to see the code
for NumericMatrix to be absolutely sure, but i cannot explain things
otherwise

On Wed, Nov 2, 2011 at 12:31 AM, Christian Gunning <xian at unm.edu> wrote:

>
> 1:  For the NumericMatrix->arma, you can use an advanced constructor
> to get the behavior that you desire.
> http://arma.sourceforge.net/docs.html#Mat -- you'll want copy_aux_mem
> = false.
>
> 2:  Are you actually doing matrix math?  If you're just doing simple
> element-by-element arithmetic, you might get just as good performance
> with a simple loop or iterator.  You might try this first to
> understand the R/C++ process, and *then* move to using Armadillo :)
>
> 3:  For completeness, note that "Rcpp::NumericMatrix r_m(clone(mem));"
> *forces* a copy, thus restoring R's "no side-effects" semantics.
>
> -Christian
>
>
>
> --
> A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111102/78ae8bd5/attachment.htm>


More information about the Rcpp-devel mailing list