Skip to content
Prev 3101 / 10988 Next

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

On Fri, Nov 4, 2011 at 8:36 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
The distinction between a C++ object that allocates its own storage
and one that uses the storage allocated by R is explicit in RcppEigen.
 The templated class Eigen::Map uses the storage from the SEXP and
throws an error if, for example, you pass integers to a double
precision vector.  In the RcppEigen-Intro vignette that Dirk and I are
writing we mention that, because of this, you really want to declare
such objects as const.

The vector structure for double precision values in Eigen is called VectorXd so

const  Eigen::Map<Eigen::VectorXd>   foo(as<Eigen::Map<Eigen::VectorXd> >(Foo));

is guaranteed to use R's storage for the vector or throw an error.

The topic of this thread, modifying an array in R memory from C++, is
a no-no.  There are occasions, such as optimizing models with complex
structures or MCMC, where you want to modify the state of the object
without copying a large structure every time but, in these cases, it
is better to keep the volatile storage in the C++ object and expose it
through methods and fields in a reference class object in R.  Rcpp
modules are one way of doing this.  I ended up rolling my own method
in the lme4Eigen package (only available on R-forge at present)
because of the need to allow for serialize/unserialize operations on
the reference class object.

Thread (18 messages)

Christian Gunning can one modify array in R memory from C++ without copying it? Nov 1 andre zege can one modify array in R memory from C++ without copying it? Nov 1 Christian Gunning can one modify array in R memory from C++ without copying it? Nov 2 andre zege can one modify array in R memory from C++ without copying it? Nov 3 Darren Cook can one modify array in R memory from C++ without copying it? Nov 3 Darren Cook can one modify array in R memory from C++ without copying it? Nov 4 Romain Francois can one modify array in R memory from C++ without copying it? Nov 4 Darren Cook can one modify array in R memory from C++ without copying it? Nov 4 Dirk Eddelbuettel can one modify array in R memory from C++ without copying it? Nov 4 andre zege can one modify array in R memory from C++ without copying it? Nov 4 Dirk Eddelbuettel can one modify array in R memory from C++ without copying it? Nov 4 Steve Lianoglou can one modify array in R memory from C++ without copying it? Nov 4 Dirk Eddelbuettel can one modify array in R memory from C++ without copying it? Nov 4 Steve Lianoglou can one modify array in R memory from C++ without copying it? Nov 4 Douglas Bates can one modify array in R memory from C++ without copying it? Nov 4 Darren Cook can one modify array in R memory from C++ without copying it? Nov 6 Dirk Eddelbuettel can one modify array in R memory from C++ without copying it? Nov 6 Davor Cubranic can one modify array in R memory from C++ without copying it? Nov 6