4

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.

GeneralCode
  • 794
  • 1
  • 11
  • 26

2 Answers2

8

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
GeneralCode
  • 794
  • 1
  • 11
  • 26
  • 1
    NOTE: Using OpenBLAS, rather than Netlib's BLAS will result in significantly improved performance. – GeneralCode Oct 07 '22 at 15:36
  • If your final target is a shared library, make sure to include `-fPIC` flag in make.inc `FFLAGS` and `FFLAGS_NOOPT`. The make process for lapack can take a while. – John Jiang Feb 25 '23 at 08:41
2

You can clone official repo to your cluster via git:

git clone https://github.com/xianyi/OpenBLAS.git

now cd to clonned repo, compile and install it:

cd OpenBLAS
make 
make install PREFIX=~/blas

Now you can link BLAS this way:

g++ main.cpp -L~/blas/lib -lblas

More info about compiling BLAS can be found in official repo.
Same for LAPACK.

According to README.md from repo, LAPACK can be compiled with CMAKE:

cd lapack
mkdir build
cd build
cmake -DCMAKE_INSTALL_LIBDIR=/home/user/lapack ..
cmake --build . -j --target install
Aleksey
  • 775
  • 5
  • 14