简体   繁体   中英

RStudio keeps crashing with Rcpp

I'd like to understand what is making this program crash. I've seen at least three related questions here , here and here , but I haven't found a clear answer addressing the issue, so here's is some sample code to replicate the problem.

The R code:

library(Rcpp)

Rcpp::sourceCpp("myfunction.cpp")

data1      <- rnorm(2000)
data2      <- rnorm(2000)

mydata <- matrix(cbind(data1, data2), nrow=2000, ncol=2)
values <- log(1:6)

for (i in 1:1000) {
  myfunction(values, mydata)
}

The C++ code:

#include "Rcpp.h"
#include "math.h"
using namespace Rcpp;

// [[Rcpp::export]]
double myfunction(const NumericVector& theta, const NumericMatrix& data) {

  double ans = 0;

  int theta_size = theta.length();
  NumericVector mytheta(theta_size);

  int data_size = data.nrow();
  NumericMatrix mat(data_size, 2);

  for (int i = 0; i < theta_size; i++) {
    mytheta(i) = exp(theta(i));
  }

  if ( true ) {  // Flow control

    for (int i = 0; i < data_size; i++) {
      mat(i, 1) = pow(data(i-1, 1), 2) + mytheta(1)*mat(i-1, 1);
      ans = ans + 1;
    }

    for (int i = 0; i < data_size; i++) {
      mat(i, 2) = pow(data(i-1, 2), 2) + mytheta(4)*mat(i-1, 2);
      ans = ans + 1;
    }

  }

  Rcout << "Ok!\n";

  return ans;
}

Everything works fine at least the first time I use myfunction() , but it then crashes when called within the R for loop. I reinstalled R, Rtools and RStudio (on Windows) to see if maybe something was wrong with the installation, but I still face the same issue.

This is making the seamless integration between R and C++ less seamless that I first thought, and since I've seen that I'm not the only one who's been facing this issue, it looks like we are all making some obvious mistake when starting with Rcpp (at least on RStudio), but what is it?

Essentially, I want to be sure I'm not missing something completely obvious here because all the answers I've seen so far seem to imply that it is the case.

Note : this is a shortened version of a longer function I've been testing with Rcpp, the original version seems to work fine the first few times I call it, but it also eventually crashes.

Update: removed rm(), oops.

 for (int i = 0; i < data_size; i++) { mat(i, 1) = pow(data(i-1, 1), 2) + mytheta(1)*mat(i-1, 1); ans = ans + 1; } 

When i == 0 , you try to access data(-1, 1) and things go pear-shaped from there. The fact that it didn't crash the first time you ran it just means you got lucky (or unlucky, in that it lured you into a false sense of confidence).

So here is a repaired version with

  • @HongOoi's correction to the loop index
  • my correction to the columns
  • R code in the C++ file
  • no outout from C++ code called N times
  • output at end
  • seeding the RNG to make it reproducible

Code

#include "Rcpp.h"
#include "math.h"
using namespace Rcpp;

// [[Rcpp::export]]
double myfunction(const NumericVector& theta, const NumericMatrix& data) {

  double ans = 0;

  int theta_size = theta.length();
  NumericVector mytheta(theta_size);

  int data_size = data.nrow();
  NumericMatrix mat(data_size, 2);

  for (int i = 0; i < theta_size; i++) {
    mytheta(i) = exp(theta(i));
  }

  if ( true ) {  // Flow control

    for (int i = 1; i < data_size; i++) {
      mat(i, 0) = pow(data(i-1, 0), 2) + mytheta(1)*mat(i-1, 0);
      ans = ans + 1;
    }

    for (int i = 1; i < data_size; i++) {
      mat(i, 1) = pow(data(i-1, 1), 2) + mytheta(4)*mat(i-1, 1);
      ans = ans + 1;
    }

  }

  //Rcout << "Ok!\n";

  return ans;
}

/**** R
set.seed(123)
data1      <- rnorm(2000)
data2      <- rnorm(2000)

mydata <- matrix(cbind(data1, data2), nrow=2000, ncol=2)
values <- log(1:6)

for (i in 1:1000) {
  myfunction(values, mydata)
}
cat("Success\n")
*/

Demo

edd@rob:~$ Rscript -e 'Rcpp::sourceCpp("/tmp/trusky.cpp")'

R> set.seed(123)

R> data1 <- rnorm(2000)

R> data2 <- rnorm(2000)

R> mydata <- matrix(cbind(data1, data2), nrow = 2000, 
+     ncol = 2)

R> values <- log(1:6)

R> for (i in 1:1000) {
+     myfunction(values, mydata)
+ }

R> cat("Success\n")
Success
edd@rob:~$ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM