MKL学习——矩阵矩阵操作

前言

前面介绍了BLAS Level 1中向量-向量操作,以及BLAS Level 2中矩阵-向量的操作,就剩下这一篇的BLAS Level 3中的矩阵-矩阵的操作了。对稀疏矩阵的操作以后等要用再看看。对基本的矩阵和向量运算的函数有所了解以后,就进行常用参数分析,以及实现了。

所有函数概览

函数名称 缺失部分 描述
cblas_?gemm s,d,c,z 一般矩阵的矩阵-矩阵乘法
cblas_?hemm c,z 某个输入矩阵是Hermitian,然后计算矩阵-矩阵乘法
cblas_?herk c,z Hermitian的k阶更新
cblas_?herk2 c,z Hermitian的2k阶更新
cblas_?symm s,d,c,z 某个输入矩阵是对称的,然后计算矩阵-矩阵乘法
cblas_?syrk s,d,c,z 对称矩阵的k阶更新
cblas_?syr2k s,d,c,z 对称矩阵的2k阶更新
cblas_?trmm s,d,c,z 某个输入矩阵是三角的,就是你矩阵-矩阵的乘法
cblas_?trsm s,d,c,z 解三角矩阵方程

矩阵运算

cblas_?gemm

  • 作用 : 一般矩阵的矩阵-矩阵乘法

  • 定义函数

    void cblas_sgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const float alpha, const float *a, const MKL_INT lda, const float *b, const MKL_INT ldb, const float beta, float *c, const MKL_INT ldc);
    
    void cblas_dgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const double alpha, const double *a, const MKL_INT lda, const double *b, const MKL_INT ldb, const double beta, double *c, const MKL_INT ldc);
    
    void cblas_cgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
    
    void cblas_zgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
  • 运算

    C:=αop(A)op(B)+βC

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

    其中

    op(x)
    op(x)可以是

    op(x)=x
    op(x)=x或者

    op(x)=xT
    op(x)=x^T或者

    op(x)=XH
    op(x)=X^H

    α
    \\alpha和

    β
    \\beta是标量

    A,B,C
    A,B,C是矩阵:

    op(A)
    op(A)是一个

    mk
    m*k的矩阵,

    op(B)
    op(B)是

    kn
    k*n的矩阵,C是一个

    mn
    m*n的矩阵

  • 输入参数

    Layout: 指定矩阵是行优先(CblasRowMajor)还是列优先(CblasColMajor)

    transa: 指定对矩阵的操作

    op(A)
    op(A),如果transa=CblasNoTrans那么

    op(A)=A
    op(A)=A;如果transa=CblasTrans那么

    op(A)=AT
    op(A)=A^T;如果transa=CblasConjTrans那么

    op(A)=AH
    op(A)=A^H

    transb: 同上

    m: 矩阵

    op(A)
    op(A)和

    C
    C的行数

    n: 矩阵op(B)op(B)和C的列数

    k: 矩阵

    op(A)
    op(A)和

    op(B)
    op(B)的列数

    alpha: 标量

    a:

transa=CblasNoTrans transa=CblasTrans或者transa=CblasConjTrans
Layout=CblasColMajor 矩阵大小

ldak
lda*k

Layout=CblasRowMajor 矩阵大小

ldam
lda*m

lda : 引导维度

transa=CblasNoTrans transa=CblasTrans或者transa=CblasConjTrans
Layout=CblasColMajor

lda
lda至少为

max(1,m)
max(1,m)

Layout=CblasRowMajor

lda
lda至少为

max(1,k)
max(1,k)

b:

transa=CblasNoTrans transa=CblasTrans或者transa=CblasConjTrans
Layout=CblasColMajor 矩阵大小

ldbn
ldb*n

Layout=CblasRowMajor 矩阵大小

ldbk
ldb*k

ldb:

transa=CblasNoTrans transa=CblasTrans或者transa=CblasConjTrans
Layout=CblasColMajor

ldb
ldb至少为

max(1,k)
max(1,k)

Layout=CblasRowMajor

ldb
ldb至少为

max(1,n)
max(1,n)

beta: 标量

c: 当Layout=CblasColMajor时候,数组大小为

ldan
lda*n;当Layout=CblasRowMajor时候,数组大小为

ldam
lda*m

ldc:当Layout=CblasColMajor的时候,ldc必须至少为

max(1,m)
max(1,m);当Layout=CblasRowMajor的时候,ldc必须至少为

max(1,n)
max(1,n)

  • 输出参数: 将计算得到的矩阵写入到c

cblas_?hemm

  • 作用 : 当一个输入矩阵为Hermitian时,计算矩阵-矩阵的乘法

  • 定义函数

    void cblas_chemm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
    
    void cblas_zhemm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
  • 运算

    C:=αAB+βCC:=αBA+βC

    C:=\\alpha*A*B+\\beta*C\\\\
    C:=\\alpha*B*A+\\beta *C

    其中,

    α,β
    \\alpha,\\beta是标量,

    A
    A是Hermitian矩阵,B,CB,C是

    mn
    m*n的矩阵

cblas_?herk

  • 作用: Hermitian的k阶更新

  • 定义函数:

    void cblas_cherk (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const float alpha, const void *a, const MKL_INT lda, const float beta, void *c, const MKL_INT ldc);
    
    void cblas_zherk (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const double alpha, const void *a, const MKL_INT lda, const double beta, void *c, const MKL_INT ldc);
  • 运算

    C:=αAAH+βCC:=αAHA+βC

    C:=\\alpha*A*A^H+\\beta*C\\\\
    C:=\\alpha*A^H*A+\\beta*C

    其中,

    α,β
    \\alpha,\\beta是标量,

    C
    C是nnn*n的Hermitian矩阵,第一个式子的

    A
    A是nkn*k矩阵,第二个式子的

    A
    A是knk*n的矩阵

cblas_?herk2

  • 作用: Hermitian矩阵的2阶更新

  • 定义函数

    void cblas_cher2k (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const float beta, void *c, const MKL_INT ldc);
    
    void cblas_zher2k (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const double beta, void *c, const MKL_INT ldc);
  • 运算

    C:=αABH+conjg(α)BAH+βCC:=αAHB+conjg(α)BHA+βC

    C:=\\alpha*A*B^H+conjg(\\alpha)*B*A^H+\\beta*C\\\\
    C:=\\alpha*A^H*B+conjg(\\alpha)*B^H*A+\\beta*C

    其中,

    α,β
    \\alpha,\\beta是标量,

    C
    C是nnn*n是Hermitian矩阵,第一个式子中

    A,B
    A,B是

    nk
    n*k矩阵,第二个式子中是

    kn
    k*n的矩阵

cblas_?symm

  • 作用: 某个输入是对称矩阵时候,计算矩阵-矩阵的乘法

  • 定义函数

    void cblas_ssymm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const MKL_INT m, const MKL_INT n, const float alpha, const float *a, const MKL_INT lda, const float *b, const MKL_INT ldb, const float beta, float *c, const MKL_INT ldc);
    
    void cblas_dsymm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const MKL_INT m, const MKL_INT n, const double alpha, const double *a, const MKL_INT lda, const double *b, const MKL_INT ldb, const double beta, double *c, const MKL_INT ldc);
    
    void cblas_csymm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
    
    void cblas_zsymm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
  • 运算

    C:=αAb+βCC:=αBA+βC

    C:=\\alpha*A*b+\\beta*C\\\\
    C:=\\alpha*B*A+\\beta*C

    其中

    α,β
    \\alpha,\\beta是标量,

    A
    A是对称阵,B,CB,C是

    mn
    m*n的矩阵

cblas_?syrk

  • 作用: 对称矩阵的k阶更新

  • 定义函数

    void cblas_ssyrk (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const float alpha, const float *a, const MKL_INT lda, const float beta, float *c, const MKL_INT ldc);
    
    void cblas_dsyrk (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const double alpha, const double *a, const MKL_INT lda, const double beta, double *c, const MKL_INT ldc);
    
    void cblas_csyrk (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *beta, void *c, const MKL_INT ldc);
    
    void cblas_zsyrk (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *beta, void *c, const MKL_INT ldc)
  • 运算

    C:=αAA+βCC:=αAA+βC

    C:=\\alpha*A*A’+\\beta*C\\\\
    C:=\\alpha*A\’*A+\\beta*C

    其中,

    α,β
    \\alpha,\\beta是标量,

    C
    C是nnn*n的对称阵,第一个式子中

    A
    A是nnn*n的矩阵,第二个式子中

    A
    A是knk*n的矩阵

cblas_?syr2k

  • 作用: 对称阵的二阶更新

  • 定义函数

    void cblas_ssyr2k (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const float alpha, const float *a, const MKL_INT lda, const float *b, const MKL_INT ldb, const float beta, float *c, const MKL_INT ldc);
    
    void cblas_dsyr2k (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const double alpha, const double *a, const MKL_INT lda, const double *b, const MKL_INT ldb, const double beta, double *c, const MKL_INT ldc);
    
    void cblas_csyr2k (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
    
    void cblas_zsyr2k (const CBLAS_LAYOUT Layout, const CBLAS_UPLO uplo, const
    CBLAS_TRANSPOSE trans, const MKL_INT n, const MKL_INT k, const void *alpha, const void *a, const MKL_INT lda, const void *b, const MKL_INT ldb, const void *beta, void *c, const MKL_INT ldc);
  • 运算

    C:=αAB+αBA+βCC:=αAB+αBA+βC

    C := \\alpha*A*B\’ + \\alpha*B*A\’ + \\beta*C\\\\
    C := \\alpha*A\’*B + \\alpha*B\’*A + \\beta*C

    其中,

    α,β
    \\alpha,\\beta是标量,

    C
    C是nnn*n的对称阵,第一个式子中

    A,B
    A,B是

    nk
    n*k的矩阵,第二个式子中

    A,B
    A,B是

    kn
    k*n的矩阵

cblas_?trmm

  • 作用: 某个输入矩阵为三角阵的时候,计算矩阵-矩阵的乘法

  • 定义函数

    void cblas_strmm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const float alpha, const float *a, const MKL_INT lda, float *b, const MKL_INT ldb);
    
    void cblas_dtrmm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const double alpha, const double *a, const MKL_INT lda, double *b, const MKL_INT ldb);
    
    void cblas_ctrmm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, void *b, const MKL_INT ldb);
    
    void cblas_ztrmm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, void *b, const MKL_INT ldb);
  • 运算

    B:=αop(A)BB:=αBop(A)

    B := \\alpha*op(A)*B\\\\
    B := \\alpha*B*op(A)

    其中,

    α
    \\alpha是标量,

    B
    B是mnm*n的矩阵,

    op(A)=A
    op(A)=A或者

    op(A)=A
    op(A)=A\’或者

    op(A)=conjg(A)
    op(A)=conjg(A\’)

cblas_?trsm

  • 作用: 解三角矩阵方程

  • 定义函数

    void cblas_strsm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const float alpha, const float *a, const MKL_INT lda, float *b, const MKL_INT ldb);
    
    void cblas_dtrsm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const double alpha, const double *a, const MKL_INT lda, double *b, const MKL_INT ldb);
    
    void cblas_ctrsm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, void *b, const MKL_INT ldb);
    
    void cblas_ztrsm (const CBLAS_LAYOUT Layout, const CBLAS_SIDE side, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE transa, const CBLAS_DIAG diag, const MKL_INT m, const MKL_INT n, const void *alpha, const void *a, const MKL_INT lda, void *b, const MKL_INT ldb);
  • 运算

    op(A)X=αBXop(A)=alphaB

    op(A)*X = \\alpha*B\\\\
    X*op(A) = alpha*B

    其中,

    α
    \\alpha是一个标量,

    XB
    X和B是一个

    mn
    m*n的矩阵,

    A
    A是单位或者非单位,上三角或者下三角矩阵,op(A)=Aop(A)=A或者

    op(A)=A
    op(A)=A\’或者

    op(A)=conjg(A)
    op(A)=conjg(A’)。把等式的解矩阵

    X
    X冲写入到BB矩阵中。

后续

研究BLAS Level 1 2 3中各种矩阵相关概念。随后是代码实现

© 版权声明
THE END
喜欢就支持一下吧
点赞907 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容