Slava, thanks for your pointers. I changed the code to the way Dirk example goes, namely<br>------------------------------------------------------<br>#include <RcppArmadillo.h><br><br>
RcppExport void modify(SEXP mem){<br>
Rcpp::NumericMatrix r_m(mem); <br>
arma::mat m(r_m.begin(), r_m.nrow(), r_m.ncol(), false);<br>
m.print();<br>
m=m+m;<br>
}<br>-----------------------------------------------------------------<br>i compiled it, and run shared lib from R doing<br>>m<br>
[,1] [,2] [,3] [,4] [,5]<br>
[1,] 1 3 5 7 9<br>
[2,] 2 4 6 8 10<br><br>>dyn.load("....so")<br>
> .Call("modify", m)<br>> m<br>
[,1] [,2] [,3] [,4] [,5]<br>
[1,] 1 3 5 7 9<br>
[2,] 2 4 6 8 10<br><br><br>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.<br>
<p><br></p><br><br><br><br> <br><br><div class="gmail_quote">On Tue, Nov 1, 2011 at 11:32 PM, Slava Razbash <span dir="ltr"><<a href="mailto:slava.razbash@gmail.com">slava.razbash@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Andre,<br>
<br>
You should compare your code with a working RcppArmadillo example,<br>
such as the one on Dirk's website:<br>
<a href="http://dirk.eddelbuettel.com/code/rcpp.armadillo.html" target="_blank">http://dirk.eddelbuettel.com/code/rcpp.armadillo.html</a><br>
"Writing R Extensions" descirbes the .Call() interface and the R API.<br>
When using .Call(), it does not make copies of the objects passed to<br>
it.<br>
<br>
Rcpp wraps around the R API so that you program with Rcpp objects.<br>
This simplifies memory management.<br>
In the code that you provided, you should instantiate Rcpp objects first.<br>
<br>
Further, if you #include <RcppArmadillo.h>, then you don't have to<br>
#include <Rcpp.h> or #include <armadillo>.<br>
<br>
You could also read Software for data analysis: programming with R by<br>
John Chambers.<br>
<br>
<br>
Best Regards,<br>
<font color="#888888"><br>
Slava<br>
</font><div><div></div><div class="h5"><br>
On Wed, Nov 2, 2011 at 1:38 PM, andre zege <<a href="mailto:andre.zege@gmail.com">andre.zege@gmail.com</a>> wrote:<br>
> Dirk, apologies, i meant to sent it to R-devel, i just replied to wrong<br>
> list. Now that i read responses on both lists, i am confused. Simon Urbanek<br>
> seemed to indicate that call by reference from<br>
> R to C++ is impossible with .C interface and dangerous and unreliable with<br>
> .Call. If i understood you correctly you seem to say that Rcpp facilitates<br>
> call by reference from R to C++. I actually tried that<br>
> in Rcpp as well but didn't succeed either, may be you could point me in the<br>
> right direction. Here is what i tried<br>
><br>
><br>
> modify.cpp<br>
> ===========================<br>
> #include <Rcpp.h><br>
> #include <RcppArmadillo.h><br>
> #include <armadillo><br>
> using namespace Rcpp;<br>
> using namespace arma;<br>
><br>
> RcppExport void modify(SEXP mem){<br>
> mat m=as<mat>(mem);<br>
> m.print();<br>
> m=m+m;<br>
><br>
> }<br>
> ==========================<br>
> I compiled the above, pointing to RccpArmadillo include dir and linked<br>
> shared library to armadillo code. Then i loaded shared library and tried to<br>
> run code from R as follows<br>
><br>
><br>
>>dyn.load("/home/az05625/testarma/passptr.so")<br>
>> .Call("modify", m)<br>
><br>
> matrix m prints out fine, but code segfaults on the last statement m=m+m<br>
> Could you give me some idea on how to fix this?<br>
><br>
><br>
><br>
><br>
><br>
><br>
>> Please ask basic R programming questions on R-devel as you seem to have<br>
>> read<br>
>> the wrong documentation --- there is no support for .C() in Rcpp. We do<br>
>> what<br>
>> we do via SEXP objects, and those require .Call(). So I suggest you read<br>
>> a<br>
>> little more in "Writing R Extensions". As well as the Rcpp documentation.<br>
>><br>
>> And as you will learn in the "Rcpp-introduction" and other places, we use<br>
>> what is called proxy model --- so yes, we do pass pointers and no, you<br>
>> don;t<br>
>> get more lightweight than this.<br>
>><br>
>> Then again, R uses copy-on-write so if you want to truly alter without<br>
>> having<br>
>> R create new copies for you then external pointers are your best bet.<br>
>><br>
>> Dirk<br>
>><br>
>> --<br>
>> "Outside of a dog, a book is a man's best friend. Inside of a dog, it is<br>
>> too<br>
>> dark to read." -- Groucho Marx<br>
><br>
><br>
><br>
</div></div><div><div></div><div class="h5">> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
><br>
</div></div></blockquote></div><br>