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.olms...@gmail.com http://about.me/olmjo ------------------------------------------------------------------------- On Thu, May 16, 2013 at 2:22 PM, Xiao He <praguewaterme...@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 <e...@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 | e...@debian.org | http://dirk.eddelbuettel.com >> > > > _______________________________________________ > Rcpp-devel mailing list > Rcpp-devel@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel >
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel