<div dir="ltr"><div style><font face="arial, sans-serif">Jonathan,</font></div><span style="font-family:arial,sans-serif"><div><span style="font-family:arial,sans-serif"><br></span></div>| ^ I could have been more explicit. This is what I meant. You might may or may not be able to do this in the sourceCpp() framework, though. How you do | this is much more obvious if you go the route of making an Rcpp package. For as much as the convenience functions are a blessing, I've always found it  | hard to think about how I'd do more complex things with them. It could be possible, I just don't see it. I find the package approach to be a nice balance |between abstracting away from some details but keeping some in the forefront of your mind. If you email me off the list and I can send you a very small |package that using Rcpp. Multiple C++ functions are created, only some are exposed to R.</span><br>
<div><span style="font-family:arial,sans-serif"><br></span></div><div><span style="font-family:arial,sans-serif"><br></span></div><div style><span style="font-family:arial,sans-serif">Your are right creating a package is probably the best thing to do especially in the long term (when hopefully this stuff will go in an article and </span></div>
<div style><span style="font-family:arial,sans-serif">the code in a CRAN package). I'll email you to ask you the package you where talking about.</span></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Fri, May 17, 2013 at 11:32 AM, Matteo Fasiolo <span dir="ltr"><<a href="mailto:matteo.fasiolo@gmail.com" target="_blank">matteo.fasiolo@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 dir="ltr">Sorry yesterday evening by mistake I sent a message only to Dirk:<div><br></div><div><div class="im"><span style="font-family:arial,sans-serif;font-size:13px">Thanks, you are right I'll try cxxfunction and/or I'll see whether I can</span><div style="font-family:arial,sans-serif;font-size:13px">

move avoid doing so many calls to C++.</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">For the sake of minimality: this code is enough to have a segfault or to make R stall</div>

<div style="font-family:arial,sans-serif;font-size:13px">(at least on my computer):</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div></div><div style="font-family:arial,sans-serif;font-size:13px"><div>
library(Rcpp)</div>
<div>myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input){ return input; }')</div><div class="im"><div><div><br></div><div>n <- 10</div><div>x <- 1:n^2</div><div>N <- 1e6</div><div>b <- 0</div>

</div></div><div class="im"><div>A <- matrix(x, n, n)</div></div><div class="im"><div>for (j in 1:N) {</div></div><div class="im"><div>    res <- myFun(A)</div><div>    a <- res[1,1]</div></div><div class="im"><div>
<div>    b <- b + a</div><div>}</div><div><br></div><div>cat(sprintf("Done, b is %d\n", b))</div>
<div><br></div></div></div><div class="im"><div>Thanks a lot to everybody for the help!</div></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, May 17, 2013 at 4:17 AM, Ivan Popivanov <span dir="ltr"><<a href="mailto:ivan.popivanov@gmail.com" target="_blank">ivan.popivanov@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 dir="ltr">Ignore my previous mail ...</div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Thu, May 16, 2013 at 11:12 PM, Ivan Popivanov <span dir="ltr"><<a href="mailto:ivan.popivanov@gmail.com" target="_blank">ivan.popivanov@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 dir="ltr">How is this code supposed to work? If n=1e6, then the matrix has 1e12 elements, right? That's in the terabyte range - the memory manager is going to blow up or overflow and the results would be unpredictable. Or am I missing something?<div>



<br></div><div>Regards,</div><div>Ivan</div></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, May 16, 2013 at 7:14 PM, Jonathan Olmsted <span dir="ltr"><<a href="mailto:jpolmsted@gmail.com" target="_blank">jpolmsted@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 dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div style="font-size:small" class="gmail_default">



Matteo,</div><br> </div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


The other obvious of course is that you are not forced to control a loop over<br>
10^6 elements from R either:  pass N=10^6 down to C++ code, and run your N<br>
loops there.  You will also get a considerable speed boost.<br><div><div><br></div></div></blockquote><div><br></div></div><div><div style="font-size:small" class="gmail_default">^ I could have been more explicit. This is what I meant. You might may or may not be able to do this in the sourceCpp() framework, though. How you do this is much more obvious if you go the route of making an Rcpp package. For as much as the convenience functions are a blessing, I've always found it hard to think about how I'd do more complex things with them. It could be possible, I just don't see it. I find the package approach to be a nice balance between abstracting away from some details but keeping some in the forefront of your mind. If you email me off the list and I can send you a very small package that using Rcpp. Multiple C++ functions are created, only some are exposed to R.</div>





<div style="font-size:small" class="gmail_default"><br></div><div style="font-size:small" class="gmail_default">It seems like you'd probably do something similar.</div><span><font color="#888888"><div style="font-size:small" class="gmail_default">





<br></div><div style="font-size:small" class="gmail_default">-Jonathan<br><br></div></font></span></div><div><div><div><div style="font-size:13px;font-family:arial,sans-serif"><div style="font-size:small" class="gmail_default">




<br>
</div></div><div style="font-size:13px;font-family:arial,sans-serif"><br></div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">





<div><div>
| On Thu, May 16, 2013 at 8:11 PM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>> wrote:<br>
|<br>
|<br>
|     On 16 May 2013 at 14:49, Jonathan Olmsted wrote:<br>
|     | Several things.<br>
|     |<br>
|     | 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/<br>
|     | cxxfunction approach, I don't have any issues. One obvious difference you<br>
|     can<br>
|     | see (like was mentioned) in the generated code (visibile using verbose=<br>
|     TRUE) is<br>
|     | the declaration of an RNGScope object.<br>
|     |<br>
|     | But, if the memory issue crops up when you call a C++ function (syncing<br>
|     with<br>
|     | R's RNG state) 1e6 times AND you are already writing C++ maybe this is<br>
|     an<br>
|     | opportunity to just put one more layer of the code into C++ and create<br>
|     only one<br>
|     | such RNGScope object?<br>
|<br>
|     Beautiful. So we get to blame R Core after all?  ;-)<br>
|<br>
|     Dirk<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>
|     _______________________________________________<br>
|     Rcpp-devel mailing list<br>
|     <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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>
|<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></div></div><br></div></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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></div></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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></div></blockquote></div><br></div>