[Rcpp-devel] Segfault error during simulation in Rcpp

Jonathan Olmsted jpolmsted at gmail.com
Thu May 16 20:49:23 CEST 2013


Several things.

Xiao, Dirk's code gives me a segfault immediately and reliably.

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.

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?

-Jonathan



-------------------------------------------------------------------------
J.P. Olmsted
j.p.olmsted at gmail.com
http://about.me/olmjo
-------------------------------------------------------------------------


On Thu, May 16, 2013 at 2:22 PM, Xiao He <praguewatermelon at gmail.com> wrote:

> Hi Dirk,
>
> 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.
>
>  > myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){
> +   NumericMatrix A(n, n);
> +   for(int Row = 0; Row < n; Row++) {
> +     for(int Col = 0; Col < n; Col++) {
>  +       A(Row, Col) = input(Row, Col);
> +     }
> +   }
> +   return A;
> + }')
> >
> > n <- 10
> > x <- 1:n^2
> > N <- 1e6
> > b <- 0
> > for (j in 1:N) {
> +     means <- matrix(x, n, n)
> +     res <- myFun(means, n)
> +     a <- res[1, 1]
> +     b <- b + a
> + }
> > cat(sprintf("Done, b is %d\n", b))
> Done, b is 1000000
> >
>
>
>
>
> On Thu, May 16, 2013 at 11:02 AM, Dirk Eddelbuettel <edd at debian.org>wrote:
>
>>
>> Here is the self-contained example I asked for.
>>
>> And yes, it crashes for me too.  So let's not create 1e6 temp matrices.
>> Until someone has time to debug memory management internals. Which is
>> really
>> hard, so this may not get fixed for a while.  Sorry.
>>
>>
>> #!/usr/bin/Rscript
>>
>> library(Rcpp)
>> myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){
>>   NumericMatrix A(n, n);
>>   for(int Row = 0; Row < n; Row++) {
>>     for(int Col = 0; Col < n; Col++) {
>>       A(Row, Col) = input(Row, Col);
>>     }
>>   }
>>   return A;
>> }')
>>
>> n <- 10
>> x <- 1:n^2
>> N <- 1e6
>> b <- 0
>> for (j in 1:N) {
>>     means <- matrix(x, n, n)
>>     res <- myFun(means, n)
>>     a <- res[1, 1]
>>     b <- b + a
>> }
>>
>> cat(sprintf("Done, b is %d\n", b))
>>
>>
>> --
>> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130516/0d9482a7/attachment.html>


More information about the Rcpp-devel mailing list