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

John
John 2024년 5월 9일
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 2024년 5월 10일
Hello Jose, do any of v1, v2, A or B stay the same as the expresson is evauated maybe millions of times?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Sparse Matrices에 대해 자세히 알아보기

질문:

2011년 3월 9일

댓글:

2024년 5월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by