Travis Bemrose
Travis Bemrose

Reputation: 442

MATLAB sparsity - Is there a speed advantage in my situation?

I have an MxM matrix S whose entries are zero on the diagonal, and non-zero everywhere else. I need to make a larger, block matrix. The blocks will be size NxN, and there will be MxM of them.

The (i,j)th block will be S(i,j)I where I=eye(N) is the NxN identity. This matrix will certainly be sparse, S has M^2-M nonzero entries and my block matrix will have N(M^2-M) out of (NM)^2 or about 1/N% nonzero entries, but I'll be adding it to another NMxNM matrix that I do not expect to be sparse.

Since I will be adding my block matrix to a full matrix, would there be any speed gain by trying to write my code in a 'sparse' way? I keep going back and forth, but my thinking is settling on: even if my code to turn S into a sparse block matrix isn't very efficient, when I tell it to add a full and sparse matrix together, wouldn't MATLAB know that it only needs to iterate over the nonzero elements? I've been trained that for loops are slow in MATLAB and things like repmat and padding with zeros is faster, but my guess is that the fastest thing to do would be to not even build the block matrix at all, but write code that adds the entries of (the small matrix) S to my other (large, full) matrix in a sparse way. If I were to learn how to build the block matrix with sparse code (faster than building it in a full way and passing it to sparse), then that code should be able to do the addition for me in a sparse way without even needing to build the block matrix right?

Upvotes: 3

Views: 302

Answers (2)

Matthew Gunn
Matthew Gunn

Reputation: 4519

From your description, using sparse is likely slower for your problem:

If you're adding a sparse matrix A to a full matrix B, the result is full and there's almost certainly no advantage to having A sparse.

For example: n = 12000; A = rand(n, n); B1 = rand(n, n); B2 = spalloc(n, n, n*n); B2 is as sparse as possible, that is, it's all zeros! On my machine, A+B1 takes about .23 seconds while A + B2 takes about .7 seconds.

Basically, operations on full matrices use BLAS/LAPACK library calls that are insanely optimized. Overhead associated with sparse is going to make things worse unless you're in the special cases where sparse is super useful.

When is sparse super useful?

Sparse is super useful when the size of matrices suggest that some algorithm should be very slow, but because of sparsity (+ perhaps special matrix structure), the actual number of computations required is orders of magnitude less.

EXAMPLE: Solving linear system A*x=b where A is block diagonal matrix: As = sparse(rand(5, 5)); for(i=1:999) As = blkdiag(As, sparse(rand(5,5))); end %generate a 5000x5000 sparse block diagonal matrix of 5x5 blocks

Af = full(As); b = rand(5000, 1);

On my machine, solving the linear system on the full matrix (i.e. Af \ b) takes about 2.3 seconds while As \ b takes .0012 seconds.

Sparse can be awesome, but it's only helpful for large problems where you can cleverly exploit structure.

Upvotes: 1

Felix Darvas
Felix Darvas

Reputation: 507

If you can keep a full NMxNM matrix in memory, dont bother with sparse operations. In fact in most cases A+B, with A full and B sparse, will take longer than A+B, where A and B are both full.

Upvotes: 1

Related Questions