daknowles
daknowles

Reputation: 1430

Allocating a large 2D array of NumericVectors in Rcpp

I am trying to allocate a large(ish) 2D array of NumericVectors (the total memory should only be around 16Mb barring overhead), but I get a stack overflow. Here is the smallest reproducible example I can come up with:

require(Rcpp)
require(inline)
testfun = cxxfunction(
    signature(x="list"),
    body='using namespace std;
        vector<vector<NumericVector> > rs=as< vector<vector<NumericVector> > >( x );
        return wrap(rs);',
    plugin="Rcpp")
x=lapply(1:1000,function(g) lapply(1:1000, function(h) runif(3)))
testfun(x)

Profiling with gdb with only tells me that I am very deep in a libR.so recursion. Note that while here the arrays are rectangular so I could use a NumericMatrix instead in my actual problem they will be jagged. Thanks for any thoughts.

Upvotes: 1

Views: 483

Answers (1)

Kevin Ushey
Kevin Ushey

Reputation: 21315

EDIT: A solution using the (fairly new) ListOf<T> template class:

require(Rcpp)
require(inline)
testfun = cxxfunction(
  signature(x="list"),
  body='using namespace std;
        ListOf<ListOf<NumericVector> > rs=as< ListOf<ListOf<NumericVector> > >( x );
        return wrap(rs);',
  plugin="Rcpp")
x=lapply(1:1000,function(g) lapply(1:1000, function(h) runif(3)))
testfun(x)

We just replace any calls to std::vector with Rcpp::ListOf and, somewhat magically, it works. Best of all -- data is not copied here (assuming x really is a list of numeric vectors).

Upvotes: 2

Related Questions