CGEMM

In this article, we have covered CGEMM (Single Precision Complex General Matrix Multiplication) which is a standard library function for Matrix Multiplication and a variant of GEMM.

Table of contents:

  1. Introduction to CGEMM
  2. Use of CGEMM
  3. Difference between CGEMM and other GEMM functions

Introduction to CGEMM

CGEMM stands for "Single Precision Complex General Matrix Multiplication".

It is a standard gemm routine in BLAS and BLIS libraries like OpenBLAS and is used to do Matrix Multiplication. It performs the standard GEMM operation that is Matrix Matrix multiplication with the matrices being of datatype Complex Float 32 bits. Complex number means it has both real and imaginary part.

The API of CGEMM is as follows:

status dgemm(
    char transa,
    char transb,
    dim_t M,
    dim_t N,
    dim_t K,
    Complex_32 alpha,
    const Complex_32* A,
    dim_t lda,
    const Complex_32* B,
    dim_t ldb,
    Complex_32 beta,
    Complex_32* C,
    dim_t ldc
    )

Note:

  • The 3 matrices A, B and C are in Complex 32 bits datatype (32 bits).

CGEMM operation is defined as follows:

C = alpha * op(A) * op(B) + beta * C

where

  • op(X) = X or XT depending on transa and related values. X is a matrix.
  • alpha and beta are scalars
  • A, B, and C are matrices
  • op(A) is an MxK matrix
  • op(B) is an KxN matrix
  • C is an MxN matrix (output)

The parameters are as follows:

  • transa: Transposition flag for matrix A. If it is set to 0, op(A) = A and if it is set to 1, op(A) = AT.
  • transb: Transposition flag for matrix B. If it is set to 0, op(B) = B and if it is set to 1, op(B) = BT.
  • M, N, K: dimensions
  • alpha: parameter that is used to scale the product of matrices A and B.
  • A: Input matrix of size MxK
  • lda: Leading dimension for matrix A
  • B: Input matrix of size KxN
  • ldb: Leading dimension for matrix B
  • beta: Beta parameter that is used to scale matrix C
  • C: Output matrix
  • ldc: Leading dimension for matrix C

The different combinations in CGEMM will be:

  • C = alpha * A * B + beta * C
  • C = alpha * AT * B + beta * C
  • C = alpha * A * BT + beta * C
  • C = alpha * AT * BT + beta * C
  • C = alpha * A * BH + beta * C
  • C = alpha * AH * B + beta * C
  • C = alpha * AH * BT + beta * C
  • C = alpha * AT * BH + beta * C
  • C = alpha * AH * BH + beta * C

where:

  • T is transpose
  • H is conjugate transpose

The GEMM operations like CGEMM from any library are highly optimized for specific applications and platforms.

Use of CGEMM

CGEMM is a relatively less used GEMM function compared to SGEMM and DGEMM.

CGEMM is used in mathematical applications that require Complex numbers. Note CGEMM is not used in scientific applications as an higher precision alternative exists.

CGEMM functions are not used in Machine Learning models as it deals with real numbers. Note that CGEMM can be used with only the real component set in which case, it will act as SGEMM.

CGEMM calls are available in different libraries like:

  • BLAS like OpenBLAS
  • BLIS like FLAME BLIS
  • FBGEMM
  • OneDNN

and others.

Difference between cgemm and other gemm functions

  • CGEMM vs SGEMM + DGEMM"

CGEMM deals with complex numbers where there is a real and imaginary part.

On the other hand, SGEMM and DGEMM deal with real numbers only. Hence, CGEMM can be viewed as a generalization of SGEMM as the precision (float 32 bit) is same.

  • CGEMM vs GEMM

The main difference is that GEMM is the generalized group of functions. CGEMM is a specific implementation of GEMM.

The general GEMM functions have different variations with different datatypes for the 3 matrices involved like:

  • gemm_u8s8s32: GEMM with A of datatype unsigned INT8, B of datatype signed INT8 and output as signed INT32.
  • gemm_s8s8s32: GEMM with A of datatype signed INT8, B of datatype signed INT8 and output as signed INT32.

and much more.

The datatype is CGEMM is fixed that is Single 32 bits Complex numbers.

  • CGEMM vd ZGEMM

CGEMM deal with Single 32 bits while ZGEMM deal with Double 64 bits. Both deal with Complex numbers.

With this article at OpenGenus, you must have the complete idea of CGEMM.