簡體   English   中英

使用 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的 sample.h移植了代碼以僅使用arma::vec

見: https : //github.com/RcppCore/RcppArmadillo/pull/101

唯一的問題是這不適用於std::string因為arma沒有為此定義類型。 (我想您可以使用template編寫它嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM