An Rcpp object uses the storage allocated in R hence it has the same column-major ordering of the elements in a matrix.  The same is true of matrix objects in RcppArmadillo.<div><br></div><div>Basically anything that uses Lapack and BLAS or calling sequences compatible with Lapack or BLAS uses column-major ordering.</div>
<div><br></div><div>With RcppEigen the default is column-major but you can specify row-major.  However, when wrapping the Eigen object to return it to R there will be a copying operation to convert row major to column major.</div>
<div><br></div><div>You are probably safest staying with column major ordering if possible.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Nov 7, 2012 at 1:25 PM, Ramon Diaz-Uriarte <span dir="ltr"><<a href="mailto:rdiaz02@gmail.com" target="_blank">rdiaz02@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Dear All,<br>
<br>
I've recently started using Rcpp, so maybe this is a trivial question, but<br>
I have not been able to find an answer in previous posts or the docs.<br>
<br>
<br>
I thought that, for any of the Matrix types available, if we have<br>
<br>
x(i, j)<br>
<br>
then<br>
<br>
x(i, j + 1)<br>
<br>
would be contiguous in memory (whereas x(i + 1, j) could be extremely far<br>
away, depending on the size of the matrix).<br>
<br>
However, it seems that with Matrix objects it is the other way around, so<br>
that x(i, j) is next to x(i + 1, j), not to x(i, j + 1):<br>
<br>
#################<br>
<br>
showThePointers <- cxxfunction(signature(dm = "integer"),<br>
                     body = '<br>
<br>
int m = as<int>(dm);<br>
IntegerMatrix xm(m, m);<br>
int *p;<br>
int i;<br>
int j;<br>
<br>
std::cout << std::endl << "***** Row-major access *****" << std::endl;<br>
<br>
for(i = 0; i < m; i++) {<br>
   for(j = 0; j < m; j++) {<br>
     xm(i, j) = i * m + j;<br>
     p = &xm(i, j);<br>
     std::cout << "i = " << i << "; j = " <<<br>
     j << "; xm(i, j) = " << xm(i, j) << "; p " << p << std::endl;<br>
  }<br>
}<br>
<br>
std::cout << std::endl << " ****** Column-major access *****" << std::endl;<br>
<br>
for(j = 0; j < m; j++) {<br>
   for(i = 0; i < m; i++) {<br>
     xm(i, j) = i * m + j;<br>
     p = &xm(i, j);<br>
     std::cout << "i = " << i << "; j = " <<<br>
     j << "; xm(i, j) = " << xm(i, j) << "; p " << p << std::endl;<br>
  }<br>
}<br>
<br>
',<br>
                     plugin = "Rcpp",<br>
                     verbose = TRUE)<br>
<br>
showThePointers(3)<br>
<br>
#################<br>
<br>
<br>
<br>
<br>
I noticed that because if we time functions that access and modify in<br>
row-major order, they are a lot slower than in column-major order (in my<br>
laptop, about four times slower in the examples below, about nine times if<br>
I then return the object to R).<br>
<br>
<br>
#################<br>
<br>
byRow <- cxxfunction(signature(dm = "integer"),<br>
                     body = '<br>
<br>
int m = as<int>(dm);<br>
IntegerMatrix xm(m, m);<br>
int i;<br>
int j;<br>
for(i = 0; i < m; i++) {<br>
   for(j = 0; j < m; j++) {<br>
     xm(i, j) = i * m + j;<br>
  }<br>
}<br>
',<br>
                     plugin = "Rcpp",<br>
                     verbose = TRUE)<br>
<br>
byCol <- cxxfunction(signature(dm = "integer"),<br>
                     body = '<br>
<br>
int m = as<int>(dm);<br>
IntegerMatrix xm(m, m);<br>
int i;<br>
int j;<br>
for(j = 0; j < m; j++) {<br>
   for(i = 0; i < m; i++) {<br>
     xm(i, j) = i * m + j;<br>
  }<br>
}<br>
',<br>
                     plugin = "Rcpp",<br>
                     verbose = TRUE)<br>
<br>
<br>
nn <- 10000<br>
benchmark(byRow(nn), byCol(nn),<br>
          columns=c("test", "replications", "elapsed", "relative",<br>
          "user.self", "sys.self"),<br>
          order="relative", replications=10)<br>
<br>
<br>
#################<br>
<br>
<br>
I understand it is not possible to store things in row-major order with<br>
Rcpp Matrix types?<br>
<br>
<br>
Best,<br>
<br>
<br>
R.<br>
<br>
<br>
--<br>
Ramon Diaz-Uriarte<br>
Department of Biochemistry, Lab B-25<br>
Facultad de Medicina<br>
Universidad Autónoma de Madrid<br>
Arzobispo Morcillo, 4<br>
28029 Madrid<br>
Spain<br>
<br>
Phone: +34-91-497-2412<br>
<br>
Email: <a href="mailto:rdiaz02@gmail.com">rdiaz02@gmail.com</a><br>
       <a href="mailto:ramon.diaz@iib.uam.es">ramon.diaz@iib.uam.es</a><br>
<br>
<a href="http://ligarto.org/rdiaz" target="_blank">http://ligarto.org/rdiaz</a><br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div><br></div>