2

I have numpy linked with mkl and I was doing matrix multiplication on sparse matrices and it seemed to be very slow. For example for matrix A of size (100000, 1000) , sparsity density = 0.1 and dtype=numpy.float64, I have timeit A.T.dot(A) giving me 5.24s but if I convert A to dense matrix and then do multiplication, I get ~650ms. Does blas operation does not kick in for sparse matrices? Also I have observerd that my cpu usage shows only one core in use while doing sparse multiplication but uses multiple cores for dense case.

My numpy.show_config() has following configuration :

lapack_opt_info:
    libraries = ['mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
    library_dirs = ['/home/user/miniconda2/envs/sketch/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/user/miniconda2/envs/sketch/include']
blas_opt_info:
    libraries = ['mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
    library_dirs = ['/home/user/miniconda2/envs/sketch/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/user/miniconda2/envs/sketch/include']
openblas_lapack_info:
  NOT AVAILABLE
lapack_mkl_info:
    libraries = ['mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
    library_dirs = ['/home/user/miniconda2/envs/sketch/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/user/miniconda2/envs/sketch/include']
blas_mkl_info:
    libraries = ['mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
    library_dirs = ['/home/user/miniconda2/envs/sketch/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/user/miniconda2/envs/sketch/include']

Any thoughts on how I can make numpy's performance improve on sparse matrices. Thanks.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
user1131274
  • 493
  • 6
  • 20
  • What's the dtype? – Divakar Jul 23 '17 at 13:53
  • dtype is numpy.float64 – user1131274 Jul 23 '17 at 13:54
  • If you have 41218703 nonzero values in a array with shape (583250, 76), why are you using a sparse matrix? Your array is 93% full! Of course sparse matrix multiplication is slower than dense array multiplication with such a densely populated matrix. Sparse matrices use a more complicated data structure than the simple block of memory used by dense arrays, and therefore require a multiplication algorithm written specifically for that data structure. – Warren Weckesser Jul 23 '17 at 14:35
  • @WarrenWeckesser You are right. But I think I have different issue because even with 10% nnz the sparse matrix multiplication seems to be like 10 times slower. Also my cpu usage shows only one core in use for sparse matrix mulitplication but uses multiple core for dense case. I have updated my question with information. Thanks – user1131274 Jul 23 '17 at 14:57
  • Also note that numpy knows nothing about sparse matrices. They are implemented in scipy (https://github.com/scipy/scipy/tree/master/scipy/sparse). – Warren Weckesser Jul 23 '17 at 15:22
  • @WarrenWeckesser Yeah. Also I have found this(https://stackoverflow.com/questions/25098653/is-it-possible-to-use-blas-to-speed-up-sparse-matrix-multiplication) which suggest that blas does not help with sparse matrices. Is there anything that can be done in this case then? Thanks – user1131274 Jul 23 '17 at 15:28
  • From last year - https://stackoverflow.com/q/37536106/901925 – hpaulj Jul 23 '17 at 15:47

0 Answers0