// [[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
功能(我不认为这很容易 - 我是初学者)。
任何解决方案将不胜感激。请帮忙。
答案 0 :(得分:3)
我已经从RcppArmadillo
's sample.h移植了代码,仅使用arma::vec
。
请参阅:
https://github.com/SMAC-Group/gmwm/blob/f5f132c5a15820643286bb3c19b676b80d8f46c1/src/sampler.cpp
您需要致电的功能是:
arma::vec rsample(const arma::vec &x, const int size, const bool replace, arma::vec prob_ );
唯一的问题是这不适用于std::string
,因为arma
没有为此定义类型。 (我想你可以用template
来写它?