C++ 如何优化矩阵乘法运算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6061921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:25:48  来源:igfitidea点击:

How to optimize matrix multiplication operation

c++matrixmatrix-multiplication

提问by george

I need to perform a lot of matrix operations in my application. The most time consuming is matrix multiplication. I implemented it this way

我需要在我的应用程序中执行很多矩阵运算。最耗时的是矩阵乘法。我是这样实现的

template<typename T>
Matrix<T> Matrix<T>::operator * (Matrix& matrix)
{


    Matrix<T> multipliedMatrix = Matrix<T>(this->rows,matrix.GetColumns(),0);

    for (int i=0;i<this->rows;i++)
    {
        for (int j=0;j<matrix.GetColumns();j++)
        {
            multipliedMatrix.datavector.at(i).at(j) = 0;
            for (int k=0;k<this->columns ;k++)
            {
                multipliedMatrix.datavector.at(i).at(j) +=  datavector.at(i).at(k) * matrix.datavector.at(k).at(j);
            }
            //cout<<(*multipliedMatrix)[i][j]<<endl;
        }
    }
    return multipliedMatrix;
}

Is there any way to write it in a better way?? So far matrix multiplication operations take most of time in my application. Maybe is there good/fast library for doing this kind of stuff ?? However I rather can't use libraries which uses graphic card for mathematical operations, because of the fact that I work on laptop with integrated graphic card.

有没有更好的写法??到目前为止,矩阵乘法运算在我的应用程序中花费了大部分时间。也许有好的/快速的库可以做这种事情??但是,我宁愿不能使用使用图形卡进行数学运算的库,因为我在带有集成图形卡的笔记本电脑上工作。

回答by Arlen

Eigenis by far one of the fastest, if not the fastest, linear algebra libraries out there. It is well written and it is of high quality. Also, it uses expression template which makes writing code that is more readable. Version 3 just released uses OpenMP for data parallelism.

Eigen是迄今为止最快的线性代数库之一,如果不是最快的话。它写得很好,质量很高。此外,它使用表达式模板使编写的代码更具可读性。刚刚发布的第 3 版使用 OpenMP 进行数据并行。

#include <iostream>
#include <Eigen/Dense>

using Eigen::MatrixXd;

int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
}

回答by Clinton

Boost uBLASI think is definitely the way to go with this sort of thing. Boost is well designed, well tested and used in a lot of applications.

Boost uBLAS我认为绝对是处理这类事情的方法。Boost 设计精良,经过充分测试,并在许多应用程序中使用。

回答by jedwards

Consider GNU Scientific Library, or MV++

考虑GNU Scientific LibraryMV++

If you're okay with C, BLASis a low-level library that incorporates both C and C-wrapped FORTRAN instructions and is used a huge number of higher-level math libraries.

如果你对 C 没问题,BLAS是一个低级库,它结合了 C 和 C 包装的 FORTRAN 指令,并使用了大量的高级数学库。

I don't know anything about this, but another option might be Meschachwhich seems to have decent performance.

我对此一无所知,但另一个选择可能是Meschach,它似乎具有不错的性能

Edit: With respect to your comment about not wanting to use libraries that use your graphics card, I'll point out that in many cases, the libraries that use your graphics card are specialized implementations of standard (non-GPU) libraries. For example, various implementations of BLAS are listed on it's Wikipedia page, only some are designed to leverage your GPU.

编辑:关于您关于不想使用使用您的显卡的库的评论,我会指出,在许多情况下,使用您的显卡的库是标准(非 GPU)库的专门实现。例如,BLAS 的各种实现都列在其 Wikipedia 页面上,只有一些旨在利用您的 GPU。

回答by Mayank

There is a book called Introduction to Algorithms. You may like to check the chapter of Dynamic Programming. It has an excellent matrix multiplication algo using dynamic programming. Its worth a read. Well, this info was in case you want to write your own logic instead of using a library.

有一本书叫Introduction to Algorithms。您可能想查看动态规划的章节。它具有使用动态规划的出色矩阵乘法算法。值得一读。好吧,这些信息是为了防止您想编写自己的逻辑而不是使用库。

回答by Yochai Timmer

There are plenty of algorithms for efficient matrix multiplication.

有很多有效的矩阵乘法算法。

Algorithms for efficient matrix multiplication

高效矩阵乘法算法

Look at the algorithms, find an implementations.

查看算法,找到实现。

You can also make a multi-threaded implementation for it.

你也可以为它做一个多线程的实现。