1

I am using bigmemory package to handle large matrix of size 8000 x 8000.

What is the equivalent of row() and col() for big matrix?

When I tried using the above two functions to access the big.matrix object, I am receiving the following error.

"Error in row(phi) : a matrix-like object is required as argument to 'row'"

Below is my code snippet.

k <- big.matrix(nrow = 8000, ncol = 8000, type = 'double', init = 0)
k <- ifelse(row(k) < col(k), 0, (row(k)-col(k))^5 + 2)

2
  • 8000x8000 is only 500Mb. Maybe you don't need bigmemory here. I think you should code this function with Rcpp.
    – F. Privé
    Feb 15, 2018 at 11:52
  • Note that if your problem is that specific you could precompute pow(x, 5) + 2 in a NumericVector tab for x in [0; n[ and directly use tab[i - j] in the loops. Expect a dramatic decrease of computation time.
    – F. Privé
    Feb 15, 2018 at 13:55

1 Answer 1

1

So, with Rcpp, you can do:

// [[Rcpp::depends(BH, bigmemory)]]
#include <bigmemory/MatrixAccessor.hpp>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void fillBM(SEXP pBigMat) {

  XPtr<BigMatrix> xpMat(pBigMat);
  MatrixAccessor<double> macc(*xpMat);

  int n = macc.nrow();
  int m = macc.ncol();

  for (int j = 0; j < m; j++) {
    for (int i = j; i < n; i++) {
      macc[j][i] = pow(i - j, 5) + 2;
    }
  }
}

/*** R
library(bigmemory)
k <- big.matrix(nrow = 8000, ncol = 8000, type = 'double', init = 0)
k.mat <- k[]

system.time(
  fillBM(k@address)
)
k[1:5, 1:5]

system.time(
  k.mat <- ifelse(row(k.mat) < col(k.mat), 0, (row(k.mat)-col(k.mat))^5 + 2)
)
k.mat[1:5, 1:5]
all.equal(k.mat, k[])
*/

The Rcpp function takes 2 sec while the R version (on a standard R matrix) takes 10 seconds (and much more memory).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.