BLAS库学习

本文介绍BLAS不同级别的矩阵运算函数,并通过一个示例程序演示如何使用这些函数进行矩阵乘法计算。比较了直接使用C代码与调用BLAS函数在效率上的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,命名规范:

There are three levels of BLAS operations,

 

Level 1
Vector operations, e.g. y = /alpha x + y  向量操作
Level 2
Matrix-vector operations, e.g. y = /alpha A x + /beta y  矩阵与向量操作
Level 3
Matrix-matrix operations, e.g. C = /alpha A B + C    矩阵与矩阵的操作

Each routine has a name which specifies the operation, the type of matrices involved and their precisions. Some of the most common operations and their names are given below,

 

 

 

DOT
scalar product, x^T y
AXPY
vector sum, /alpha x + y
MV
matrix-vector product, A x
SV
matrix-vector solve, inv(A) x
MM
matrix-matrix product, A B
SM
matrix-matrix solve, inv(A) B

 

The type of matrices are,

 

GE
general   
GB
general band
SY
symmetric
SB
symmetric band
SP
symmetric packed
HE
hermitian
HB
hermitian band
HP
hermitian packed
TR
triangular
TB
triangular band
TP
triangular packed

Each operation is defined for four precisions,

 

S
single real
D
double real
C
single complex
Z
double complex

Thus, for example, the name SGEMM stands for "single-precision general matrix-matrix multiply" and ZGEMM stands for "double-precision complex matrix-matrix multiply".

 

因此,例如,命名为SGEMM的函数意思为“单精度普通矩阵乘法”,ZGEMM为“双精度复数矩阵乘法”。

 

 更多的,可以参考:http://www.netlib.org/blas/blasqr.pdf

 

下面这个例子是在csdn论坛上看到的,简单地改一下,可用GotoBlas2调用成功。

 

程序说明:下面的matrix.c 文件分别调用 C 代码, BLAS Level 1 函数 (ddot), BLAS Level 2 函数(dgemv) 与 BLAS Level 3的函数(DGEMM)完成矩阵计算: Yours_multiply 是 C 源代码,它直接依赖编译器生成优化代码。Ddot_Multiply,Dgemv_multiply使用Gotoblas2调用实现部分矩阵运算。Dgemm_multiply 直接调用GotoBlas2 的矩阵计算函数。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

05-01
### 关于 BLAS 的使用说明 BLAS(Basic Linear Algebra Subprograms)是一个用于执行基本线性代数运算的标准接口集合。它被广泛应用于科学计算领域,尤其是在数值分析和高性能计算中。 #### BLAS 的层次结构 BLAS 被分为三个主要级别,分别对应不同复杂度的操作[^3]: - **Level 1**: 向量操作,例如 `y = α * x + y`。 - **Level 2**: 矩阵与向量操作,例如 `y = α * A * x + β * y`。 - **Level 3**: 矩阵与矩阵操作,例如 `C = α * A * B + C`。 这些级别的划分使得开发者可以根据具体需求选择合适的函数来优化性能。 --- #### BLAS 的下载与安装 BLAS 并不是一个单一的实现,而是一组标准接口。因此,实际应用中通常会选用某个具体的实现版本。以下是几个常见的 BLAS 实现及其获取方式: 1. **Netlib BLAS** Netlib 提供了一个基础版的 BLAS 实现,适用于学习或测试目的。可以从其官方网站下载源码并编译: ```bash wget http://www.netlib.org/blas/blas.tgz tar zxvf blas.tgz make ``` 2. **OpenBLAS** OpenBLAS 是一个高度优化的开源 BLAS 实现,支持多种硬件架构。可以通过包管理器安装或手动编译: - Ubuntu/Linux: ```bash sudo apt-get install libopenblas-dev ``` - 手动编译: ```bash git clone https://github.com/xianyi/OpenBLAS.git cd OpenBLAS make && make install ``` 3. **ATLAS (Automatically Tuned Linear Algebra Software)** ATLAS 自动调整以适应特定硬件环境下的最佳性能。可以尝试通过配置脚本来检测加速: ```bash ./configure --prefix=/path/to/install --with-blas=libatlas.so make && make install ``` 4. **Intel MKL (Math Kernel Library)** Intel MKL 提供了针对英特尔处理器的高度优化 BLAS 实现。可通过 Intel 官方网站免费注册获得许可证,并按照文档指导完成安装。 --- #### 配置 BLAS 加速 某些情况下可能需要显式指定使用的 BLAS 。例如,在构建软件时可以通过命令行选项传递路径信息[^2]: ```bash ./configure --with-blas=/path/to/libblas.so ``` 如果系统自动探测失败,则需确认环境变量设置正确无误。对于 Linux 用户来说,这通常涉及修改 `LD_LIBRARY_PATH` 和链接动态共享对象文件的位置。 --- #### 示例代码:调用 BLAS 进行简单矩阵乘法 以下展示如何利用 CBLAS 接口完成两个矩阵相乘的任务[^1]: ```c #include <stdio.h> #include <cblas.h> int main() { int m = 2, n = 2, k = 2; double alpha = 1.0, beta = 0.0; // 初始化输入矩阵 A[m*k], B[k*n] double A[] = {1, 2, 3, 4}; double B[] = {5, 6, 7, 8}; // 输出矩阵 C[m*n] double C[4]; cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, n, k, alpha, A, k, B, n, beta, C, n); printf("Resultant matrix:\n"); for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ printf("%f ", C[i*n+j]); } printf("\n"); } return 0; } ``` 此程序展示了 Level 3 中典型的 GEMM 操作 (`General Matrix Multiply`)。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值