[Rcpp-devel] RNGScope does work?

Dirk Eddelbuettel edd at debian.org
Thu Feb 17 18:38:38 CET 2011


On 17 February 2011 at 18:16, Carlos Almeida wrote:
| Hi,
| 
| I'm new by using C++ and Rcpp, but I wrote an minimal example which 
| shows the problem:
| 
| ------------------------
| 
| require( inline )
| require( Rcpp )
| 
| 
| src <- '
|      RNGScope scope;
|      //GetRNGstate();
| 
|      int NN = INTEGER(sNN)[0];
|      int T = INTEGER(sT)[0];

More common in Rpp:    

     int NN = as<int>(sNN);
     int T = as<int>(sT);
 
|      Rcpp::NumericMatrix lar(T+1,NN);
|      Rcpp::NumericMatrix la1r(T+1,NN);

You add 'using namespace Rpp;' so you don't need Rcpp::. Or maybe you don't
need 'using namesapce Rcpp;' ...

|      Rcpp::NumericVector epsr(NN*(T+1));
|      epsr = rnorm((T+1)*NN,0.0,1.0); // check
| 
|      Rcpp::List res = Rcpp::List::create(Rcpp::Named("gamma") = lar);

I don't understand this.  You creaye lar, and return it. You also create la1r
and ignore it, idemt for epsr. Why?

Also, you can just return via

       return lar;

as NumericVector get converted to SEXP automagially via wrap().

 
|      //PutRNGstate();
|      return(res);
| '
| 
| inc <- '
| 
| 
| using namespace Rcpp;
| using namespace std;
| 
| #include <stdio.h>
| #include <Rmath.h>

These includes are implicit. 
 
| '
| 
| 
| 
| fun <- cfunction(signature(sNN="integer", sT="integer"),
|                   src,
|                   includes=inc,
|                   Rcpp=TRUE,
|                   cppargs="-I/usr/include")

"-I/usr/include" is a default value.

 
| a <- fun(10L,1000L)$gamma[-1,]
| 
| 
| for (i in 1:10000){
| a <- fun(10L,1000L)$gamma[-1,]
| #if(i%%100 ==0)
|     cat(i,"\n")
| }
| 
| --------------------------------------------------------------
| 
| The output of this script is random, some times it runs without problem, 
| but often the output is like as:
| ________________________________
| 
| ....
| 639
| 640
| 641
| 
|   *** caught segfault ***
| address 0x28, cause 'memory not mapped'
| 
| Possible actions:
| 1: abort (with core dump, if enabled)
| 2: normal R exit
| 3: exit R without saving workspace
| 4: exit R saving workspace
| 
|   ____________________________________
| 
| The problem seems to be solved by using
| GetRNGstate();  and
| PutRNGstate();

That would surprise me. If you look at how RNGScore is defined:

class RNGScope{
public:
	RNGScope(){ GetRNGstate(); }
	~RNGScope(){ PutRNGstate(); }	
} ;

this is _all_ it does.  We probably do not have a simpler class in Rcpp.

If I had to guess, my money were on you doing something funky with your
indices. 

Dirk

| 
| Any help greatly appreciated.
| Cheers,
| 
| 
| -- 
| 
| Dr. Carlos Almeida
| Technische Universitaet Muenchen
| Zentrum Mathematik
| Lehrstuhl fr Mathematische Statistik
| Boltzmannstr. 3
| 85747 Garching
| Germany
| 
| phone: +49-89-289-17439
| fax: +49-89-289-17435
| 
| email: calmeida at ma.tum.de
| http://www-m4.ma.tum.de/m4/pers/almeida/index.html
| 
| 
| 
| _______________________________________________
| 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