使用 RcppParallel 时堆栈不平衡

Stack imbalance while using RcppParallel

// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using namespace RcppArmadillo;
using namespace RcppParallel;
using namespace std;

struct Sum : public Worker
{   
  vector<string> output;

  Sum() {}
  Sum(const Sum& sum, Split) {}

  void operator()(std::size_t begin, std::size_t end) {
    vector<string> states;
    states.push_back("a");
    states.push_back("b");
    states.push_back("c");
    states.push_back("d");

    vector<double> probs;
    probs.push_back(0.3);
    probs.push_back(0.4);
    probs.push_back(0.1);
    probs.push_back(0.2);

    vector<string> rstat = sample(states, 1, false, wrap(probs));
    output.push_back(rstat[0]);

  }

  void join(const Sum& rhs) { 
    for(int i=0;i<rhs.output.size();i++) {
      output.push_back(rhs.output[i]);
    }
  }
};

// [[Rcpp::export]]
CharacterVector parallelVectorSum(int n) {

  Sum sum;

  parallelReduce(0, n, sum);

  return wrap(sum.output);
}

以上代码只是实验学习RcppParllel。我做了很多搜索,发现我们应该避免使用 CharacterVectorNumericVector 等数据类型。这就是我使用 C++ STL.

的原因

输出 1

> parallelVectorSum(1)
[1] "b"

输出 2

> parallelVectorSum(11)
 [1] "d" "a" "b" "b" "d" "a" "b" "b" "d" "b" "a"

输出 3

> parallelVectorSum(111)
Warning: stack imbalance in '.Call', 7 then 6
  [1] "a" "b" "d" "b" "a" "b" "d" "d" "a" "b" "a" "b" "d" "b" "b" "c" "a" "a" "a" "d" "b" "b" "b" "a" "c" "a" "b" "a"
 [29] "a" "b" "b" "d" "a" "b" "c" "b" "b" "d" "d" "b" "b" "a" "b" "a" "d" "b" "b" "a" "a" "a" "b" "b" "a" "a" "b" "d"
 [57] "a" "a" "b" "d" "a" "a" "c" "d" "b" "c" "a" "d" "a" "d" "d" "b" "a" "a" "d" "b" "b" "d" "d" "b" "b" "b" "a" "a"
 [85] "c" "a" "b" "d" "c" "b" "b" "a" "d" "d" "b" "b" "a" "a" "d" "d" "a" "c" "b" "b" "a" "a" "b" "b" "b" "c" "d"

在上次 运行 中,我收到了与堆栈不平衡相关的警告,我确信这是因为使用了 RcppArmadillosample 函数。在 sample 方法的定义中,我发现正在使用 R 数据类型。事实上 sample 的第四个参数本身就是 NumericVector 这是一个问题。

有什么办法可以解决这个问题?我是否需要实现自己的 sample 功能(我认为这并不容易 - 我是初学者)。 任何解决方案将不胜感激。请帮忙。

我已经从 RcppArmadillo's sample.h 移植了代码以仅使用 arma::vec

参见:https://github.com/RcppCore/RcppArmadillo/pull/101

唯一的问题是这不适用于 std::string,因为 arma 没有为此定义类型。 (我想你可以用 template 来写吗?