Sparse matrix vector product

1 view (last 30 days)
José Blasques
José Blasques on 9 Mar 2011
Commented: David Goodmanson on 10 May 2024
Hi,
I have to evaluate the following expression thousands of times (maybe up on the millions):
M=v1'*A*v2 + v2'*B*v2 + v2'*A*v1
where v1 and v2 are column vectors (typically six columns) with a large number of rows filled with zeros and only a few with real values. v1 and v2 have been stored as full vectors. A and B are sparse square symmetric matrices of the same size. Like v1 and v2, A and B also have a few blocks of non-zero values and have a large number of rows and columns filled with zeros. Finally, this expression is inside a parfor loop.
I am looking for suggestions as to how I can speed up this operation?
All help is gratefully appreciated.
Thanks in advance.
José
  2 Comments
John
John on 9 May 2024
Sparse matrix-vector multiplication:
- Since A and B are sparse matrices, you can utilize MATLAB's sparse matrix operations to perform efficient matrix-vector multiplications.
- Replace A*v2 with A*sparse(v2) and B*v2 with B*sparse(v2) to take advantage of sparse matrix-vector multiplication.
-------
Transpose and reuse intermediate results:
- Instead of computing v1'*A*v2 and v2'*A*v1 separately, you can compute v1'*A*v2 and then reuse the result for v2'*A*v1 by taking its transpose.
- Replace v2'*A*v1 with (v1'*A*v2)'.
--------
Preallocate memory:
- Preallocate memory for the output variable M before the loop to avoid repeated memory allocations.
- Use M = zeros(1, num_iterations) to preallocate memory for M, where num_iterations is the number of iterations in the parfor loop.
--------
Vectorize the loop:
- If possible, try to vectorize the parfor loop to reduce the overhead of parallel computation.
- Consider restructuring the loop to operate on multiple iterations simultaneously, if feasible.
David Goodmanson
David Goodmanson on 10 May 2024
Hello Jose, do any of v1, v2, A or B stay the same as the expresson is evauated maybe millions of times?

Sign in to comment.

Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!