前言
前面介绍了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
其中op(x)可以是op(x)=x或者op(x)=xT或者op(x)=XHα和β是标量
A,B,C是矩阵: op(A)是一个m∗k的矩阵,op(B)是k∗n的矩阵,C是一个m∗n的矩阵
输入参数
Layout
: 指定矩阵是行优先(CblasRowMajor
)还是列优先(CblasColMajor
)transa
: 指定对矩阵的操作op(A),如果transa=CblasNoTrans
那么op(A)=A;如果transa=CblasTrans
那么op(A)=AT;如果transa=CblasConjTrans
那么op(A)=AHtransb
: 同上m
: 矩阵op(A)和C的行数n
: 矩阵op(B) 和C的列数k
: 矩阵op(A)和op(B)的列数alpha
: 标量a
:
transa=CblasNoTrans | transa=CblasTrans或者transa=CblasConjTrans |
---|---|
Layout=CblasColMajor | 矩阵大小lda∗k |
Layout=CblasRowMajor | 矩阵大小lda∗m |
lda
: 引导维度
transa=CblasNoTrans | transa=CblasTrans或者transa=CblasConjTrans |
---|---|
Layout=CblasColMajor | lda至少为max(1,m) |
Layout=CblasRowMajor | lda至少为max(1,k) |
b
:
transa=CblasNoTrans | transa=CblasTrans或者transa=CblasConjTrans |
---|---|
Layout=CblasColMajor | 矩阵大小ldb∗n |
Layout=CblasRowMajor | 矩阵大小ldb∗k |
ldb
:
transa=CblasNoTrans | transa=CblasTrans或者transa=CblasConjTrans |
---|---|
Layout=CblasColMajor | ldb至少为max(1,k) |
Layout=CblasRowMajor | ldb至少为max(1,n) |
beta
: 标量
c
: 当Layout=CblasColMajor
时候,数组大小为lda∗n;当Layout=CblasRowMajor
时候,数组大小为lda∗m
ldc
:当Layout=CblasColMajor
的时候,ldc
必须至少为max(1,m);当Layout=CblasRowMajor
的时候,ldc
必须至少为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:=α∗A∗B+β∗CC:=α∗B∗A+β∗C
其中,α,β是标量,A是Hermitian矩阵,B,C 是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:=α∗A∗AH+β∗CC:=α∗AH∗A+β∗C
其中,α,β是标量,C是n∗n 的Hermitian矩阵,第一个式子的A是n∗k 矩阵,第二个式子的A是k∗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:=α∗A∗BH+conjg(α)∗B∗AH+β∗CC:=α∗AH∗B+conjg(α)∗BH∗A+β∗C
其中,α,β是标量,C是n∗n 是Hermitian矩阵,第一个式子中A,B是n∗k矩阵,第二个式子中是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:=α∗A∗b+β∗CC:=α∗B∗A+β∗C
其中α,β是标量,A是对称阵,B,C 是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:=α∗A∗A′+β∗CC:=α∗A′∗A+β∗C
其中,α,β是标量,C是n∗n 的对称阵,第一个式子中A是n∗n 的矩阵,第二个式子中A是k∗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:=α∗A∗B′+α∗B∗A′+β∗CC:=α∗A′∗B+α∗B′∗A+β∗C
其中,α,β是标量,C是n∗n 的对称阵,第一个式子中A,B是n∗k的矩阵,第二个式子中A,B是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:=α∗B∗op(A)
其中,α是标量,B是m∗n 的矩阵,op(A)=A或者op(A)=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=α∗BX∗op(A)=alpha∗B
其中,α是一个标量,X和B是一个m∗n的矩阵,A是单位或者非单位,上三角或者下三角矩阵,op(A)=A 或者op(A)=A′或者op(A)=conjg(A′)。把等式的解矩阵X冲写入到B 矩阵中。
后续
研究BLAS Level 1 2 3中各种矩阵相关概念。随后是代码实现