1. 构建openblas lib
git clone git@github.com:OpenMathLib/OpenBLAS.git
cd OpenBLAS/
如果要安装在自定义文件夹中,可以修改 PREFIX 的定义:
将
PREFIX = /opt/OpenBLAS
修改成
PREFIX = ../local/
然后构建:
make -j
make install
如果要构建 debug 版本的openblas,则可以在 Makefile.rule中,修改如下 大约在244行:
将
# Build Debug version
# DEBUG = 1
修改为
# Build Debug version
DEBUG = 1
然后在执行构建指令:
make -j
make install
2. 调试openblas 示例
2.1 示例1,LU分解
//hello_LAPACK_sgetrf.c//#include <lapack.h>
#include <f77blas.h>
#include <stdio.h>int main() {int n = 3; // Dimension of the matrixfloat A[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 10.0}; // Input matrixint lda = n; // Leading dimension of Aint ipiv[n]; // Array to store pivot indicesint info; // Output variable for error info// Call the LAPACK sgetrf function for LU decompositionsgetrf_(&n, &n, A, &lda, ipiv, &info);if (info == 0) {printf("LU decomposition successful!\n");// Print the decomposed matrix Aprintf("Decomposed matrix A:\n");for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {printf("%f ", A[i + j*n]);}printf("\n");}printf("ipiv=\n");for(int i=0; i<n; i++)printf("%d ", ipiv[i]);printf("\n");} else {printf("LU decomposition failed!\n");}return 0;
}
2.2 示例2 QR分解
hello_qrf.c
#include <stdio.h>
#include <lapack.h>int min(int m, int n){ return m<n? m:n;}int main() {int m = 3; // Number of rows in the matrixint n = 2; // Number of columns in the matrixdouble A[] = {1.0, 4.0, 2.0, 5.0, 3.0, 6.0}; // Input matrixint lda = m; // Leading dimension of Adouble tau[min(m,n)]; // Array to store elementary reflectorsint info; // Output variable for error infoint lwork = -1;double* work = (double*)malloc(8);// Call the LAPACK dgeqrf function for QR decompositiondgeqrf_(&m, &n, A, &lda, tau, work, &lwork, &info);lwork = (int)work[0];printf("lwork=%d\n", lwork);work = (double*)malloc(lwork*sizeof(double)); // Workspace array// Call the LAPACK dgeqrf function again with correct workspacedgeqrf_(&m, &n, A, &lda, tau, work, &lwork, &info);if (info == 0) {printf("QR decomposition successful!\n");// Print the decomposed matrix Aprintf("Decomposed matrix A:\n");for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {printf("%f ", A[i + j*m]);}printf("\n");}} else {printf("QR decomposition failed!\n");}return 0;
}
Makefile:
INC = -I ../local/include
#LD_FLAGS = -L../local/lib -lopenblas
#LD_FLAGS = /home/hipper/ex_openblas/tmp_/local/lib/libopenblas_skylakexp-r0.3.26.dev.so
EXE := hello_LAPACK_sgetrf hello_qrfall: $(EXE)hello_qrf: hello_qrf.o
hello_LAPACK_sgetrf: hello_LAPACK_sgetrf.o%.o: %.cgcc -g $< -c -o $@ $(INC)%: %.o /home/hipper/ex_openblas/tmp_/local/lib/libopenblas_skylakexp-r0.3.26.dev.agcc -g $^ -o $@ $(LD_FLAGS) -lm -lgfortran#$(LD_FLAGS).PHONY: clean
clean:-rm -rf $(EXE) *.o
调试效果:
这里使用的是静态库
也可以使用动态库