简体   繁体   English

矩阵 - 矩阵乘法

[英]Matrix-Matrix Multiplication

I'm writing a C code including matrix multiplication and I'm using 3 nested loops for that operation. 我正在写一个包含矩阵乘法的C代码,我正在使用3个嵌套循环进行该操作。 So, does anyone know how we can improve that code by removing one of the nested loops? 那么,有没有人知道如何通过删除其中一个嵌套循环来改进代码?

for (i = 0; i < SIZE; ++i)
    for (j = 0; j < SIZE; ++j)
        for (k = 0; k < SIZE; ++k)
            c[i][j] += a[i][k] * b[k][j];

Matrix multiplication for dense matrices has O(n^3). 密集矩阵的矩阵乘法具有O(n ^ 3)。 This can be accelerated by using Strassen's algorithm to O(n^(2.8)) or Coppersmith-Winogar to O(n^(2.37)). 这可以通过使用Strassen算法对O(n ^(2.8))或Coppersmith-Winogar到O(n ^(2.37))来加速。

Strassen algorithm is a classic one to try. Strassen算法是一个经典的算法。

http://en.wikipedia.org/wiki/Strassen_algorithm http://en.wikipedia.org/wiki/Strassen_algorithm

It is more complicated than writing three loops and the overall speed gain might not show through if the matrix size is small. 它比写三个循环更复杂,如果矩阵大小很小,整体速度增益可能无法显示。

As far as I know, Mathematica and Matlab use the three nested loop multiplication for small matrices and switch to Strassen for larger ones. 据我所知,Mathematica和Matlab使用三个嵌套循环乘法用于小矩阵,并切换到Strassen用于较大矩阵。

There are other algorithms that theoretically perform better asymptotically, but unless you are doing very very large matrix multiplication, I don't think it will help that much. 还有其他算法在理论上渐进地表现得更好,但除非你做的是非常大的矩阵乘法,否则我认为它不会那么有用。

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

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