If you work on a Windows computer without admin privileges to modify system files (e.g., many government employees), you can likely still install R in your user folder and replace the default BLAS library there.* Instructions:
- Download the latest R installer.
- Run the installer and select a user folder for installation when prompted, e.g.
C:\Users\USERNAME\SUBFOLDER. - In RStudio go to 'Tools' > 'Global Options' > 'General' > 'R version' to change the R installation being used to the one you just installed in your user folder.
- Download OpenBLAS following these instructions. A summarized version of this and subsequent steps follows.
- Extract
libopenblas.dllfrom the OpenBLAS zip file. - Rename or move
Rblas.dllandRlapack.dllfrom theR\R-4.X.X\bin\x64subfolder (whereX.Xrepresents version numbers). Make sure you are doing so from the R installation in your user folder that you just made. - Paste two copies of
libopenblas.dllinto the same folder as above and rename themRblas.dllandRlapack.dll. - Restart R/RStudio.
More detailed instructions from step 3 onwards are available here.
Test to make sure OpenBLAS is being used:
m <- 1e4; n <- 1e3; k <- 3e2
X <- matrix(rnorm(m*k), nrow=m); Y <- matrix(rnorm(n*k), ncol=n)
system.time(X %*% Y)The result should look something like:
## user system elapsed
## 0.169 0.011 0.165 The standard BLAS shipped with R will likely take 1-2 seconds for the above.
*Thanks to Paul Regular and Semra Yalcin for figuring this out and to Seb Pardo for drafting the step-by-step instructions.
You may find that openBLAS uses all available cores, which may not be computationally efficient with TMB. If so, you can specify BLAS should only use 1 core with the RhpcBLASctl package:
RhpcBLASctl::blas_set_num_threads(1)
RhpcBLASctl::omp_set_num_threads(1)You can either set that at the beginning of each script or (better yet) place it in your .Rprofile using:
usethis::edit_r_profile()