Frage

Ich habe das folgende Programm.

#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;
}

Die Ausgabe ist

1
Segmentation fault

Das bedeutet, dass der vv2 des C ++ - Vektors nicht initialisiert werden kann.Warum kann vv2 nicht zugewiesen werden?

Mit Inline habe ich Folgendes:

> 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

War es hilfreich?

Lösung

Können Sie dasselbe in R auf Ihrem Computer tun?2 Millionen Elemente sind ziemlich viel.

Außerdem rufe ich R-Funktionen auf diese Weise fast nie zurück, da sie ineffizient sind.Wenn Sie eine Beschleunigung wünschen, implementieren Sie sample() in C ++ erneut, was nicht zu schwierig sein kann.

Als nächstes würde ich auch einen einfacheren Ausdruck empfehlen, der zuerst über Inline versucht wird.

Zum Schluss, und ich werde wie ein gebrochener Rekord klingen, probieren Sie die rcpp-devel-Liste aus.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top