Rstudio使用Rcpp和OpenMP功能崩溃

时间:2019-02-18 23:03:22

标签: rcpp

这是对dqrng with Rcpp for drawing from a normal and a binomial distribution的跟进问题。我尝试实现答案,但不是从单一发行版中提取而是从3中提取。这是我编写的代码:

check_call(["pkill", "-f", "MotionDetector.py"])

当我尝试运行它时,Rstudio崩溃。但是,当我像下面那样更改代码时,它确实起作用:

// [[Rcpp::depends(dqrng, BH, RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <boost/random/binomial_distribution.hpp>
#include <xoshiro.h>
#include <dqrng_distribution.h>
// [[Rcpp::plugins(openmp)]]
#include <omp.h>

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

// [[Rcpp::export]]
arma::mat parallel_random_matrix(int n, int m, int ncores, double p=0.5) {
  dqrng::xoshiro256plus rng(42);
  arma::mat out(n*m,3);
  // ok to use rng here
#pragma omp parallel num_threads(ncores)
{
  dqrng::xoshiro256plus lrng(rng);      // make thread local copy of rng 
  lrng.jump(omp_get_thread_num() + 1);  // advance rng by 1 ... ncores jumps 
  int iter = 0;
#pragma omp for
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      iter = i * n + j;
      // p can be a function of i and j
      boost::random::binomial_distribution<int> dist_binomial(1,p);
      auto gen_bernoulli = std::bind(dist_binomial, std::ref(lrng));
      boost::random::normal_distribution<int> dist_normal1(2.0,1.0);
      auto gen_normal1 = std::bind(dist_normal1, std::ref(lrng));
      boost::random::normal_distribution<int> dist_normal2(4.0,3.0);
      auto gen_normal2 = std::bind(dist_normal2, std::ref(lrng));
      out(iter,0) = gen_bernoulli();
      out(iter,1) = gen_normal1();
      out(iter,2) = gen_normal2();
    }
  }
}
// ok to use rng here
return out;
}

/*** R
parallel_random_matrix(5, 5, 4, 0.75)
*/

我在做什么错了?

1 个答案:

答案 0 :(得分:2)

问题出在这里

  boost::random::normal_distribution<int> dist_normal1(2.0,1.0);
                                     ^^^ 

此分布适用于实数类型,而不是整数类型。 https://www.boost.org/doc/libs/1_69_0/doc/html/boost/random/normal_distribution.html。正确的是

  boost::random::normal_distribution<double> dist_normal1(2.0,1.0);