我正在使用JNI帮助下开发一些优化的Java矩阵封装。需要确认的是,您能给出一些有关矩阵优化的提示吗?我要实现的是:
矩阵可以表示为四组缓冲区/数组,一个用于水平访问,一个用于垂直访问,一个用于对角线访问,以及一个命令缓冲区,仅在需要时计算矩阵元素。这里是一个示例。
Matrix signature:
0 1 2 3
4 5 6 7
8 9 1 3
3 5 2 9
First(hroizontal) set:
horSet[0]={0,1,2,3} horSet[1]={4,5,6,7} horSet[2]={8,9,1,3} horSet[3]={3,5,2,9}
Second(vertical) set:
verSet[0]={0,4,8,3} verSet[1]={1,5,9,5} verSet[2]={2,6,1,2} verSet[3]={3,7,3,9}
Third(optional) a diagonal set:
diagS={0,5,1,9} //just in case some calculation needs this
Fourth(calcuation list, in a "one calculation one data" fashion) set:
calc={0,2,1,3,2,5} --->0 means multiply by the next element
1 means add the next element
2 means divide by the next element
so this list means
( (a[i]*2)+3 ) / 5 when only a[i] is needed.
Example for fourth set:
A.mult(2), A.sum(3), A.div(5), A.mult(B)
(to list) (to list) (to list) (calculate *+/ just in time when A is needed )
so only one memory access for four operations.
loop start
a[i] = b[i] * ( ( a[i]*2) +3 ) / 5 only for A.mult(B)
loop end
如上所述,当需要访问列元素时,第二个集合提供连续访问,没有跳跃。水平访问可以通过第一个集合实现相同的效果。
这样做会使一些事情变得更容易,一些事情变得更困难:
Easier:
**Matrix transpozing operation.
Just swapping the pointers horSet[x] and verSet[x] is enough.
**Matrix * Matrix multiplication.
One matrix gives one of its horizontal set and other matrix gives vertical buffer.
Dot product of these must be highly parallelizable for intrinsics/multithreading.
If the multiplication order is inverse, then horizontal and verticals are switched.
**Matrix * vector multiplication.
Same as above, just a vector can be taken as horizontal or vertical freely.
Harder:
** Doubling memory requirement is bad for many cases.
** Initializing a matrix takes longer.
** When a matrix is multiplied from left, needs an update vertical-->horizontal
sets if its going to be multiplied from right after.(same for opposite)
(if a tranposition is taken between, this does not count)
Neutral:
** Same matrix can be multiplied with two other matrices to get two different
results such as A=A*B(saved in horizontal sets) A=C*A(saved in vertical sets)
then A=A*A gives A*B*C*A(in horizontal) and C*A*A*B (in vertical) without
copying A.
** If a matrix always multiplied from left or always from right, every access
and multiplication will not need update and be contiguous on ram.
** Only using horizontals before transpozing, only using verticals after,
should not break any rules.
主要目的是拥有一个大小为(8的倍数,8的倍数)的矩阵,并使用多个线程应用AVX指令集(每个线程同时处理一组)。
我只实现了向量*向量点积。如果您编程方面很精通,我会进一步学习。
我编写的点积(使用Intrinsics)比展开循环版本快6倍(后者是一一相乘的两倍),但在包装器中启用多线程时卡在存储带宽上限处(8x --> 使用近20GB/s,接近DDR3的极限)。已经尝试过OpenCL,对于CPU来说速度较慢,但对于GPU来说非常好。
谢谢。
编辑: "块矩阵"缓冲区如何执行?在乘法大矩阵时,小补丁以特殊方式相乘,并且可能使用缓存以减少主内存访问。但这需要在竖直-水平-对角和该块之间的矩阵乘法之间进行更多更新。