BOOST uBLAS matrix product extremely slow

时光怂恿深爱的人放手 提交于 2019-12-01 07:31:07

Post your C+ code for advice on any possible optimizations.

You should be aware however that Matlab is highly specialized for its designed task, and you are unlikely to be able to match it using Boost. On the other hand - Boost is free, while Matlab decidedly not.

I believe that best Boost performance can be had by binding the uBlas code to an underlying LAPACK implementation.

tunc

You should use noalias in the left hand side of matrix multiplications in order to get rid of unnecessary copies.

Instead of E = prod(A,B); use noalias(E) = prod(A,b);

From documentation:

If you know for sure that the left hand expression and the right hand expression have no common storage, then assignment has no aliasing. A more efficient assignment can be specified in this case: noalias(C) = prod(A, B); This avoids the creation of a temporary matrix that is required in a normal assignment. 'noalias' assignment requires that the left and right hand side be size conformant.

There are many efficient BLAS implementation, like ATLAS, gotoBLAS, MKL, use them instead.

I don't pick at the code, but guess the ublas::prod(A, B) using three-loops, no blocks and not cache friendly. If that's true, prod(A, B.trans()) will be much faster then others.

If cblas is avaiable, using cblas_dgemm to do the calculation. If not, you can simply rearrange the data, means, prod(A, B.trans()) instead.

Mike Dunlavey

You don't know what role memory-management is playing here. prod is having to allocate a 32mb matrix, and so is trans, twice, and then you're doing all that 10 times. Take a few stackhots and see what it's really doing. My dumb guess is if you pre-allocate the matrices you get a better result.

Other ways matrix-multiplication could be speeded up are

  • pre-transposing the left-hand matrix, to be cache-friendly, and

  • skipping over zeros. Only if A(i,k) and B(k,j) are both non-zero is any value contributed.

Whether this is done in uBlas is anybody's guess.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!