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

2 次查看(过去 30 天)
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?

回答(1 个)

Arjun
Arjun 2024-10-10
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!

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by