<div dir="ltr">Hi all,<div><br></div><div style> I'm writing again about the segfault problem during simulation.</div><div style>I have added the missing line: </div><div style><br></div><div style>RNGScope scope;</div>
<div style><br></div><div style>and I have got rid of all temporary expressions.</div><div style>After that everything seemed to go well, actually to make sure it</div><div style>was working I run 10^8 iterations in 4 R session without getting any segfault.</div>
<div style>It must be noted that if I run the program in valgrind I am still (occasionally) getting</div><div style>output like:</div><div style><br></div><div style><div>==9697==  Address 0xb214b10 is 0 bytes inside a block of size 440 free'd</div>
<div>==9697==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)</div><div>==9697==    by 0x4F43B8F: ??? (in /usr/lib/R/lib/libR.so)</div><div>==9697==    by 0x4F4661F: Rf_allocVector (in /usr/lib/R/lib/libR.so)</div>
<div>==9697==    by 0x4E65E1F: PutRNGstate (in /usr/lib/R/lib/libR.so)</div><div>==9697==    by 0xDF4CC8D: sourceCpp_94930_rickerCpp (random.h:71)</div><div>==9697==    by 0x4ED4F12: ??? (in /usr/lib/R/lib/libR.so)</div><div>
==9697==    by 0x4F1368B: Rf_eval (in /usr/lib/R/lib/libR.so)</div><div>==9697==    by 0x4F16ABC: Rf_applyClosure (in /usr/lib/R/lib/libR.so)</div><div>==9697==    by 0x4F1333F: Rf_eval (in /usr/lib/R/lib/libR.so)</div><div>
==9697==    by 0x4F152EF: ??? (in /usr/lib/R/lib/libR.so)</div><div>==9697==    by 0x4F1345E: Rf_eval (in /usr/lib/R/lib/libR.so)</div><div>==9697==    by 0x4F1546F: ??? (in /usr/lib/R/lib/libR.so)</div><div><br></div><div style>
The problem is that the segfaults are back, so I tried to simplify my code to see what is going on. </div><div style>I have arrived to the following version:</div><div style><br></div><div style><div>// [[Rcpp::export]]</div>
<div>NumericMatrix rickerCpp(int days, int nSimul, int nBurn, NumericVector params)</div><div>{</div><div>  RNGScope scope;</div><div>  </div><div>  int nParams = params.size(); </div><div>  int totDays = nBurn + days;</div>
<div><br></div><div>  double r = exp(params(0));</div><div>  double sigma = exp(params(1));</div><div>  double phi = exp(params(2));</div><div>  </div><div>  double initState = 1.0;</div><div>  double currState;</div><div>
      </div><div>  NumericMatrix output( nSimul, days );</div><div>  </div><div>  for(int iRow = 0; iRow < nSimul; iRow++)</div><div>  {</div><div>    </div><div>   currState = initState;</div><div>     </div><div>   for(int iCol = 1; iCol <= nBurn; iCol++){</div>
<div>     currState = r * currState * exp( - currState );</div><div>   }</div><div>   </div><div>   output(iRow, 0) = phi * currState;</div><div>   </div><div>   for(int iCol = 1; iCol < days; iCol++){</div><div>     currState = r * currState * exp( - currState );</div>
<div>     output(iRow, iCol) = phi * currState;</div><div>   }</div><div>    </div><div>  }</div><div>  </div><div>  return output;</div><div>  </div><div>}</div><div><br></div><div style>actually here I'm not calling any rnorm() or rpois() anymore. If I run it in R:</div>
<div style><br></div><div style><div>library(Rcpp)</div><div>sourceCpp("~/Desktop/a.cpp")</div><div><br></div><div>days <- 50</div><div>nBurn <- 50</div><div>nSimul <- 1</div><div> </div><div>set.seed(545784365)</div>
<div>for(ii in 1:10^6)</div><div>{</div><div>  params <- log(c(r = 44.7, sigma = 0.3, phi = 10))</div><div>  res <- rickerCpp(days, nSimul = nSimul, nBurn, params = params)</div><div>  res <- t(res)</div><div>}</div>
<div><br></div></div><div style>I can do 10^7 iteration without problems or it might crash immediately with message:</div><div style><br></div><div> *** caught segfault ***</div><div>address 0x6d6994, cause 'memory not mapped'</div>
<div><br></div><div>Traceback:</div><div style> 1: t(res) </div><div style><br></div><div style>up to 6 time in a row (also pointing at the same address 0x6d6994)! </div><div style>In addition valgrind still gives the same message about PutRNGstate etc...</div>
<div style>Probably it's just my impression, but segfaults seem to correlate between R<br>sessions: few sessions run fine and then they just keep crashing.</div><div style><br></div><div style>Sorry for the long message but I'm really clueless (as I said 4 by 10^8 iterations</div>
<div style>and it is fine, then it crashes several times then it is fine again?).</div><div style>Thanks,</div><div style><br></div><div style>Matteo</div><div><br></div></div><div><br></div></div><div style><br></div><div style>
<br></div><div style><br></div><div style><br></div><div style><br></div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, May 9, 2013 at 5:04 PM, Jared Murray <span dir="ltr"><<a href="mailto:jmurray.1022@gmail.com" target="_blank">jmurray.1022@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have had a similar experience; I wrote a small-moderate sized<br>
function to do some simulations and tested via sourceCpp() (and<br>
included an explicit instance of RNGScope() as well)  with no problems<br>
whatever. Then I wrote a wrapper to this function and tried running it<br>
in parallel with mclapply and got intermittent segfaults. When I<br>
backed off and tried just running the wrapper function itself the<br>
(intermittent) segfaults persisted. I tried several things, including<br>
moving the function into a package, but none of these helped.<br>
<br>
Refactoring the code to use fewer temporaries fixed the problem.  I<br>
got the hint by adding some debug messages and turning on gcinfo(),<br>
and watching it segfault every time gc happened while the function was<br>
executing.. I tried to reproduce the bug with something simple but had<br>
no luck, and since the fix ended up being writing code that wasn't<br>
stupid I let it pass :) But you might try the same Matteo, as you seem<br>
to be in a very similar boat.<br>
<br>
As an aside, you can replace code like this:<br>
<div class="im">rpois(1, phi * currState)[0];<br>
<br>
</div>with a call to the original Rmath function using the R:: namespace<br>
R::rpois( phi * currState);<br>
<br>
since you don't need the (wonderful!) sugar vectorization.<br>
<div class="HOEnZb"><div class="h5"><br>
On Mon, May 6, 2013 at 2:47 PM, Matteo Fasiolo <<a href="mailto:matteo.fasiolo@gmail.com">matteo.fasiolo@gmail.com</a>> wrote:<br>
> Ok, I'll do exactly this and I'll report if I still encounter this<br>
> problem in simpler programs. Thanks.<br>
><br>
><br>
> On Mon, May 6, 2013 at 7:39 PM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
>><br>
>><br>
>> On 6 May 2013 at 19:03, Matteo Fasiolo wrote:<br>
>> | Sorry for the silly question, but since I'm using sourceCpp() too I<br>
>> should't<br>
>> | call<br>
>> | getRNGstate/putRNGstate, right?<br>
>><br>
>> If you use any of the random number generators provided by R via Rcpp,<br>
>> then<br>
>> you __MUST__ also instantiate an RNGScope() object (which does this for<br>
>> you).<br>
>><br>
>> We documented this *very* clearly in a number of places in the<br>
>> documentation.<br>
>><br>
>> (And yes, sourceCpp() adds one as a courtesy but you should still add<br>
>> one. The extra few nanoseconds in execution speed won't matter.)<br>
>><br>
>> You report is otherwise still unreproducible at my end.<br>
>><br>
>> Your program is also complex in that it does a few things.  Try writing<br>
>> smaller and smaller programs still exhibiting the error at your end.  And<br>
>> do<br>
>> report back if you find something, or have questions.<br>
>><br>
>> Dirk<br>
>><br>
>> --<br>
>> Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
><br>
><br>
><br>
</div></div><div class="HOEnZb"><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>
</div></div></blockquote></div><br></div>