Skip to content
Prev 4938 / 10988 Next

[Rcpp-devel] How to increase the coding efficiency

The comment of Doug Bates on this thread motivated me to better examine
opportunities for algorithm improvements in my R package c++ code written
under Rcpp and RcppArmadillo.   I performed a few changes:  firstly, i
removed all general matrix inverse computations, replacing many with
triangular solving from cholesky decompositions (e.g. to conduct posterior
sampling from a multivariate gaussian where the precision matrix is readily
available).   secondly, instead of repeatedly performing high-dimensional
matrix computations composing sampled parameter and design objects during a
sequential sampling scan, I made the computation once per iteration and
then operated on the resulting large object by repeatedly extracting
portions, pre-sampling, and inserting back the post-sampled result during a
sequential scan.   thirdly,  while linear algebra compositions of
parameters and data objects must be rendered repeatedly across posterior
sampling iterations, compositions of design/data objects (with each other)
may be performed only once and re-used.   I had been sloppy and was
repeatedly performing computations purely involving data objects inside the
sampling loops.   I replaced this approach by slicing and dicing the design
objects and performing the matrix operations, once, and then repeatedly
inserting the objects during sampling.   While the first two steps -
particularly the second - produced some runtime speed improvements, I was
surprised that the third step produced only a very small improvement where
I had expected a large reduction in runtime.   I used RcppArmadillo and
placed these sliced and diced matrix objects into fields from which I then
extracted elements during sampling.   So I replaced matrix computations
with extractions from field containers.   Is it possible that extraction
speeds from fields and multiplying a chain of moderate-dimensioned matrices
(e.g. 5 x 600)  are of equivalent computational intensity?  Was the
compiler compensating for my hackery?


On Tue, Dec 11, 2012 at 7:07 AM, Honglang Wang
<wanghonglang2008 at gmail.com>wrote: