简体   繁体   English

如果瓶颈是一个大的矩阵乘法,我可以使用MEX文件加速我的MATLAB代码吗?

[英]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. 我有一个项目,要求我在while循环中多次设置两个大矩阵。 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. 我只是好奇如果使用mex和.cpp文件可以真正提高程序的速度。 Isn't the built-in MATLAB matrix multiplication already quite good? 内置的MATLAB矩阵乘法不是很好吗?

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. 在某些情况下,MEX功能肯定可以提高性能,有些情况下它们不能。 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. 矩阵乘法是Matlab的优势之一,只需将矩阵乘法移动到MEX函数中就不太可能使代码运行得更快。

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. Matlab对稀疏带状矩阵有很好的支持。 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. 如果您确实编写了MEX函数,请尝试将整个while循环移动到MEX函数中,以避免多次穿过Matlab / MEX边界。 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. 重复调用MEX函数可能非常昂贵,并且在MEX函数内执行整个循环通常也很容易(或更容易),特别是如果循环是简单的迭代。

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. 给出基本方程C(i)= A(i)* B(i)如果创建delta矩阵Da = A(i + 1) - A(i)Db = B(i + 1) - B(i)那么当然C(i + 1)= C(i)+ Da * B(i)+ A(i)* Db + Da * Db其中C(i)是已知的。 These 3 matrix multiplies may be much faster, if Da and Db are small or sparse. 如果Da和Db很小或稀疏,则这3个矩阵乘法可以快得多。 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! Matlab矩阵问题有很多工具,不要放弃Matlab了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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