Matlab中的快速矩阵乘法

发布于 2024-12-08 08:05:34 字数 474 浏览 2 评论 0 原文

我需要在 Matlab 中进行非常大尺寸的矩阵/向量乘法:“A”是一个 655360 x 5 实值矩阵,不一定是稀疏的,“B”是一个 655360 x 1 实值向量。我的问题是如何有效地计算:B'*A。

我注意到通过计算 A'*B 可以稍微提高时间,它给出了一个列向量。但仍然很慢(我需要在程序中多次执行此操作)。

经过一点搜索,我发现了一个有趣的 Matlab 工具箱 MTIMESX,我希望它能够提高上述矩阵乘法的性能。经过几次试验,我只能比 Matlab 原生矩阵乘法获得非常有限的收益。

关于如何重写 A'*B 以使操作更有效有什么建议吗?谢谢。

I need to make a matrix/vector multiplication in Matlab of very large sizes: "A" is an 655360 by 5 real-valued matrix that are not necessarily sparse and "B" is a 655360 by 1 real-valued vector. My question is how to compute: B'*A efficiently.

I have notice a slight time improvement by computing A'*B instead, which gives a column vector. But still it is quite slow (I need to perform this operation several times in the program).

With a little bit search I found an interesting Matlab toolbox MTIMESX by James Tursa, which I hoped would improve the above matrix multiplication performance. After several trials, I can only have very marginal gains over the Matlab native matrix multiplication.

Any suggestions about how should I rewrite A'*B so that the operation is more efficient? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

救赎№ 2024-12-15 08:05:34

Matlab 存在的理由是进行矩阵计算。如果您可以使用手工制作的工具显着优于其内置矩阵乘法,我会感到相当惊讶。首先,您应该确保您的乘法实际上可以明显更快地执行。您可以通过在 C++ 中使用 Eigen 实现类似的乘法来实现此目的。

Matlab's raison d'etre is doing matrix computations. I would be fairly surprised if you could significantly outperform its built-in matrix multiplication with hand-crafted tools. First of all, you should make sure your multiplication can actually be performed significantly faster. You could do this by implementing a similar multiplication in C++ with Eigen.

澜川若宁 2024-12-15 08:05:34

我使用 GPU 进行 matlab 矩阵乘法,取得了很好的结果

I have had good results with matlab matrix multiplication using the GPU

乖乖兔^ω^ 2024-12-15 08:05:34

为了避免转置操作,您可以尝试:

sum(bsxfun(@times, A, B), 2)

但我会惊讶它比直接版本更快。请参阅@thiton 的回答。

另请参阅 http://www.mathworks.co.uk/ company/newsletters/news_notes/june07/patterns.html 了解为什么基于列向量的版本比基于行向量的版本更快。

In order to avoid the transpose operation, you could try:

sum(bsxfun(@times, A, B), 2)

But I would be astonished it was faster than the direct version. See @thiton's answer.

Also look at http://www.mathworks.co.uk/company/newsletters/news_notes/june07/patterns.html to see why the column-vector-based version is faster than the row-vector-based version.

抠脚大汉 2024-12-15 08:05:34

Matlab 是使用相当优化的库(BLAS 等)构建的,因此您无法轻松地从 Matlab 内部对其进行改进。您可以改进的地方是获得更好的 BLAS,例如针对您的处理器进行优化的 BLAS - 这将通过从主内存获取适当大小的数据块来更好地使用缓存。了解如何创建您自己的 ATLAS、ACML、MKL 和 Goto BLAS 编译版本。

我不会尝试解决这个特定的乘法,除非它真的要了你的命。更改 BLAS 可能会带来更满意的解决方案,特别是如果您当前没有使用多核处理器。

Matlab is built using fairly optimized libraries (BLAS, etc.), so you can't easily improve upon it from within Matlab. Where you can improve is to get a better BLAS, such as one optimized for your processor - this will enable better use of the caches by getting appropriately sized blocks of data from main memory. Take a look into creating your own compiled versions of ATLAS, ACML, MKL, and Goto BLAS.

I wouldn't try to solve this one particular multiplication unless it's really killing you. Changing up the BLAS is likely to lead to a happier solution, especially if you're not currently making use of multicore processors.

闻呓 2024-12-15 08:05:34

如果这是您的瓶颈,您的第一选择是重新检查您的算法。请参阅此问题优化 MATLAB 代码,获取一个很好的示例,说明如何选择不同的算法将运行时间减少三个数量级震级。

Your #1 option, if this is your bottleneck, is to re-examine your algorithm. See this question Optimizing MATLAB code for a great example of how choosing a different algorithm reduced runtime by three orders of magnitude.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文