How to reduce the overall cost(Storage as well as communication) of transfer a dense matrix in nested functions

3 views (last 30 days)
Suppose I have a dense matrix in the very first stage of my code and I need to tranfer that matrix into the function again and again and just need a matrix vector multiplication inside the function as shown below
clc
Matrix A= 100000×100000 (dense)
Program starts
{
Function1
{
Function2
{
Here I need Matrix multiplication with some vector (M.a = b)....... [a,b = vectors]
}
}
}
How can I tranfer this matrix efficiently in the code and get matrix-vector multiplication and How can I store it efficiently?

Answers (1)

Arjun
Arjun on 10 Oct 2024
I understand that you want to efficiently store and transfer dense matrices in your programs.
Here are some suggestions which might help you:
  • To avoid repeated transfers, you can make the matrix a “global” variable; however, this must be used sparingly and specific to the application. Refer to the documentation of “global” : https://www.mathworks.com/help/matlab/ref/global.html
  • You can also use “persistent” storage to preserve a variable inside a function between repeated function calls. To use “persistent” efficiently, refer to the documentation of “persistent”: https://www.mathworks.com/help/matlab/ref/persistent.html
  • To store matrix efficiently, you must ensure the correct datatype to use which can save space since storing a double is costlier compared to “int32” data type, if entries are single precision, then use “int32” or “single” instead of “double”.
  • You must use optimized function like “A*a” for efficient matrix vector multiplication to boost up speed as the internal implementations are highly optimized for performance.
You can try implementing the above suggestions to the problem.
I hope this will help!

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!