[英]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
的实验。 我做了很多搜索,发现我们应该避免使用CharacterVector
、 NumericVector
等数据类型,这就是我使用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"
在上次运行中,我收到了与堆栈不平衡相关的警告,我确信这是因为使用了RcppArmadillo
的sample
函数。 在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.