Skip to content

OpenBLAS

OpenBLAS is an optimized open-source BLAS and LAPACK library, providing routines for linear algebra operations (matrix multiplication, factorizations, solvers, etc.).

Loading the module

module load OpenBLAS/0.3.23-GCC-12.3.0

To see all installed versions across clusters, check the software catalog or run module spider OpenBLAS.

Info

OpenBLAS is loaded automatically as part of the foss toolchain. If you already loaded foss-2023a or similar, OpenBLAS is already available. Check with module list.

Compiling against OpenBLAS

# C
gcc -o example example.c -lopenblas

# Fortran
gfortran -o example example.f90 -lopenblas
Example: DGEMM matrix multiplication
#include <stdio.h>
#include <cblas.h>

int main() {
    int m = 2, n = 3, k = 2;
    double A[4] = {1.0, 2.0, 3.0, 4.0};
    double B[6] = {5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
    double C[6] = {0};

    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
                m, n, k, 1.0, A, k, B, n, 0.0, C, n);

    printf("Result:\n");
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            printf(" %6.2f", C[i * n + j]);
        printf("\n");
    }
    return 0;
}

Batch job example

openblas.slurm
#!/bin/bash
#SBATCH --qos=regular
#SBATCH --job-name=openblas
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=4G
#SBATCH --time=00:30:00
#SBATCH --output=%x.%j.out
#SBATCH --error=%x.%j.err

module load OpenBLAS/0.3.23-GCC-12.3.0

export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK}

./example

Thread control

OpenBLAS uses threading by default. Set OMP_NUM_THREADS (or OPENBLAS_NUM_THREADS) to match --cpus-per-task. To run single-threaded, set it to 1.

See also