[Rcpp-devel] Segfault error during simulation in Rcpp

Dirk Eddelbuettel edd at debian.org
Fri May 17 00:33:52 CEST 2013


On 16 May 2013 at 23:27, Matteo Fasiolo wrote:
| So the error appears when we are creating a RNGScope object AND we are
| allocating memory 
| repeatedly?

It is unclear. It is possible that setting up RNGScope 10^6 times tickles
something somewhere.  You should only call it once.  OTOH what the code does
is so simple ...

| So that something like this:
| 
| library(Rcpp)
| 
| myFun <- cppFunction('void myFun(){
| RNGScope scope;
| }')
| 
| N <- 1e6
| for (j in 1:N) {
|     res <- myFun( )
| }
| 
| doesn't crash because we are not allocating memory using NumericMatrix or
| NumericVector?
| 
| In any case I can't move all my code to C++, so maybe the easiest thing to do
| for my original problem (given my limited expertise)
| is generating random variables and allocating memory in R and then pass the
| pointers to a .C function. 

No, not at all.

But take the one really good observation to heart and consider to rewrite
your setup to not use sourceCpp() to not have RNGScope inserted for every
function call, eg use cxxfunction() or just write a package the old fashioned
way.

The other obvious of course is that you are not forced to control a loop over
10^6 elements from R either:  pass N=10^6 down to C++ code, and run your N
loops there.  You will also get a considerable speed boost.

| Sigh! :(

Don't give up yet.  You tried a cheap and easy approach which turned out to
have side effects. You may have uncovered a real bug -- so this was really
helpful.  Now just try to a different approach to avoid the bottleneck.

Dirk

 
| On Thu, May 16, 2013 at 8:11 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| 
| 
|     On 16 May 2013 at 14:49, Jonathan Olmsted wrote:
|     | 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?
| 
|     Beautiful. So we get to blame R Core after all?  ;-)
|    
|     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
| 
| 

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list