[Rcpp-devel] Irregular crash with rcpp and inline

Dirk Eddelbuettel edd at debian.org
Fri Apr 13 15:14:29 CEST 2012


As a second follow-up: if you 

  a) use RcppArmadillo, 

  b) you also define

        PKG_CPPFLAGS=	      -UNDEBUG

     somewhere (I use ~/.R/Makevars) to suppress R's new -DNDEBUG 
     which otherwise turns debugging asserts off

then you get bounds checking from Armadillo on the normal (eg round
parenthesis) vector and matrix access operations.  

In which case the run-time behaviour of the (non-fixed but translated to
RcppArmadillo) file becomes

  edd at max:~$ r /tmp/marie.r 

  error: Mat::operator(): out of bounds

  Error in myfx(dist, rta) : 
  Execution halted
  edd at max:~$ 

which is a lot nicer than a naked segfault.

The RcppArmadillo version is below.  I also corrected one last minor
editorial thingie: cxxfunction's signature use 'NumericVector', we usually
use "numeric" or "integer" here.

Dirk


suppressMessages(library(inline))        # this will include Rcpp for us
suppressMessages(library(CircStats))

mycppfx <- '
  // Input values                       # no need to include cmath again
  arma::vec distc = Rcpp::as<arma::vec>(dist);
  arma::vec rtac  = Rcpp::as<arma::vec>(rta);
  int nc = distc.size();
  arma::vec dx(nc);
  arma::vec dy(nc);
  arma::mat coord(nc+1,2);

  // Transforming relative turning angle into absolute turning angle
  arma::vec ata(nc);
  ata(0) = fmod(rtac(0),2*PI);
  for(int j = 1; j < nc+1; j++){
    ata(j)= fmod(ata(j-1) + rtac(j),2*PI);
  }

  /////////////////////////////////
  //Change into cartesian coordinates

  // Calculate the displacement in each direction

  dx = distc * cos(ata);
  dy = distc * sin(ata);

  nc += 1;

  // Add the displacments
  coord(0,0) = coord(0,1) = 0;
  for(int j = 1; j < nc+1; j++){
    coord(j,0) = coord(j-1, 0) + dx(j-1);
    coord(j,1) = coord(j-1, 1) + dy(j-1);
  }

  return Rcpp::wrap(coord);
'
myfx <- cxxfunction(signature(dist ="numeric", rta="numeric"),
                    body = mycppfx, plugin = "RcppArmadillo")

set.seed(123)
n <- 1000
dist <- rexp(n,10)
rta <- rvm(n,0,1)

res <- myfx(dist,rta)
print(head(res))

cat("Done\n")



-- 
R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com


More information about the Rcpp-devel mailing list