Search code examples
c++linuxlapackblas

How to build BLAS and LAPACK for use in C++ on Linux cluster?


I have a large computational problem I am working on. To decrease the computation speed of a set of linear equations in a square matrix, I have made use of lapack and blas. To get the libraries on my laptop (Ubuntu 2020) I ran the following command

sudo apt-get install libblas-dev liblapack-dev

Then I linked the code at compile by entering the following

g++ main.cpp -llapack -lblas

However, the cluster I am working on does not seem to have both of the libraries installed. It is much slower on the cluster, but yet a better chip. It runs, so I think it has the lapack libraries installed, but not blas. I'd like to install both.

How would I build and compile the lapack and blas libraries with neither access to root nor apt-get?

Here is a short script for testing.

#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;
}

I'd like to build this with

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

where the b matrix is replaced with the solution, and the solution is 1.71, 1.29, 0.18 (which is sort of arbitrary so I'm not providing the "print_matrix" function in the code to reduce clutter).

Thank you for your time.


Solution

  • BLAS

    • Download the latest version of BLAS

    • Open a terminal and go to the directory where you have it saved

    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
    

    LAPACK

    • Download the latest version of LAPACK
    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
    

    Now that the libraries have been built, and are stored in path/to/lib, the short example code in the question can be compiled.

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