分配大向量的问题
Problems with assigning large vectors
我有以下程序。
#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
你能在你的机器上用R语言做同样的事情吗?200万个元素是相当多的。
同样,我几乎从不以这种方式回调R函数,因为它效率低下。如果你想要加速,在c++中重新实现sample()
不会太难。
接下来,我还建议首先通过内联尝试更简单的表达式。
最后,我要重复一遍,试试rcpp-devel列表。
相关文章:
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 添加存储在向量中的大整数的函数出现问题
- 尝试将向量初始化为类成员时出现问题
- 在将字符串放入字符串向量时遇到问题?
- 取消引用向量时出现问题
- 在C++中对结构向量进行排序时出现问题
- 在 c++ 中创建和访问 4D 向量时出现问题
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 使用指针向量(带代码)C++(以及对其使用的便利性的怀疑)时出现问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 在 c++ 中访问列表向量时出现问题
- c++向量push_back问题
- shared_ptr类的向量的向量初始化问题
- SWIG c++到python:向量问题
- 在 Rcpp-Function 中使用布尔向量的子集(Rcpp 初学者的问题..)
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 在构造函数中更新指针向量时出现问题
- 在使用 std::sort() 和 lambda 函数按属性对 ADT 的向量进行排序时遇到问题
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- C++:问题向量 STL.