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 matrix
float A[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 10.0}; // Input matrix
int lda = n; // Leading dimension of A
int ipiv[n]; // Array to store pivot indices
int info; // Output variable for error info
// Call the LAPACK sgetrf function for LU decomposition
sgetrf_(&n, &n, A, &lda, ipiv, &info);
if (info == 0) {
printf("LU decomposition successful!\n");
// Print the decomposed matrix A
printf("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 matrix
int n = 2; // Number of columns in the matrix
double A[] = {1.0, 4.0, 2.0, 5.0, 3.0, 6.0}; // Input matrix
int lda = m; // Leading dimension of A
double tau[min(m,n)]; // Array to store elementary reflectors
int info; // Output variable for error info
int lwork = -1;
double* work = (double*)malloc(8);
// Call the LAPACK dgeqrf function for QR decomposition
dgeqrf_(&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 workspace
dgeqrf_(&m, &n, A, &lda, tau, work, &lwork, &info);
if (info == 0) {
printf("QR decomposition successful!\n");
// Print the decomposed matrix A
printf("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_qrf
all: $(EXE)
hello_qrf: hello_qrf.o
hello_LAPACK_sgetrf: hello_LAPACK_sgetrf.o
%.o: %.c
gcc -g $< -c -o $@ $(INC)
%: %.o /home/hipper/ex_openblas/tmp_/local/lib/libopenblas_skylakexp-r0.3.26.dev.a
gcc -g $^ -o $@ $(LD_FLAGS) -lm -lgfortran
#$(LD_FLAGS)
.PHONY: clean
clean:
-rm -rf $(EXE) *.o
调试效果:
这里使用的是静态库
也可以使用动态库