<div dir="ltr"><div class="gmail_default" style="font-size:small">Several things.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Xiao, Dirk's code gives me a segfault immediately and reliably.<br>


<br>All, when I do this whole song and dance using the "old" Rcpp/inline/cxxfunction approach, I don't have any issues. One obvious difference you can see (like was mentioned) in the generated code (visibile using verbose=TRUE) is the declaration of an RNGScope object.<br>


<br>But, if the memory issue crops up when you call a C++ function (syncing with R's RNG state) 1e6 times AND you are already writing C++ maybe this is an opportunity to just put one more layer of the code into C++ and create only one such RNGScope object?</div>

<div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">-Jonathan</div><div class="gmail_extra"><br clear="all"><div><div dir="ltr"><div><font><font color="#666666"><font face="arial, helvetica, sans-serif"><br>

</font></font></font></div><div><br></div><div><span style="color:rgb(102,102,102);font-size:small;font-family:arial,helvetica,sans-serif">-------------------------------------------------------------------------</span><br>

</div><font face="arial, helvetica, sans-serif"><font><font color="#666666">J.P. Olmsted<br></font></font></font><div><font face="arial, helvetica, sans-serif"><font><font color="#666666"><a href="mailto:j.p.olmsted@gmail.com" target="_blank">j.p.olmsted@gmail.com</a></font></font></font></div>

<div><div><font face="arial, helvetica, sans-serif"><font color="#666666"><a href="http://about.me/olmjo" target="_blank">http://about.me/olmjo</a><font><br></font><font></font><font>-------------------------------------------------------------------------</font></font></font><br>

</div></div></div></div>
<br><br><div class="gmail_quote">On Thu, May 16, 2013 at 2:22 PM, Xiao He <span dir="ltr"><<a href="mailto:praguewatermelon@gmail.com" target="_blank">praguewatermelon@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div>Hi Dirk,</div><div><br></div><div>Does the code crash your R every time you run it or only occasionally? I ran your sample code exactly the way it's written, and my R did not crash. </div><div><br></div><div><div class="im">

<div>

> myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){</div></div><div>+   NumericMatrix A(n, n);</div><div>+   for(int Row = 0; Row < n; Row++) {</div><div>+     for(int Col = 0; Col < n; Col++) {</div>

<div class="im">

<div>+       A(Row, Col) = input(Row, Col);</div><div>+     }</div><div>+   }</div><div>+   return A;</div></div><div>+ }')</div><div class="im"><div>> </div><div>> n <- 10</div><div>> x <- 1:n^2</div>

<div>> N <- 1e6</div>

<div>> b <- 0</div><div>> for (j in 1:N) {</div></div><div>+     means <- matrix(x, n, n)</div><div class="im"><div>+     res <- myFun(means, n)</div></div><div>+     a <- res[1, 1]</div><div>+     b <- b + a</div>

<div>+ }</div><div class="im">

<div>> cat(sprintf("Done, b is %d\n", b))</div></div><div>Done, b is 1000000</div><div>> </div></div><div class="HOEnZb"><div class="h5"><div><br></div><br>
<br><br><div class="gmail_quote">On Thu, May 16, 2013 at 11:02 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<br>
Here is the self-contained example I asked for.<br>
<br>
And yes, it crashes for me too.  So let's not create 1e6 temp matrices.<br>
Until someone has time to debug memory management internals. Which is really<br>
hard, so this may not get fixed for a while.  Sorry.<br>
<br>
<br>
#!/usr/bin/Rscript<br>
<br>
library(Rcpp)<br>
myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){<br>
<div>  NumericMatrix A(n, n);<br>
  for(int Row = 0; Row < n; Row++) {<br>
    for(int Col = 0; Col < n; Col++) {<br>
      A(Row, Col) = input(Row, Col);<br>
    }<br>
  }<br>
  return A;<br>
</div>}')<br>
<div><br>
n <- 10<br>
x <- 1:n^2<br>
</div>N <- 1e6<br>
b <- 0<br>
for (j in 1:N) {<br>
<div>    means <- matrix(x, n, n)<br>
</div>    res <- myFun(means, n)<br>
<div>    a <- res[1, 1]<br>
</div>    b <- b + a<br>
}<br>
<br>
cat(sprintf("Done, b is %d\n", b))<br>
<div><div><br>
<br>
--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</div></div></blockquote></div><br>
</div></div><br>_______________________________________________<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></blockquote></div><br></div></div>