When should I be using `sparse`?

前端 未结 3 2102
渐次进展
渐次进展 2021-01-04 03:10

I\'ve been looking through Matlab\'s sparse documentation trying to find whether there are any guidelines for when it makes sense to use a sparse representation rather than

3条回答
  •  醉酒成梦
    2021-01-04 03:55

    Many operations on full matrices use BLAS/LAPACK library calls that are insanely optimized and tough to beat. In practice, operations on sparse matrices will only outperform those on full matrices in specialized situations that can sufficiently exploit (i) sparsity and (ii) special matrix structure.

    Just randomly using sparse probably will make you worse off. Example: which is faster, adding a 10000x10000 full matrix to a 10000x10000 full matrix? Or adding a 10000x10000 full matrix to an entirely sparse (i.e. everything is zero) 10000x10000 matrix? try it! On my system, the full + full is faster!

    What are some examples of situations where sparse CRUSHES full?

    Example 1: solving linear system A*x=b where A is 5000x5000 but is block diagonal matrix constructed of 500 5x5 blocks. Setup code:

    As = sparse(rand(5, 5));
    for(i=1:999)
       As = blkdiag(As, sparse(rand(5,5))); 
    end;                         %As is made up of 500 5x5 blocks along diagonal
    Af = full(As); b = rand(5000, 1);
    

    Then you can test speed difference:

    As \ b % operation on sparse As takes .0012 seconds
    Af \ b % solving with full Af takes about 2.3 seconds
    

    In general, a 5000 variable linear system is somewhat difficult, but 1000 separate 5 variable linear systems is trivial. The latter is basically what gets solved in the sparse case.

    The overall story is that if you have special matrix structure and can cleverly exploit sparsity, it's possible to solve insanely large problems that otherwise would be intractable. If you have a specialized problem that is sufficiently large, have a matrix that is sufficiently sparse, and are clever with linear algebra (so as to preserve sparsity), a sparse typed matrix can be extremely powerful.

    On the other hand, randomly throwing in sparse without deep, careful thought is almost certainly going to make your code slower.

提交回复
热议问题