简体   繁体   English

RStudio不断崩溃与Rcpp

[英]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: R代码:

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: C ++代码:

#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. 至少在第一次使用myfunction() ,一切正常,但是在R for循环中调用时,它崩溃了。 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. 我重新安装了R,Rtools和RStudio(在Windows上)以查看安装是否有问题,但是仍然遇到相同的问题。

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? 这使得R和C ++之间的无缝集成不像我最初想象的那么无缝,并且因为我已经看到我并不是唯一一个遇到此问题的人,所以看起来我们在开始时都犯了一些明显的错误 。 Rcpp(至少在RStudio上),但这是什么?

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. 注意 :这是我一直在使用Rcpp测试的较长函数的简化版本,最初的版本在我调用它的最初几次时似乎可以正常工作,但最终也会崩溃。

Update: removed rm(), oops. 更新:删除rm(),哎呀。

 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. i == 0 ,您尝试访问data(-1, 1) ,然后从那里变成梨形。 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 @HongOoi对循环索引的更正
  • my correction to the columns 我对列的更正
  • R code in the C++ file C ++文件中的R代码
  • no outout from C++ code called N times 没有N次调用C ++代码的麻烦
  • output at end 最后输出
  • seeding the RNG to make it reproducible 播种RNG以使其可重现

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:~$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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