// [[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"在最后一次运行中,我收到了与堆栈不平衡相关的警告,我确信这是因为使用了sample函数的RcppArmadillo。在sample方法的定义中,我发现正在使用R数据类型。事实上,sample的第四个参数是NumericVector本身,这是一个问题。
这个问题的解决办法是什么?我是否需要实现我自己的sample函数(我不认为这很容易--我是个初学者)。任何解决方案都将不胜感激。请帮帮忙。
发布于 2016-06-14 18:00:59
我已经将代码从's sample.h移植到只使用arma::vec。
请参阅:https://github.com/RcppCore/RcppArmadillo/pull/101
唯一的问题是这将不适用于std::string,因为arma没有为此定义任何类型。(我想你可以用template写它
https://stackoverflow.com/questions/37818872
复制相似问题