简体   繁体   English

为什么这个矩阵乘法算法比另一个更快?

[英]why is this matrix multiplication algorithm faster than the other?

int mmult_omp(double *c,
           double *a, int aRows, int aCols,
           double *b, int bRows, int bCols, int numThreads)
{
  for (i = 0; i < aRows; i++) {
for (j = 0; j < bCols; j++) {
  c[i*bCols + j] = 0;
}
for (k = 0; k < aCols; k++) {
  for (j = 0; j < bCols; j++) {
                c[i*bCols + j] += a[i*aCols + k] * b[k*bCols + j];
  }
}

} }

for (i = 0; i < aRows; i++) {
    for (j = 0; j < bCols; j++) {
    c[i*bCols + j] = 0;
    for (k = 0; k < aCols; k++) {
    c[i*bCols + j] += a[i*aCols + k] *  b[k*bCols + j];
  }
}

} }

Why is the first algorithm faster than the second?为什么第一个算法比第二个算法快? I've used C's time library and the first algorithm is objectively faster than the second.我使用了 C 的时间库,第一个算法客观上比第二个算法快。 Why is that?这是为什么?

This code is very hard to understand.这段代码很难理解。 I had to copy it and reformat it to see what loops were what.我不得不复制它并重新格式化它以查看循环是什么。 I'm not really sure why one is faster but here's a great resource to see why.我不确定为什么一个更快,但这里有一个很好的资源来了解原因。

Here are links to inspect the assembly output:以下是检查程序集输出的链接:

link for #1 #1 的链接
link for #2 #2 的链接

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

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