简体   繁体   中英

Can I speed up my MATLAB code using a MEX-file if the bottleneck is a big matrix multiplication?

I have a project which requires me to set up multiply two large matrices many times inside of a while loop. With larger matrices, the code can run quite slowly.

I'm just curious if using mex and a .cpp file can really increase my program's speed at this point. Isn't the built-in MATLAB matrix multiplication already quite good?

Without more specific information about your problem, there are few specific things that anyone can say. There are some situations where MEX functions can definitely increase performance, and some where they cannot. Matrix multiplication is one of Matlab's strengths, and simply moving a matrix multiplication into a MEX function is unlikely to make your code run faster.

That said, there are a few general strategies for improving performance:

  1. Profile your code. Make sure that the matrix multiplication is actually the culprit. In my own experience, performance problems can come from a variety of sources, including careless use of temporary variables. This should always be your first step.

  2. If your matrices have any kind of structure, exploit it. Matlab has pretty good support for sparse and banded matrices. If your matrix has structure, using it can considerably reduce the cost of matrix operations.

  3. If you do write a MEX function, try to move the entire while loop into the MEX function to avoid crossing the Matlab/MEX boundary more than once. It can be quite expensive to repeatedly call into a MEX function, and it's often just as easy (or easier) to perform the entire loop inside the MEX function, especially if the loop is simple iteration.

First of all, I am making the assumption that these matrices are related to each other, evolving over time or a similar situation. If each matrix from one instance to the next is completely unrelated this won't help. If they are related, however, it occurred to me that the change in the matrix might be small or even sparse. Given your basic equation C(i) = A(i)*B(i) If you create the delta matrices Da = A(i+1) - A(i) Db = B(i+1) - B(i) then of course C(i+1) = C(i) + Da*B(i) + A(i)*Db + Da*Db where C(i) is known. These 3 matrix multiplies may be much faster, if Da and Db are small or sparse. They may even be orthogonal and the last term dropped, or its result neglected as being second order in magnitude. Just a thought. There are many tools out there for Matlab matrix problems, don't give up on Matlab yet!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM