首页 > 解决方案 > 如何在 Linux 集群上构建 BLAS 和 LAPACK 以在 C++ 中使用?

问题描述

我正在处理一个大的计算问题。为了降低方阵中一组线性方程的计算速度,我使用了lapackblas。为了在我的笔记本电脑(Ubuntu 2020)上获取库,我运行了以下命令

sudo apt-get install libblas-dev liblapack-dev

然后我通过输入以下内容在编译时链接代码

g++ main.cpp -llapack -lblas

但是,我正在处理的集群似乎没有安装这两个库。它在集群上要慢得多,但芯片更好。它运行,所以我认为它lapack安装了库,但没有安装blas. 我想安装两者。

我将如何构建和编译lapack既不访问root 也不访问的blas库?apt-get

这是一个用于测试的简短脚本。

#include <iostream>
#include <vector>

extern "C" void dgesv_( int *n, int *nrhs, double  *a, int *lda, int *ipiv, double *b, int *lbd, int *info  );

int main() {
    int SIZE = 3;
    int nrhs = 1; // one column in b
    int lda = SIZE;
    int ldb = SIZE;
    std::vector<int> i_piv(SIZE, 0);  // pivot column vector
    int info;
    std::vector<double> A(SIZE*SIZE, 0); // sq mat with 0's
    A = {5, 2, 8, 9, 7, 2, 10, 3, 4};
    std::vector<double> b(SIZE);
    b = {22, 13, 17};

    dgesv_( &SIZE, &nrhs, &*A.begin(), &lda, &*i_piv.begin(), &*b.begin(), &ldb, &info );
    return 0;
}

我想用

g++ main.cpp -L/path/to/lapack -L/path/to/blas -llapack -lblas

其中b矩阵被替换为解决方案,并且解决方案是1.71, 1.29, 0.18(这是任意的,所以我没有在代码中提供“print_matrix”函数以减少混乱)。

感谢您的时间。

标签: c++linuxlapackblas

解决方案


布拉斯

  • 下载最新版本的 BLAS

  • 打开终端并转到保存它的目录

tar -xvf blas-3.8.0.tgz  # unzip the blas source files
cd BLAS-3.8.0/ 
make
mv blas_LINUX.a libblas.a
mv *.a path/to/lib  # move the blas lib to the library you will be including at compile

拉包

tar -xvf lapack-3.9.0.tar.gz
cd lapack-3.9.0/
cp make.inc.example make.inc  # use example make as make
make
cp *.a path/to/lib

现在库已经构建并存储在 中path/to/lib,可以编译问题中的简短示例代码。

g++ main.cpp -L/path/to/lib -llapack -lblas -lgfortran  # compiles the code
./a.out  # runs the code

推荐阅读