kml_sparse_?csrmm
矩阵与矩阵计算,其中一个稀疏矩阵采用CSR格式存储,具体执行操作如下:
- C = alpha * A*B + beta * C
- C = alpha * AT * B + beta * C
- C = alpha * AH * B + beta * C
其中,B和C为稠密矩阵,A是采用CSR格式存储的mxk的稀疏矩阵。
layout与sparse matrix indexing的关系如表4-11所示。
Sparse matrix indexing |
Dense matrix layout |
---|---|
KML_SPARSE_INDEX_BASE_ZERO |
KML_SPARSE_LAYOUT_ROW_MAJOR |
KML_SPARSE_INDEX_BASE_ONE |
KML_SPARSE_LAYOUT_COLUMN_MAJOR |
接口定义
C interface:
kml_sparse_status_t kml_sparse_scsrmm(const kml_sparse_operation_t opt, const KML_INT m, const KML_INT n, const KML_INT k, const float alpha, const char *matdescra, const float *val, const KML_INT *indx, const KML_INT *pntrb, const KML_INT *pntre, const float *b, const KML_INT ldb, const float beta , float *c , const KML_INT ldc);
kml_sparse_status_t kml_sparse_dcsrmm(const kml_sparse_operation_t opt, const KML_INT m, const KML_INT n, const KML_INT k, const double alpha, const char *matdescra, const double *val, const KML_INT *indx, const KML_INT *pntrb, const KML_INT *pntre, const double *b, const KML_INT ldb, const double beta , double *c , const KML_INT ldc);
kml_sparse_status_t kml_sparse_ccsrmm(const kml_sparse_operation_t opt, const KML_INT m, const KML_INT n, const KML_INT k, const KML_Complex8 alpha, const char *matdescra, const KML_Complex8 *val, const KML_INT *indx, const KML_INT *pntrb, const KML_INT *pntre, const KML_Complex8 *b, const KML_INT ldb, const KML_Complex8 beta , KML_Complex8 *c , const KML_INT ldc);
kml_sparse_status_t kml_sparse_zcsrmm(const kml_sparse_operation_t opt, const KML_INT m, const KML_INT n, const KML_INT k, const KML_Complex16 alpha, const char *matdescra, const KML_Complex16 *val, const KML_INT *indx, const KML_INT *pntrb, const KML_INT *pntre, const KML_Complex16 *b, const KML_INT ldb, const KML_Complex16 beta , KML_Complex16 *c , const KML_INT ldc);
Fortran interface:
RES = KML_SPARSE_SCSRMM(OPT, M, N K, ALPHA, MATDESCRA, VAL, INDX, PNTRB, PNTRE, B, LDB, BETA, C, LDC);
RES = KML_SPARSE_DCSRMM(OPT, M, N K, ALPHA, MATDESCRA, VAL, INDX, PNTRB, PNTRE, B, LDB, BETA, C, LDC);
RES = KML_SPARSE_CCSRMM(OPT, M, N K, ALPHA, MATDESCRA, VAL, INDX, PNTRB, PNTRE, B, LDB, BETA, C, LDC);
RES = KML_SPARSE_ZCSRMM(OPT, M, N K, ALPHA, MATDESCRA, VAL, INDX, PNTRB, PNTRE, B, LDB, BETA, C, LDC);
参数
参数名 |
类型 |
描述 |
输入/输出 |
---|---|---|---|
opt |
枚举类型kml_sparse_operation_t |
表示是否转置:
|
输入 |
m |
整型数 |
矩阵A的行数,取值范围为[1, MAX_KML_INT]。 |
输入 |
n |
整型数 |
矩阵C的列数,取值范围为[1, MAX_KML_INT]。 |
输入 |
k |
整型数 |
矩阵A的列数,取值范围为[1, MAX_KML_INT]。 |
输入 |
alpha |
|
标量alpha。 |
输入 |
matdescra |
char指针 |
矩阵的操作属性,具体参考matdescra说明。 |
输入 |
val |
|
CSR格式中values数组,存储矩阵A的非零元素,长度为pntre[m-1] - pntrb[0]。 |
输入 |
indx |
整型数组 |
CSR格式中columns数组,用于表示矩阵A中非零元素所在的列索引。 |
输入 |
pntrb |
整型数组 |
长度为m的数组,包含矩阵A的行索引,pntrb[i] - pntrb[0]表示第i行第一个非零元素在val和indx数组内的下标。 |
输入 |
pntre |
整型数组 |
长度为m的数组,包含矩阵A的行索引,pntre[i] - pntrb[0]-1表示第i行最后一个非零元素在val和indx数组内的下标。 |
输入 |
b |
|
矩阵B的value数组。 |
输入 |
ldb |
整型数 |
|
输入 |
beta |
|
标量beta。 |
输入 |
c |
|
矩阵C的value数组。 |
输入/输出 |
ldc |
整型数 |
|
输入 |
B矩阵参数约束如表2所示。
opt |
B矩阵规模 |
B矩阵数据排布 |
参数范围 |
---|---|---|---|
op(A) = A |
k * n |
行主序 |
k * ldbMAX_KML_INT |
op(A) = A |
k * n |
列主序 |
ldb * nMAX_KML_INT |
op(A) = AT或AH |
m * n |
行主序 |
m * ldbMAX_KML_INT |
op(A) = AT或AH |
m * n |
列主序 |
ldb * nMAX_KML_INT |
C矩阵参数约束如表3所示。
opt |
C矩阵规模 |
C矩阵数据排布 |
参数范围 |
---|---|---|---|
op(A) = A |
m * n |
行主序 |
m * ldcMAX_KML_INT |
op(A) = A |
m * n |
列主序 |
ldc * nMAX_KML_INT |
op(A) = AT或AH |
k * n |
行主序 |
k * ldcMAX_KML_INT |
op(A) = AT或AH |
k * n |
列主序 |
ldc * nMAX_KML_INT |
函数内部不对参数做完整校验,接口调用者需保证pntrb和pntre内部元素不超出矩阵最大索引值。
返回值
函数执行状态,枚举类型kml_sparse_status_t。
依赖
C: "kspblas.h"
Fortran: "kspblas.f03"
示例
C interface:
kml_sparse_operation_t opt = KML_SPARSE_OPERATION_NON_TRANSPOSE; KML_INT m = 3; KML_INT n = 3; KML_INT k = 3; float alpha = 1.0; float beta = 1.0; char *matdescra = "G00C"; //一般矩阵,基0索引 float val[4] = {9, 8, 5, 2}; KML_INT indx[4] = {2, 1, 2, 2}; KML_INT pntrb[3] = {0, 2, 3}; KML_INT pntre[3] = {2, 3, 4}; float b[9] = {1, 5, 7, 4, 7, 7, 3, 3, 7}; float c[9] = {0, 8, 8, 0, 2, 8, 3, 5, 6}; KML_INT ldb = 3; KML_INT ldc = 3; kml_sparse_status_t status = kml_sparse_scsrmm(opt, m, n, k, alpha, matdescra, val, indx, pntrb, pntre, b, ldb, beta, c, ldc); /* * Output c: * 59.00 91.00 127.00 15.00 17.00 43.00 9.00 11.00 20.00 * */
Fortran interface:
INTEGER(C_INT) :: OPT = KML_SPARSE_OPERATION_NON_TRANSPOSE INTEGER(C_INT) :: M = 3 INTEGER(C_INT) :: N = 3 INTEGER(C_INT) :: K = 3 REAL(C_FLOAT) :: ALPHA = 1.0 REAL(C_FLOAT) :: BETA = 1.0 INTEGER(C_INT) :: LDB = 3 INTEGER(C_INT) :: LDC = 3 INTEGER(C_INT) :: STATUS CHARACTER(KIND=C_CHAR, LEN=4) :: MATDESCRA = "G00C" !一般矩阵,基0索引 REAL(C_FLOAT) :: VAL(4), B(9), C(9) INTEGER(C_INT) :: INDX(4), PNTRB(4), PNTRE(3) DATA VAL/9, 8, 5, 2/ DATA B /1, 5, 7, 4, 7, 7, 3, 3, 7/ DATA C /0, 8, 8, 0, 2, 8, 3, 5, 6/ DATA INDX/2, 1, 2, 2/ DATA PNTRB/0, 2, 3/ DATA PNTRE/2, 3, 4/ STATUS = KML_SPARSE_SCSRMM(OPT, M, N, K, ALPHA, MATDESCRA, VAL, INDX, PNTRB, PNTRE, B, LDB, BETA, C, LDC) ! ! OUTPUT C: ! 59.00 91.00 127.00 15.00 17.00 43.00 9.00 11.00 20.00 !