Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow matrix multiplication performance using MTJ/Netlib (native)

I need to multiply big matrices of size 5000x5000 up to 20000x20000. I'm having a problem finding a library that has sparse matrices and yet can do fast multiplication.

First of all, I've read the previous question on the performance of Java matrix libraries (Performance of Java matrix math libraries?). Based on the top answer there, I decided to go with JBLAS since it was one of the fastest. In my case, it took roughly 50s or so to multiply a 5000x5000 matrix, which is quite a lot slower than Matlab but still tolerable.

The problem is the matrices can be quite large (up to 20k by 20k, or more), but they're generally sparse. Only 30% of the elements in the matrix are non-zeros. JBLAS doesn't provide any sparse matrix implementation, so the memory footprint required to store a large dense matrix can get quite prohibitive. I tried switching to MTJ/Netlib since it's supposed to be one of the better libraries in the benchmark that have sparse matrix. The note here (https://github.com/fommil/netlib-java/) says to get the best performance, I have to compile a native BLAS on my machine. So I downloaded OpenBLAS, compiled and installed it. I also run a few commands to set the OpenBLAS library on Ubuntu 13.10:

$ cd ~/build/OpenBLAS
$ make
$ sudo make install PREFIX=/usr/local/openblas
$ sudo cat "/usr/local/openblas/lib" > /etc/ld.so.conf.d/openblas.conf
$ sudo ldconfig
$ sudo update-alternatives --install /usr/lib/libblas.so.3 libblas.so.3 /usr/local/openblas/lib/libopenblas.so 90
$ sudo update-alternatives --config libblas.so.3

I selected my compiled OpenBLAS library in the last update-alternatives step. I assume that after this, Netlib picks up my compiled OpenBLAS library and uses it. I also ran some benchmark from http://r.research.att.com/benchmarks/R-benchmark-25.R and observed some speed-up in the before (using the default blas from ubuntu) and after case (using my compiled OpenBLAS).

However, matrix-matrix multiplication performance in MTJ is still very slow. For example, I have two matrices A = 5824x5824, W = 5824x4782. I multiply them like this in Java

Matrix AW = new FlexCompRowMatrix(A.numRows(), W.numColumns());
A.mult(W, AW);

The code has been running for more than 45 minutes now, enough to type this whole post, and it's still not finishing. Using JBLAS, the same matrix multiplication would take less than 1 minute. Is there anything that I missed ?

Thanks !

like image 739
x112341 Avatar asked Nov 14 '13 23:11

x112341


1 Answers

JBLAS does dense matrix operations. MJT does both dense and sparse. Using "sparse" matrices in a dense way is slow. FlexCompRowMatrix creates a sparse matrix.

What you want to do, to compare directly to JBLAS, is:

Matrix a = new DenseMatrix(5000,5000);
Matrix b = new DenseMatrix(5000,5000);
Matrix c = new DenseMatrix(5000,5000);

a.multAdd(b, c);

The performance using MJT+OpenBlas should be about the same as MatLab.

like image 99
Aleksandr Dubinsky Avatar answered Sep 28 '22 04:09

Aleksandr Dubinsky