Re: [Rcpp-devel] Segfault error during simulation in Rcpp: explanation/fix

2013-05-17 Thread JJ Allaire
Ben, Thanks SO much for the work you did to discover this problem. I've committed a fix in rev 4319 (also bumped the version to 0.10.3.3 so a new tarball will also be available from R-forge soon). Best, J.J. On Fri, May 17, 2013 at 8:12 AM, QRD wrote: > Hi, > > On Thu, May 16, 2013 at 11:02 A

Re: [Rcpp-devel] Segfault error during simulation in Rcpp: explanation/fix

2013-05-17 Thread Dirk Eddelbuettel
Hi Ben, Those two emails rocked! I will have a bit of latency as I am now at R/Finance, but we will apply the simple patch and check the suggested. Thanks a bunch for doing that legwork! Dirk -- Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com ___

Re: [Rcpp-devel] Segfault error during simulation in Rcpp: explanation/fix

2013-05-17 Thread QRD
Hi, On Thu, May 16, 2013 at 11:02 AM, Dirk Eddelbuettel 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

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-17 Thread Matteo Fasiolo
Jonathan, | ^ 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

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-17 Thread Matteo Fasiolo
Sorry yesterday evening by mistake I sent a message only to Dirk: Thanks, you are right I'll try cxxfunction and/or I'll see whether I can move avoid doing so many calls to C++. For the sake of minimality: this code is enough to have a segfault or to make R stall (at least on my computer): libra

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Ivan Popivanov
Ignore my previous mail ... On Thu, May 16, 2013 at 11:12 PM, Ivan Popivanov wrote: > 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 unpredictabl

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Ivan Popivanov
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? Regards, Ivan On Thu, May 16, 2013 at 7:14 PM, Jonathan Olms

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Dirk Eddelbuettel
On 16 May 2013 at 19:14, Jonathan Olmsted wrote: | Matteo, | |   | | 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

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Jonathan Olmsted
Matteo, > 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. > > ^ I could have been more explicit. This is what I meant. You mi

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Dirk Eddelbuettel
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 c

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Matteo Fasiolo
So the error appears when we are creating a RNGScope object AND we are allocating memory repeatedly? 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 mem

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Dirk Eddelbuettel
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 (

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Jonathan Olmsted
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

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Xiao He
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++) { +

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Dirk Eddelbuettel
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 <-

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Matteo Fasiolo
Correction: as said by Brian it crashes also without --vanilla: teo@oracolo:~$ R R version 3.0.0 (2013-04-03) -- "Masked Marvel" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) [...] 'help.start()' for an HTML browser interface to help. Type 'q

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Xiao He
I tried it just now. Did not crash on my R. Mmm 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 >

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Jonathan Olmsted
Ditto Kevin's comment. I segfault no matter what. > sessionInfo() R version 2.15.3 (2013-03-01) Platform: x86_64-apple-darwin12.3.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datase

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Kevin Ushey
FWIW, I can reproduce the segfault with this example, whether running R as vanilla or not. > sessionInfo() R version 3.0.0 (2013-04-03) Platform: x86_64-apple-darwin10.8.0 (64-bit) locale: [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8 attached base packages: [1] stats gra

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Matteo Fasiolo
Thanks for your reply Dirk. Maybe I have found something. Hopefully this is reproducible and simple enough: /* * C++ file "b.cpp" * Just copying the input matrix into A and returning A. */ #include using namespace Rcpp; // [[Rcpp::export]] NumericMatrix myFun(NumericMatrix input, int n){ Num

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Dirk Eddelbuettel
Matteo, Can you provide a single, self-contained example and calling sequence that leads to reproducible crashes? That would be a bug. And we try to address it in Rcpp. As for your "issues" with RNGScope, I'd recommend that you write a C(++) function called from R __without using Rcpp__ and I v

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-16 Thread Matteo Fasiolo
Hi all, I'm writing again about the segfault problem during simulation. I have added the missing line: RNGScope scope; and I have got rid of all temporary expressions. After that everything seemed to go well, actually to make sure it was working I run 10^8 iterations in 4 R session without gett

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-09 Thread Jared Murray
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

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Matteo Fasiolo
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 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 >

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Dirk Eddelbuettel
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 (wh

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Matteo Fasiolo
Thanks for the quick replies! I'm using log() and then exp() on the parameters because this function goes within an optimization routine, so I need it keep the parameters positive. Sorry for the silly question, but since I'm using sourceCpp() too I should't call getRNGstate/putRNGstate, right? I

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Dirk Eddelbuettel
On 6 May 2013 at 08:34, Douglas Bates wrote: | The segfaults seem to be related to PutRNGstate and I don't see that you have | declared an instance of the class that causes the RNGstate to be accessed and | restored (I have forgotten the name of the class but it should be fairly easy | to find in

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Dirk Eddelbuettel
Matteo, This may reveal a subtle bug somewhere in the handling of temporary expressions, with possible interactions with the garbage collection by R itself. That said, a trivial rewrite (below) which via a single sourceCpp() compiles and runs, or re-runs it, has not lead to a single segfault he

Re: [Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Douglas Bates
The segfaults seem to be related to PutRNGstate and I don't see that you have declared an instance of the class that causes the RNGstate to be accessed and restored (I have forgotten the name of the class but it should be fairly easy to find in the examples). When you use random number generators

[Rcpp-devel] Segfault error during simulation in Rcpp

2013-05-06 Thread Matteo Fasiolo
Hi All, I am trying to simulate from the following model: Y_t = Pois( phi * N_t ); N_t = r * N_{t-1} * exp( -N_{t-1}^theta + e_t ) e_t ~ Norm(0, sigma) so I have written a Rcpp function with prototype: genRickerCpp(int days, int nSimul, int nBurn, NumericMatrix params) where: days = length o