[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:10:58 CET 2011


Slava, thanks for your pointers. I changed the code to the way Dirk example
goes, namely
------------------------------------------------------
#include <RcppArmadillo.h>

RcppExport void modify(SEXP mem){
  Rcpp::NumericMatrix r_m(mem);
  arma::mat m(r_m.begin(), r_m.nrow(), r_m.ncol(), false);
  m.print();
  m=m+m;
}
-----------------------------------------------------------------
i compiled it, and run shared lib from R doing
>m
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

>dyn.load("....so")
> .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. I was hoping there is a way to have a shared memory between R
and C++ on which one could operate, but i am beginning to understand that
is not what is done in Rcpp, and probably this could not be done.








On Tue, Nov 1, 2011 at 11:32 PM, Slava Razbash <slava.razbash at gmail.com>wrote:

> Andre,
>
> You should compare your code with a working RcppArmadillo example,
> such as the one on Dirk's website:
> http://dirk.eddelbuettel.com/code/rcpp.armadillo.html
> "Writing R Extensions" descirbes the .Call() interface and the R API.
> When using .Call(), it does not make copies of the objects passed to
> it.
>
> Rcpp wraps around the R API so that you program with Rcpp objects.
> This simplifies memory management.
> In the code that you provided, you should instantiate Rcpp objects first.
>
> Further, if you  #include <RcppArmadillo.h>, then you don't have to
> #include <Rcpp.h> or #include <armadillo>.
>
> You could also read Software for data analysis: programming with R by
> John Chambers.
>
>
> Best Regards,
>
> Slava
>
> On Wed, Nov 2, 2011 at 1:38 PM, andre zege <andre.zege at gmail.com> wrote:
> > Dirk, apologies, i meant to sent it to R-devel, i just replied to wrong
> > list. Now that i read responses on both lists, i am confused. Simon
> Urbanek
> > seemed to indicate that call by reference from
> > R to C++ is impossible with .C interface and dangerous and unreliable
> with
> > .Call. If i understood you correctly you seem to say that Rcpp
> facilitates
> > call by reference from R to C++. I actually tried that
> > in Rcpp as well but didn't succeed either, may be you could point me in
> the
> > right direction. Here is what i tried
> >
> >
> > modify.cpp
> > ===========================
> > #include <Rcpp.h>
> > #include <RcppArmadillo.h>
> > #include <armadillo>
> > using namespace Rcpp;
> > using namespace arma;
> >
> > RcppExport void modify(SEXP mem){
> >   mat m=as<mat>(mem);
> >   m.print();
> >   m=m+m;
> >
> > }
> > ==========================
> > I compiled the above, pointing  to RccpArmadillo include dir and linked
> > shared library to armadillo code. Then i loaded shared library and tried
> to
> > run code from R as follows
> >
> >
> >>dyn.load("/home/az05625/testarma/passptr.so")
> >> .Call("modify", m)
> >
> > matrix m prints out fine, but code segfaults on the last statement m=m+m
> > Could you give me some idea on how to fix this?
> >
> >
> >
> >
> >
> >
> >> Please ask basic R programming questions on R-devel as you seem to have
> >> read
> >> the wrong documentation --- there is no support for .C() in Rcpp. We do
> >> what
> >> we do via SEXP objects, and those require .Call().  So I suggest you
> read
> >> a
> >> little more in "Writing R Extensions".  As well as the Rcpp
> documentation.
> >>
> >> And as you will learn in the "Rcpp-introduction" and other places, we
> use
> >> what is called proxy model --- so yes, we do pass pointers and no, you
> >> don;t
> >> get more lightweight than this.
> >>
> >> Then again, R uses copy-on-write so if you want to truly alter without
> >> having
> >> R create new copies for you then external pointers are your best bet.
> >>
> >> Dirk
> >>
> >> --
> >> "Outside of a dog, a book is a man's best friend. Inside of a dog, it is
> >> too
> >> dark to read." -- Groucho Marx
> >
> >
> >
> > _______________________________________________
> > 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/20111102/42852703/attachment-0001.htm>


More information about the Rcpp-devel mailing list