Sparse matrix vector product
1 回表示 (過去 30 日間)
古いコメントを表示
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
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
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!