【问题标题】:Matrix accessing and multiplication optimization for cpucpu的矩阵访问和乘法优化
【发布时间】:2013-07-16 17:00:06
【问题描述】:

我正在用 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} 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 内在函数(每个胎面同时在一个集合上工作)。

我只实现了向量*向量点积。 如果各位编程高手给个方向,我会进入这个。

我写的点积(使用内在函数)比循环展开版本快 6 倍(是一乘一乘的两倍),当在包装器中启用多线程时(8x -->使用接近 20GB/s,接近我的 ddr3 的限制)已经尝试过 opencl,它对 cpu 来说有点慢,但对 gpu 来说很好。

谢谢。

编辑:“块矩阵”缓冲区的性能如何?当相乘大矩阵时,小块以特殊方式相乘,缓存可能用于减少主内存访问。但这需要在垂直-水平-对角线和此块之间的矩阵乘法之间进行更多更新。

【问题讨论】:

  • 获得高度优化的矩阵代数代码的一种方法是使用表达式模板,以专门的方法压缩所有内容,而无需执行每个特定步骤。
  • 如何使用子矩阵和一些线性代数来使用缓存更多(块算法)?这对于垂直和水平缓冲方法是否可以接受?

标签: java c++ optimization matrix intrinsics


【解决方案1】:

这实际上等同于缓存转置。听起来您打算急切地这样做;我只会在需要时计算转置,并记住它以防再次需要它。那样的话,如果你从不需要它,它就永远不会被计算出来。

【讨论】:

    【解决方案2】:

    一些库使用Expression Templates 来启用非常具体的优化函数,用于级联矩阵运算。

    The C++ Programming Lanuage 还有一个关于“融合操作”的简短章节(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()
        }
    };
    

    您可以推进这个概念,以便能够为每一个无论如何都至关重要的计算提供一个特殊的函数。

    【讨论】:

    • 我正在测试向量 x 向量,它在 45 毫秒内将两个 64M 元素向量相乘(1.4GHz fx8150)。您是否也期望矩阵乘法具有类似的性能?(例如将两个大小为 256x512 和 512 x 512 的矩阵相乘)
    • 我不是线性代数大师,但我猜在跨步数据访问的情况下,“普通”矩阵乘法会遭受大量缓存未命中,但您显然愿意通过第二个数据集。但是,您应该小心避免引入任何人为瓶颈,因为两种表示都需要匹配,并且您需要更新例程。
    • 好的,刚刚完成了第一次乘法。在具有 1024x1024 矩阵的 1.4GHz fx8150 单核上,乘法需要 1.3 秒。你认为这有任何缓存未命中吗?
    • 当我将问题大小增加到 4096x4096(64 次工作)并将 cpu 频率增加到 4.0GHz 并通过 C# 的 Parallel.For 启用并行化时(是的,我将代码迁移到 C# 并易于并行化) ,乘法耗时 14.9 秒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多