[Rcpp-devel] can one modify array in R memory from C++ without copying it?
Christian Gunning
xian at unm.edu
Wed Nov 2 05:31:52 CET 2011
On Tue, Nov 1, 2011 at 9:11 PM,
<rcpp-devel-request at r-forge.wu-wien.ac.at> wrote:
>
>> .Call("modify", m)
>> m
> [,1] [,2] [,3] [,4] [,5]
> [1,] 1 3 5 7 9
> [2,] 2 4 6 8 10
>
>
> It didn't segfault, but the memory in R process didn't change as you see,
> although the matrix in armadillo code doubled. Which means that
> NumericMatrix copied memory from R process and passed a pointer to
> armadillo.
3 quick points:
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!
More information about the Rcpp-devel
mailing list