我有一个函数名.execute()
,其中两个参数是markovchainListRcpp
,另一个是我们想要的list
。
让我们看一个例子
number of sequences
查看> mclist
[[1]]
Unnamed Markov chain
A 3 - dimensional discrete Markov Chain with following states:
a, b, c
The transition matrix (by rows) is defined as follows:
a b c
a 0.2 0.5 0.3
b 0.0 0.2 0.8
c 0.1 0.8 0.1
[[2]]
Unnamed Markov chain
A 3 - dimensional discrete Markov Chain with following states:
a, b, c
The transition matrix (by rows) is defined as follows:
a b c
a 0.2 0.5 0.3
b 0.0 0.2 0.8
c 0.1 0.8 0.1
[[3]]
Unnamed Markov chain
A 3 - dimensional discrete Markov Chain with following states:
a, b, c
The transition matrix (by rows) is defined as follows:
a b c
a 0.2 0.5 0.3
b 0.0 0.2 0.8
c 0.1 0.8 0.1
markovchainListRcpp
我们得到两个序列:
> markovchainListRcpp(n = 2, object = mclist)
[[1]]
[1] 1 1 1 2 2 2
[[2]]
[1] "b" "c" "c" "a" "b" "b"
这些序列彼此独立。所以我想使用"b" "c" "c" (1)
"a" "b" "b" (2)
并行生成这些序列。
以下是我RcppParallel
实现并行生成n个序列
RCppParallel
看看当我调用并行功能时会发生什么。
struct SequenceMC : public Worker
{
// source
const List tmatList;
const int num_tmat;
// store results
List output;
// constructors
SequenceMC(const List tmat, const int n) : tmatList(tmat), num_tmat(n), output() {}
SequenceMC(SequenceMC &sequenceMC, Split) : tmatList(sequenceMC.tmatList), num_tmat(sequenceMC.num_tmat), output(){}
// process just the elements of the range I've been asked to
void operator()(std::size_t begin, std::size_t end) {
List temp;
CharacterVector helper;
int n = end-begin;
int m = num_tmat;
temp = markovchainListRcpp(n, tmatList, false);
helper = temp[1];
for(int i = 0;i < n;i++) {
CharacterVector help(num_tmat);
for(int j = 0;j < m;j++) {
help[j] = helper[m*i+j];
}
output.push_back(help);
}
}
void join(SequenceMC& rhs) {
List temp = rhs.output;
for(int i = 0;i < temp.size();i++) {
output.push_back(temp[i]);
}
}
};
// [[Rcpp::export]]
List markovchainSequenceParallelRcpp(List object, int n) {
List tmatList = object;
SequenceMC sequenceMC(tmatList, tmatList.size());
// call it with parallelReduce
parallelReduce(0, n, sequenceMC);
return sequenceMC.output;
}
我得到了结果。
> markovchainSequenceParallelRcpp(mclist, 5)
[[1]]
[1] "b" "c" "c"
[[2]]
[1] "b" "c" "c"
[[3]]
[1] "b" "c" "b"
[[4]]
[1] "b" "c" "c"
[[5]]
[1] "b" "c" "c"
这次RStudio崩溃 R会话中止。
但是有时候> markovchainSequenceParallelRcpp(mclist, 15)
会崩溃,有时它甚至不会崩溃n = 5
我没有得到什么。任何想法。
请查看我今天观察到的内容。
n = 20.
仍然无法解决问题。请建议任何方法来克服这个问题。我正在研究它的最后一周。现在似乎浪费时间了。请帮忙。