[Rcpp-devel] Avoiding memory allocations when using Armadillo, BLAS, and LAPACK

Nathan Kurz nate at verse.com
Mon Feb 16 20:19:44 CET 2015


I'm trying to minimize the "churn" of memory in R, since I've found
that the cost of allocation plus eventual garbage collection is
frequently the most expensive part of my program.  Being able to make
"in place" modifications is helpful, although I don't yet understand
when a copy is made of an an argument and when an original is passed.
What code would I look at to see how an argument is handled as it is
passed from R to an Rcpp function?   Does the potential duplication
occur only in .Call() or are there Rcpp specific conditions as well?

I think the sort of interface I'm looking for is:
outMatrix = operation(inMatrix1, inMatrix2, optionalOutMatrix)

If the 'optionalOutMatrix' is of sufficient size for the answer, it
will be used and returned.   If it's not provided, or too small, a new
allocation will be made in the way it is done currently.   Are there
any existing interfaces to the BLAS and LAPACK libraries that take
this form, ideally already using the Rcpp classes?   My quick
examination of Armadillo hasn't shown me how to make it work in this
fashion.

My rambling question with more details, measurements, and sample code
is here: http://stackoverflow.com/questions/28532493/reusing-existing-memory-for-blas-operations-in-r

Thanks,

Nathan Kurz
nate at verse.com


More information about the Rcpp-devel mailing list