openBLAS中利用cblas_dgemm进行矩阵乘法运算举例
时间: 2023-06-30 21:25:38 浏览: 145
假设我们有两个3x3的矩阵A和B,需要计算它们的乘积C。矩阵A和B可以用数组表示,代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include "cblas.h"
int main()
{
double A[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
double B[9] = {9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0};
double C[9] = {0};
int M = 3, N = 3, K = 3;
double alpha = 1.0, beta = 0.0;
int lda = 3, ldb = 3, ldc = 3;
enum CBLAS_ORDER Order = CblasRowMajor;
enum CBLAS_TRANSPOSE TransA = CblasNoTrans;
enum CBLAS_TRANSPOSE TransB = CblasNoTrans;
cblas_dgemm(Order, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
printf("Matrix A:\n");
for (int i = 0; i < 9; ++i)
{
printf("%.1lf ", A[i]);
if ((i + 1) % 3 == 0)
printf("\n");
}
printf("\nMatrix B:\n");
for (int i = 0; i < 9; ++i)
{
printf("%.1lf ", B[i]);
if ((i + 1) % 3 == 0)
printf("\n");
}
printf("\nMatrix C:\n");
for (int i = 0; i < 9; ++i)
{
printf("%.1lf ", C[i]);
if ((i + 1) % 3 == 0)
printf("\n");
}
return 0;
}
```
运行结果如下:
```
Matrix A:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
Matrix B:
9.0 8.0 7.0
6.0 5.0 4.0
3.0 2.0 1.0
Matrix C:
30.0 20.0 10.0
84.0 56.0 28.0
138.0 92.0 46.0
```
可以看到,乘积C的计算结果正确。
阅读全文