|
GeMM
The GeMM (General Matrix Multiply) method implements the general multiplication of two matrices. The operation is defined as C ← α A B + β C , where A and B matrices can be optionally transposed. With a normal multiplication of matrices AB (MatMul), the alpha scalar is assumed to be equal to 1 and beta equal to zero.
The main difference between GeMM and MatMul in terms of efficiency is that MatMul always creates a new matrix/vector object, while GeMM works with an existing matrix object and does not re-create it. Therefore, when you use GeMM and pre-allocate memory for the corresponding matrix, then, while working with the same matrix sizes, there will be no memory reallocations. This can be an important advantage of GeMM for bulk computing, for example, when running optimizations in a strategy tester or when training a neural network.
Similar to MatMul, GeMM also has 4 overloads. However, the semantics of the fourth overload has been modified in order to enable the multiplication of a vertical vector with a and horizontal one.
In an existing matrix/vector object, it is not necessary to pre-allocate memory. The memory will be allocated and filled with zeros at the first GeMM call.
Multiplying a matrix by a matrix: matrix C[M][N] = α * ( matrix A[M][K] * matrix B[K][N]) + β * matrix C[M][N]
bool matrix::GeMM(
|
Multiplying a vector by a matrix: vector C[N] = α * ( vector A[K] * matrix B[K][N]) + β * vector C[N]
bool vector::GeMM(
|
Multiplying a matrix by a vector: vector C[M] = α * ( matrix A[M][K] * vector B[K] * ) + β * vector C[M]
bool vector::GeMM(
|
Multiplying a vector by a vector: matrix C[M][N] = α * ( vector A[M] * vector B[N] * ) + β * matrix C[M][N]. This overload returns a matrix, unlike MatMul where it returns a scalar.
bool matrix::GeMM(
|
Parameters
A
[in] Matrix or vector.
B
[in] Matrix or vector.
alpha
[in] Alpha multiplier for the AB product.
beta
[in] Beta multiplier for the resulting C matrix.
flags
[in] The ENUM_GEMM enumeration value that determines whether matrices A, B and C are transposed.
Return Value
Returns true on success or false otherwise.
Enumeration of flags for the GeMM method.
ID |
Description |
---|---|
TRANSP_A |
Use transposed matrix A |
TRANSP_B |
Use transposed matrix B |
TRANSP_C |
Use transposed matrix C |
Note
Matrices and vectors of float, double and complex types can be used as parameters A and B. The template variants of the GeMM method are as follows:
bool matrix<T>::GeMM(const matrix<T> &A,const matrix<T> &B,T alpha,T beta,ulong flags);
|
Basically the general matrix multiplication function is described as:
C[m,n] = α *Sum(A[m,k]*B[k,n]) + β*C[m,n] |
with the following sizes: matrix A is M x K, matrix B is K x N and matrix C is M x N.
Thus, the matrices should be compatible for multiplication, i.e. the number of columns in the first matrix should be equal to the number of rows in the second matrix. Matrix multiplication is non-commutative: the result of multiplying the first matrix by the second one is not equal to the result of multiplying the second matrix by the first one in the general case.
Example:
void OnStart()
|
See also