如何优化矩阵乘法运算
我需要在我的应用程序中执行大量矩阵运算。最耗时的是矩阵乘法。我是这样实现的
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;
}
有没有办法以更好的方式编写它?到目前为止,矩阵乘法运算在我的应用程序中占据了大部分时间。也许有好的/快速的库可以做这种事情? 然而,我宁愿不能使用使用显卡进行数学运算的库,因为我在带有集成显卡的笔记本电脑上工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Eigen 是迄今为止最快(即使不是最快)的线性代数库之一在那里。写得很好,而且质量很高。此外,它使用表达式模板,使编写的代码更具可读性。刚刚发布的版本 3 使用 OpenMP 实现数据并行。
Eigen is 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.
Boost uBLAS 我认为绝对是处理这类事情的方式。 Boost 设计精良、经过充分测试并在许多应用中使用。
Boost uBLAS I 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.
考虑 GNU 科学库,或 MV++
如果你对 C 没问题,BLAS 是一个低级库,其中包含C 和 C 语言包装的 FORTRAN 指令,并使用大量高级数学库。
我对此一无所知,但另一个选择可能是 Meschach其中似乎具有不错的性能。
编辑:关于您关于不想使用使用显卡的库的评论,我将指出,在许多情况下,使用显卡的库是标准(非 GPU)库的专门实现。例如,在其 Wikipedia 页面上列出了 BLAS 的各种实现,只有某些实现是为了利用您的图形处理器。
Consider GNU Scientific Library, or MV++
If you're okay with C, BLAS is a low-level library that incorporates both C and C-wrapped FORTRAN instructions and is used a huge number of higher-level math libraries.
I don't know anything about this, but another option might be Meschach which seems to have decent performance.
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.
有一本叫做
算法导论
的书。您可能想查看动态规划的章节。它具有使用动态规划的出色矩阵乘法算法。值得一读。好吧,这个信息是为了防止您想编写自己的逻辑而不是使用库。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.有很多有效的矩阵乘法算法。
高效矩阵乘法的算法
查看算法,找到实现。
您还可以为其制作多线程实现。
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.