我有以下计划。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <ctime>
#include <cmath>
#include <RInside.h>
using std::cout;
using std::endl;
using std::vector;
using namespace Rcpp;
int main(int argc, char** argv){
RInside R;
R.parseEvalQ("set.seed(1)"); Rcpp::RNGScope();
const Function sample("sample");
vector<int> vv = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 1e6, true,NumericVector::create(.3,.3,.2,.2)));
cout<<"1"<<endl<<std::flush;
const vector<int> vv2 = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 2e6, true,NumericVector::create(.3,.3,.2,.2)));
cout<<"2"<<endl;
}
输出
1
Segmentation fault
这意味着无法初始化C ++向量vv2
。为什么不能分配vv2?
使用内联,这是我得到的:
> body <- '
+ using namespace Rcpp;
+ using std::vector;
+ using std::cout;
+ using std::endl;
+ Rcpp::RNGScope();
+ const Function sample("sample");
+ vector<int> vv = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 1e6, true,NumericVector::create(.3,.3,.2,.2)));
+ cout<<"1"<<endl<<std::flush;
+ const vector<int> vv2 = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 2e6, true,NumericVector::create(.3,.3,.2,.2)));
+ cout<<"2"<<endl;
+ List vecs(2);
+ vecs[1]=vv;
+ vecs[1]=vv2;
+ return(vecs);
+ '
> require( inline )
Loading required package: inline
> require( Rcpp )
Loading required package: Rcpp
Loading required package: int64
>
> signatures <- NULL
> fx <- cxxfunction( signatures, body, plugin = "Rcpp" )
> a <- fx()
*** caught segfault ***
address 0x7f2b850eb013, cause 'memory not mapped'
Traceback:
1: .Primitive(".Call")(<pointer: 0x7f2b8627d2c0>)
2: fx()
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
答案 0 :(得分:2)
您可以在机器上的R中执行相同的操作吗? 200万元素相当多。
此外,我几乎从不以这种方式回调R函数,因为它效率低下。如果你想加速,在C ++中重新实现sample()
这不会太难。
接下来,我还建议首先通过内联尝试更简单的表达式。
最后,我听起来像是破纪录,试试rcpp-devel列表。