Matrix accessing and multiplication optimization for cpu

别来无恙 提交于 2019-12-05 14:06:23

Several libraries use Expression Templates to enable the application of very specific, optimized functions for a cascade of matrix operations.

The C++ Programming Lanuage also has a short chapter about "Fused Operations" (29.5.4, 4th Edition).

This enables concatenation of statements à la:

M = A*B.transp(); // where M, A, B are matrices

You'll want to have 3 classes in this case:

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()
    }
};

You can advance this concept to be able to have a spcialized function for every computation that is critcal by any mean.

This is effectively equivalent to cacheing the transposition. It sounds like you intend to do this eagerly; I'd just compute the transposition only when it is needed and remember it in case it is needed again. That way, if you never need it then it never gets computed.

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