The Lurking Dangers of the R Version Update
Ah, the joys of updating R to a new version (currently 4.6). If you do so, proceed at your own peril.1 Losing the reference to all installed packages is well-known. But one thing that also broke for me was linear algebra speed.
The culprit is the BLAS, the Basic Linear Algebra Subprograms library. It’s the code that actually does the matrix multiplies underneath almost everything numerical in base R. The version R ships with on macOS is the reference implementation, an unoptimized version of the library. Apple bundles a much faster one called vecLib as part of its Accelerate framework, and R can use it, but it’s not the default.
CRAN picks correctness over speed
The CRAN binary installs both libraries and a symbolic link that decides which one R loads. From the R for macOS FAQ, section 10.5:
vecLib is a part of Apple’s Accelerate framework which provides an optimized BLAS implementation for Mac hardware. Although fast, it is not under our control and may possibly deliver inaccurate results. […] Currently the default is to use the R BLAS: this is recommended for precision.
Fair enough. The catch is that every update lays down a fresh link pointing at the slow library. If you switched to vecLib last year, updating to R 4.6 quietly reset you. It seems that is what happened to me.
Check what you have
The reliable check is sessionInfo(). Look at the BLAS: line.
sessionInfo()
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/.../lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/.../lib/libRlapack.dyliblibRblas.0.dylib is the reference BLAS — note the .0.. If you see that, you’re on the slow path.
Switch and unlock more speed
One command in the terminal updates the link to vecLib. You may need sudo because the file lives in a system framework.
ln -sf /Library/Frameworks/R.framework/Resources/lib/libRblas.vecLib.dylib \
/Library/Frameworks/R.framework/Resources/lib/libRblas.dylibStart a fresh R session (the link is read at startup) and test it.
n <- 3000
A <- matrix(rnorm(n * n), n)
system.time(A %*% A)On my machine with the R default reference BLAS, this takes about seven seconds:
user system elapsed
7.413 0.061 7.500
After the switch, the same operation takes 0.15 seconds, which is roughly fifty times faster.
user system elapsed
0.154 0.001 0.155
To go back, point the link at the reference library again:
ln -sf /Library/Frameworks/R.framework/Resources/lib/libRblas.0.dylib \
/Library/Frameworks/R.framework/Resources/lib/libRblas.dylibOther systems
This post covers macOS with the CRAN binary, but the same idea applies elsewhere. On Linux, you can install OpenBLAS to swap the system BLAS. On any platform, you can also compile R against OpenBLAS or Intel MKL directly. The R Installation and Administration manual covers the options.
One line and a restart buys you most of the speed. Note that libraries like fixest and Stan bring their own linear algebra and never touch R’s BLAS.
Footnotes
For example, you can no longer install the obsolete
qspackage. Good luck if you wanted to migrate fromqstoqs2. The solution, for the one person who may have the same problem, is to install the R Installation Managerrig.↩︎
