Search code examples
rrcppmmap

Rcpp function with mmap argument?


I wish to do calculations on elements of a vector using Rcpp, but the vector is getting so large (~60 GB) that I'm resorting to memory mapping it using the mmap package, but now it's the wrong type for my Rcpp function. Can this be overcome?

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double testRcpp(NumericVector input, int index) {
  return input(index);
}

/*** R
writeBin(seq(0,1,1e-6),"test.bin")

bigvector1 <- seq(0,1,1e-6)
bigvector2 <- mmap("test.bin",mode=double())

testRcpp(bigvector1,3)
testRcpp(bigvector2,3) #"Not compatible with requested type: [type=environment; target=double]"
*/

Solution

  • Since the mmap function in r returns an object with type=environment write bigvector2[] instead of bigvector2 to use its elements. Basically replace

    testRcpp(bigvector2,3)
    

    to

    testRcpp(bigvector2[],3)
    

    If you want to try using mmap in the cpp part of Rcpp in windows you can use my repo from https://github.com/CoderRC/libmingw32_extended .