【问题标题】:Rcpp crashes RStudioRcpp 使 RStudio 崩溃
【发布时间】:2018-12-27 18:15:00
【问题描述】:

我有一个简单的rcpp 文件,它会导致 RStudio 崩溃(或者看起来如此)。

C++ 代码

这是.cpp文件(另存为Test/CppHelpers.cpp):

#include <Rcpp.h>

// [[Rcpp::plugins("cpp11")]]

using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix generateCombinations(const NumericMatrix& odds) {
  int n = odds.rows();
  int N = n*n*n;

  IntegerVector v1(n);
  std::iota(v1.begin(), v1.end(), 0);
  IntegerVector v2(n*n);
  v2 = rep_each(v1,n);

  NumericVector col0(N);
  NumericVector col1(N);
  NumericVector col2(N);
  for(int k = 0; k <= N; ++k) {
    int ind0 = k / (n*n);
    int ind1 = k % (n*n);
    ind1 = v2[ind1];
    int ind2 = k % n;

    col0[k] = odds(ind0,0);
    col1[k] = odds(ind1,1);
    col2[k] = odds(ind2,2);
  }

  NumericMatrix out(N,3);
  out(_,0) = col0;
  out(_,1) = col1;
  out(_,2) = col2;

  return out;
}

R 代码

这是我的.R 文件,我只是在其中获取上述文件并执行基本测试

Rcpp::sourceCpp("Test/CppHelpers.cpp")
n <- 9
odds <- matrix(1:n,ncol=3)
my_combs <- generateCombinations(odds)

这实际上按预期工作。但是,每次我运行代码时,一段时间后,它总是会导致 RStudio 崩溃。

我真的不明白为什么会出现这些崩溃以及如何解决它(尤其是因为它不会立即崩溃)。任何指导将不胜感激。


会话信息

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1    yaml_2.2.0  

【问题讨论】:

  • 您是否在没有 RStudio 的情况下在 R 中尝试过这个?我的猜测是,这与后者无关(因此与rstudio 标签无关)。
  • @r2evans 不,我没有,我会在RGui 中尝试它,我会删除 - 至少现在 - rstudio 标签。
  • 基本错误:k &lt;= N。你希望它是k &lt; N。 C(++) 索引从 0 到 N-1。
  • @DirkEddelbuettel 它一直在盯着我看——感觉很愚蠢。感谢您的帮助!
  • @AGrothedieck 相信我,我们都去过那里。不用担心:)

标签: r rcpp


【解决方案1】:

您有一个非常基本的索引错误:k 无法到达 N,因此将其设为 k &lt; N

纠正后的输出如下:

R> Rcpp::sourceCpp("/tmp/so53936159.cpp")

R> n <- 9

R> odds <- matrix(1:n,ncol=3)

R> my_combs <- generateCombinations(odds)
R> my_combs
      [,1] [,2] [,3]
 [1,]    1    4    7
 [2,]    1    4    8
 [3,]    1    4    9
 [4,]    1    5    7
 [5,]    1    5    8
 [6,]    1    5    9
 [7,]    1    6    7
 [8,]    1    6    8
 [9,]    1    6    9
[10,]    2    4    7
[11,]    2    4    8
[12,]    2    4    9
[13,]    2    5    7
[14,]    2    5    8
[15,]    2    5    9
[16,]    2    6    7
[17,]    2    6    8
[18,]    2    6    9
[19,]    3    4    7
[20,]    3    4    8
[21,]    3    4    9
[22,]    3    5    7
[23,]    3    5    8
[24,]    3    5    9
[25,]    3    6    7
[26,]    3    6    8
[27,]    3    6    9
R> 

使用下面包含的 R 示例修复代码。

#include <Rcpp.h>

// [[Rcpp::plugins("cpp11")]]

using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix generateCombinations(const NumericMatrix& odds) {
  int n = odds.rows();
  int N = n*n*n;

  IntegerVector v1(n);
  std::iota(v1.begin(), v1.end(), 0);
  IntegerVector v2(n*n);
  v2 = rep_each(v1,n);

  NumericVector col0(N);
  NumericVector col1(N);
  NumericVector col2(N);
  for(int k = 0; k < N; ++k) {
    int ind0 = k / (n*n);
    int ind1 = k % (n*n);
    ind1 = v2[ind1];
    int ind2 = k % n;

    col0[k] = odds(ind0,0);
    col1[k] = odds(ind1,1);
    col2[k] = odds(ind2,2);
  }

  NumericMatrix out(N,3);
  out(_,0) = col0;
  out(_,1) = col1;
  out(_,2) = col2;

  return out;
}

/*** R
n <- 9
odds <- matrix(1:n,ncol=3)
my_combs <- generateCombinations(odds)
*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多