Java cpu的矩阵存取与乘法优化
我正在用java制作一些内部优化的矩阵包装器(借助JNI)。需要肯定这一点,你能给出一些关于矩阵优化的提示吗?我要实现的是: 矩阵可以表示为四组缓冲区/数组,一组用于水平访问,一组用于垂直访问,一组用于对角访问,以及一个仅在需要时计算矩阵元素的命令缓冲区。这是一个例子Java cpu的矩阵存取与乘法优化,java,c++,optimization,matrix,intrinsics,Java,C++,Optimization,Matrix,Intrinsics,我正在用java制作一些内部优化的矩阵包装器(借助JNI)。需要肯定这一点,你能给出一些关于矩阵优化的提示吗?我要实现的是: 矩阵可以表示为四组缓冲区/数组,一组用于水平访问,一组用于垂直访问,一组用于对角访问,以及一个仅在需要时计算矩阵元素的命令缓冲区。这是一个例子 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} h
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 Intrinsic(每个踏板同时在一组上工作)
我只得到了向量*向量点积如果编程大师们给出指导,我将对此进行探讨。
我编写的dotproduct(使用内部函数)比循环展开版本快6倍(一乘一的速度是两倍),当包装器中启用多线程(8x-->使用接近我的ddr3限制的近20GB/s)尝试opencl时,也会遇到内存带宽上限问题,cpu速度有点慢,但是对于gpu来说很棒
多谢各位
编辑:一个“块矩阵”缓冲区将如何执行?当与大矩阵相乘时,小的补丁会以一种特殊的方式相乘,缓存可能用于减少主内存访问。但这需要在垂直-水平对角线和该块之间的矩阵乘法之间进行更多更新。这实际上相当于缓存转置。听起来你似乎很想这么做;我只会在需要的时候计算转置,并记住它以防再次需要。这样,如果您永远不需要它,那么它就永远不会被计算。有几个库用于支持对矩阵操作的级联应用非常特定、优化的函数 还有一个关于“熔合操作”的简短章节(29.5.4,第4版) 这样就可以将语句连接起来:
M = A*B.transp(); // where M, A, B are matrices
在这种情况下,您需要有3个类:
class Matrix;
class Transposed
{
public:
Transposed(Matrix &matrix) : m_matrix(matrix) {}
Matrix & obj (void) { return m_matrix; }
private:
Matrix & m_matrix;
};
class MatrixMatrixMulTransPosed
{
public:
MatrixMatrixMulTransPosed(Matrix &matrix, Transposed &trans)
: m_matrix(matrix), m_transposed(trans.obj()) {}
Matrix & matrix (void) { return m_matrix; }
Matrix & transposed (void) { return m_transposed; }
private:
Matrix & m_matrix;
Matrix & m_transposed;
};
class Matrix
{
public:
MatrixMatrixMulTransPosed operator* (Transposed &rhs)
{
return MatrixMatrixMulTransPosed(*this, rhs);
}
Matrix& operator= (MatrixMatrixMulTransPosed &mmtrans)
{
// Actual computation goes here and is stored in this.
// using mmtrans.matrix() and mmtrans.transposed()
}
};
你可以提出这个概念,使它能够为每一个计算都有一个特殊的函数,不管用什么方法,它都是至关重要的。获得高度优化的矩阵代数代码的一种方法是使用表达式模板,在不执行每一个特定步骤的情况下,用专门的方法压缩整个内容。那么使用子矩阵和一些线性矩阵呢代数更多地使用缓存(块算法)?这是否适用于垂直和水平缓冲区方法?Im测试向量x向量,它在45毫秒内乘以两个64M元素向量(1.4GHz fx8150)。你会期望矩阵乘法也有类似的性能吗?(比如将两个256x512和512 x 512大小的矩阵相乘)我不是线性代数大师,但我猜是“普通的”矩阵乘法在数据访问速度加快的情况下会出现大量缓存未命中,但您显然愿意通过第二个数据集来说明这一点。不过,您应该小心避免引入任何人为的瓶颈,因为两种表示都需要匹配,并且需要有更新例程。好的,刚刚完成了第一次乘法。在1.4GHz fx8150和1024x1024矩阵的单核上,乘法耗时1.3秒。你认为这会导致缓存丢失吗?当我将问题大小增加到4096x4096(工作64倍)并将cpu频率增加到4.0GHz并启用并行化时。对于C#(是的,我将代码迁移到C#,具有简单的并行化),乘法耗时14.9秒。