[Rcpp-devel] Matrix: column-major storage?

Ramon Diaz-Uriarte rdiaz02 at gmail.com
Thu Nov 8 15:50:21 CET 2012






On Thu, 8 Nov 2012 06:35:10 -0700,Christian Gunning <icos.atropa at gmail.com> wrote:
> [1  <text/plain; UTF-8 (quoted-printable)>]
> Interesting thread.  I had no idea the speed differences were that
> significant.
> I'm curious -- are there obvious use-cases where staying in column-major is
> *not* possible via careful setup?


I am sure staying with column-major is possible. Fortran also has
column-major order and a lot of things are (anything is ? ;-) possible
with Fortran. But when writing loops and traversing matrices and structs I
(so this might just be me) am used to thinking about row-major order.



> I realize it's a minimal test-case, but the cost of memory allocation in
> the above example dominates.


Hummm... interesting. Thanks for the example. I'll play with it for my
stuff.


Best,


R.


> Compare with:

> byRow_inplace <- cxxfunction(signature(xm_ = "numeric"),
>                      body = '
> //int m = as<int>(dm);
> //NumericMatrix xm(m, m);
> NumericMatrix xm(xm_);
> int m = xm.nrow();
> int i;
> int j;
> for(i = 0; i < m; i++) {
>    for(j = 0; j < m; j++) {
>      xm(i, j) = i * m + j;
>   }
> }
> ',
>                      plugin = "RcppArmadillo",
>                      verbose = TRUE)

> nn <- 10000
> aa <- matrix(nn, nrow=nn, ncol=nn)

> res <- benchmark(byRow(nn), byRow_inplace(aa),
>           columns=c("test", "replications", "elapsed", "relative",
>           "user.self", "sys.self"),
>           order="relative", replications=10)



> best,
> Christian

>  I thought that, for any of the Matrix types available, if we have
> > x(i, j)
> > then
> > x(i, j + 1)
> >
> > would be contiguous in memory (whereas x(i + 1, j) could be extremely far
> > away, depending on the size of the matrix).
> >
> > However, it seems that with Matrix objects it is the other way around, so
> > that x(i, j) is next to x(i + 1, j), not to x(i, j + 1):
> >



> -- 
> A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
> [2  <text/html; UTF-8 (quoted-printable)>]

-- 
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-25
Facultad de Medicina 
Universidad Autónoma de Madrid 
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdiaz02 at gmail.com
       ramon.diaz at iib.uam.es

http://ligarto.org/rdiaz



More information about the Rcpp-devel mailing list