此示例是利用Intel 的MKL庫函式計算矩陣的乘法,目標為:\(C=\alpha*A*B+\beta*C\),由函式cblas_dgemm實現;
其中\(A\)為\(m\times k\)維矩陣,\(B\)為\(k\times n\)維矩陣,\(C\)為\(m\times n\)維矩陣。
1 cblas_dgemm引數詳解
fun cblas_dgemm(Layout, //指定行優先(CblasRowMajor,C)或列優先(CblasColMajor,Fortran)資料排序
TransA, //指定是否轉置矩陣A
TransB, //指定是否轉置矩陣B
M, //矩陣A和C的行數
N, //矩陣B和C的列數
K, //矩陣A的列,B的行
alpha, //矩陣A和B乘積的比例因子
A, //A矩陣
lda, //矩陣A的第一維的大小
B, //B矩陣
ldb, //矩陣B的第一維的大小
beta, //矩陣C的比例因子
C, //(input/output) 矩陣C
ldc //矩陣C的第一維的大小
)
cblas_dgemm矩陣乘法預設的演算法就是\(C=\alpha*A*B+\beta*C\),若只需矩陣\(A\)與\(B\)的乘積,設定\(\alpha=1,\beta=0\)即可。
2 定義待處理矩陣
#include <stdio.h>
#include <stdlib.h>
#include "mkl.h" // 呼叫mkl標頭檔案
#define min(x,y) (((x) < (y)) ? (x) : (y))
double* A, * B, * C; //宣告三個矩陣變數,並分配記憶體
int m, n, k, i, j; //宣告矩陣的維度,其中
double alpha, beta;
m = 2000, k = 200, n = 1000;
alpha = 1.0; beta = 0.0;
A = (double*)mkl_malloc(m * k * sizeof(double), 64); //按照矩陣維度分配記憶體
B = (double*)mkl_malloc(k * n * sizeof(double), 64); //mkl_malloc用法與malloc相似,64表示64位
C = (double*)mkl_malloc(m * n * sizeof(double), 64);
if (A == NULL || B == NULL || C == NULL) { //判空
mkl_free(A);
mkl_free(B);
mkl_free(C);
return 1;
}
for (i = 0; i < (m * k); i++) { //賦值
A[i] = (double)(i + 1);
}
for (i = 0; i < (k * n); i++) {
B[i] = (double)(-i - 1);
}
for (i = 0; i < (m * n); i++) {
C[i] = 0.0;
}
其中\(A\)和\(B\)矩陣設定為:
\[\begin{array}{l}
A = \left[ {\begin{array}{*{20}{c}}
{1.0}&{2.0}& \cdots &{1000.0}\\
{1001.0}&{1002.0}& \cdots &{2000.0}\\
\vdots & \vdots & \ddots & \cdots \\
{999001.0}&{999002.0}& \cdots &{1000000.0}
\end{array}} \right] \space
B = \left[ {\begin{array}{*{20}{c}}
{-1.0}&{-2.0}& \cdots &{-1000.0}\\
{-1001.0}&{-1002.0}& \cdots &{-2000.0}\\
\vdots & \vdots & \ddots & \cdots \\
{-999001.0}&{-999002.0}& \cdots &{-1000000.0}
\end{array}} \right]
\end{array}
\]
\(C\)矩陣為全0。
3 執行矩陣乘法
回到例子中,對照上面的引數,將C矩陣用A與B的矩陣乘法表示:
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
m, n, k, alpha, A, k, B, n, beta, C, n);
//在執行完成後,釋放記憶體
mkl_free(A);
mkl_free(B);
mkl_free(C);
執行後的得到結果如下:
完整程式碼
#include <stdio.h>
#include <stdlib.h>
#include "mkl.h"
#define min(x,y) (((x) < (y)) ? (x) : (y))
int main()
{
double* A, * B, * C;
int m, n, k, i, j;
double alpha, beta;
m = 2000, k = 200, n = 1000;
alpha = 1.0; beta = 0.0;
A = (double*)mkl_malloc(m * k * sizeof(double), 64);
B = (double*)mkl_malloc(k * n * sizeof(double), 64);
C = (double*)mkl_malloc(m * n * sizeof(double), 64);
if (A == NULL || B == NULL || C == NULL) {
mkl_free(A);
mkl_free(B);
mkl_free(C);
return 1;
}
for (i = 0; i < (m * k); i++) {
A[i] = (double)(i + 1);
}
for (i = 0; i < (k * n); i++) {
B[i] = (double)(-i - 1);
}
for (i = 0; i < (m * n); i++) {
C[i] = 0.0;
}
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
m, n, k, alpha, A, k, B, n, beta, C, n);
for (i = 0; i < min(m, 6); i++) {
for (j = 0; j < min(k, 6); j++) {
printf("%12.0f", A[j + i * k]);
}
printf("\n");
}
for (i = 0; i < min(k, 6); i++) {
for (j = 0; j < min(n, 6); j++) {
printf("%12.0f", B[j + i * n]);
}
printf("\n");
}
for (i = 0; i < min(m, 6); i++) {
for (j = 0; j < min(n, 6); j++) {
printf("%12.5G", C[j + i * n]);
}
printf("\n");
}
mkl_free(A);
mkl_free(B);
mkl_free(C);
return 0;
}