Overview
Some R modules on ARC HPC clusters may be built with FlexiBLAS. FlexiBLAS is a BLAS and LAPACK wrapper library that allows the active linear algebra backend to be changed without rebuilding R.
This means R can be linked against FlexiBLAS while FlexiBLAS routes BLAS and LAPACK calls to a selected backend, such as OpenBLAS, Intel MKL, BLIS, or the Netlib reference implementation.
For most R users, this behaves similarly to using R with the selected backend directly. For example, if FlexiBLAS is configured to use OpenBLAS, many matrix operations in R will ultimately run through OpenBLAS.
Loading R
To load R, use the module command:
$ module load RTo load a specific version:
$ module load R/4.6.0To check which modules are currently loaded:
$ module listTo see what the R module sets in your environment:
$ module show RChecking the R Version
You can check the R version from the command line:
$ R --versionInside R, you can also run:
R.version.stringChecking Whether R Uses FlexiBLAS
To check whether the R executable is linked to FlexiBLAS, run:
$ Rscript -e 'sessionInfo()' | grep BLASIf the R installation is using flexiblas, the output will return
BLAS/LAPACK: FlexiBLAS $ACTIVE_BACKEND; LAPACK version x.y.zChecking the Active FlexiBLAS Backend
To list the FlexiBLAS backends available in your environment:
$ flexiblas listExample output may include entries such as:
NETLIB
MKLSERIAL
OPENBLASOPENMP
NVBLAS13
NVBLAS12These names are backend implementations, not mathematical interfaces. For example, OPENBLAS is a backend, while BLAS and LAPACK are interfaces.
To check the current backend:
$ flexiblas currentUsing OpenBLAS Through FlexiBLAS
If the R module is built with FlexiBLAS and FlexiBLAS is configured to use OpenBLAS, the call path is approximately:
R
-> FlexiBLAS
-> OpenBLASThis is similar, but not identical, to building R directly against OpenBLAS:
R
-> OpenBLASFor large matrix operations, the performance is usually expected to be similar because the computational work is performed by OpenBLAS. FlexiBLAS adds a wrapper layer, but that overhead is normally small compared with the cost of large BLAS or LAPACK operations.
| Configuration | Description |
|---|---|
| R built directly with OpenBLAS | R links directly to OpenBLAS. The backend is fixed unless R is rebuilt or relinked. |
| R built with FlexiBLAS using OpenBLAS | R links to FlexiBLAS. FlexiBLAS then routes calls to OpenBLAS. The backend can be changed more easily. |
Verifying Linear Algebra Behavior in R
You can run a small matrix multiplication test in R:
$ R
n <- 4000
A <- matrix(rnorm(n * n), n, n)
B <- matrix(rnorm(n * n), n, n)
system.time(C <- A %*% B)You can also test a LAPACK-style operation:
n <- 3000
A <- crossprod(matrix(rnorm(n * n), n, n))
system.time(chol(A))These examples are intended only as simple checks. Actual performance will depend on matrix size, node type, CPU architecture, thread settings, and the selected backend.
Thread Settings
Many BLAS backends use multithreading. On shared HPC systems, it is important to match the number of BLAS threads to the number of CPU cores requested from Slurm.
Our copies of R that are linked to FlexiBLAS also include the flexiblas package, which can be used to manage this at runtime. Simply call flexiblas::set_num_threads(Sys.getenv('SLURM_CPUS_PER_TASK')).
Example R Script
Create a file named test_linear_algebra.R:
cat("R version:\n")
print(R.version.string)
cat("\nBLAS library used by R:\n")
print(extSoftVersion()["BLAS"])
cat("\nLAPACK library used by R:\n")
print(extSoftVersion()["LAPACK"])
flexiblas::flexiblas_load_backend("OPENBLASOPENMP")
flexiblas::set_num_threads(Sys.getenv('SLURM_CPUS_PER_TASK'))
cat("\nBLAS library used by R:\n")
print(extSoftVersion()["BLAS"])
cat("\nLAPACK library used by R:\n")
print(extSoftVersion()["LAPACK"])
n <- 3000
A <- matrix(rnorm(n * n), n, n)
B <- matrix(rnorm(n * n), n, n)
cat("\nMatrix multiplication timing:\n")
print(system.time(C <- A %*% B))
cat("\nCholesky timing:\n")
M <- crossprod(A)
print(system.time(chol(M)))Run it with:
$ Rscript test_linear_algebra.RSpecial Considerations
FlexiBLAS provides flexibility, but it also adds another layer to document when reporting results or requesting help.
When reporting a performance issue, include:
$ module list
$ flexiblas list
$ flexiblas current
$ Rscript -e "sessionInfo()"Also include the relevant thread settings:
$ Rscript -e "flexiblas::flexiblas_get_num_threads()If you are comparing performance between different R modules or different clusters, make sure the same backend and thread settings are being used.
Summary
R built with FlexiBLAS and configured to use OpenBLAS is usually similar in performance to R built directly against OpenBLAS for large matrix operations. The main advantage of FlexiBLAS is that it allows the active BLAS/LAPACK backend to be changed without rebuilding R.
For documentation and reproducibility, describe the configuration as:
R is linked against FlexiBLAS. The default backend is OpenBLAS.rather than:
R is built directly against OpenBLAS.