[Rcpp-devel] Segfault error during simulation in Rcpp

Jared Murray jmurray.1022 at gmail.com
Thu May 9 18:04:01 CEST 2013


I have had a similar experience; I wrote a small-moderate sized
function to do some simulations and tested via sourceCpp() (and
included an explicit instance of RNGScope() as well)  with no problems
whatever. Then I wrote a wrapper to this function and tried running it
in parallel with mclapply and got intermittent segfaults. When I
backed off and tried just running the wrapper function itself the
(intermittent) segfaults persisted. I tried several things, including
moving the function into a package, but none of these helped.

Refactoring the code to use fewer temporaries fixed the problem.  I
got the hint by adding some debug messages and turning on gcinfo(),
and watching it segfault every time gc happened while the function was
executing.. I tried to reproduce the bug with something simple but had
no luck, and since the fix ended up being writing code that wasn't
stupid I let it pass :) But you might try the same Matteo, as you seem
to be in a very similar boat.

As an aside, you can replace code like this:
rpois(1, phi * currState)[0];

with a call to the original Rmath function using the R:: namespace
R::rpois( phi * currState);

since you don't need the (wonderful!) sugar vectorization.

On Mon, May 6, 2013 at 2:47 PM, Matteo Fasiolo <matteo.fasiolo at gmail.com> wrote:
> Ok, I'll do exactly this and I'll report if I still encounter this
> problem in simpler programs. Thanks.
>
>
> On Mon, May 6, 2013 at 7:39 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>>
>>
>> On 6 May 2013 at 19:03, Matteo Fasiolo wrote:
>> | Sorry for the silly question, but since I'm using sourceCpp() too I
>> should't
>> | call
>> | getRNGstate/putRNGstate, right?
>>
>> If you use any of the random number generators provided by R via Rcpp,
>> then
>> you __MUST__ also instantiate an RNGScope() object (which does this for
>> you).
>>
>> We documented this *very* clearly in a number of places in the
>> documentation.
>>
>> (And yes, sourceCpp() adds one as a courtesy but you should still add
>> one. The extra few nanoseconds in execution speed won't matter.)
>>
>> You report is otherwise still unreproducible at my end.
>>
>> Your program is also complex in that it does a few things.  Try writing
>> smaller and smaller programs still exhibiting the error at your end.  And
>> do
>> report back if you find something, or have questions.
>>
>> Dirk
>>
>> --
>> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
>
>
> _______________________________________________
> 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


More information about the Rcpp-devel mailing list