Skip to content

Instantly share code, notes, and snippets.

@nicebread
Last active August 29, 2023 09:00
Show Gist options
  • Save nicebread/6920c8287d7bffb03007 to your computer and use it in GitHub Desktop.
Save nicebread/6920c8287d7bffb03007 to your computer and use it in GitHub Desktop.
These code snippets have been tested on R 3.1.0 and Mac OS 10.9.3. They presumably do *not* work on R 2.X!
## Enter these commands in the Mac OS Terminal
# use faster vecLib library
cd /Library/Frameworks/R.framework/Resources/lib
ln -sf /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/libBLAS.dylib libRblas.dylib
# return to default settings
cd /Library/Frameworks/R.framework/Resources/lib
ln -sf libRblas.0.dylib libRblas.dylib
# R script for benchmarking:
# http://r.research.att.com/benchmarks/R-benchmark-25.R
# On 2011 Macbook Pro (2.7 GHz Core i7): 43 sec. (Rblas) vs. 9 sec. (vecLib)
@tvatter
Copy link

tvatter commented Jul 14, 2017

I tried the same thing and it also failed.

However, I am wondering whether linking to BLAS only may be enough.

On my computer, the default BLAS from R gives:

> d <- 2e3
> system.time({ x <- matrix(rnorm(d^2),d,d); tcrossprod(x) })
   user  system elapsed 
  3.648   0.031   3.682 
> 
> d <- 1e3
> system.time({ x <- matrix(rnorm(d^2),d,d); svd(x) })
   user  system elapsed 
  3.400   0.020   3.422 

And veclib's BLAS gives:

> d <- 2e3
> system.time({ x <- matrix(rnorm(d^2),d,d); tcrossprod(x) })
   user  system elapsed 
  0.871   0.028   0.517 
> 
> d <- 1e3
> system.time({ x <- matrix(rnorm(d^2),d,d); svd(x) })
   user  system elapsed 
  1.049   0.091   0.569 

Assuming that the crossprod is from BLAS and the SVD from LAPACK, I wonder how to interpret the results above...

@tvatter
Copy link

tvatter commented Jul 14, 2017

Actually, it seems that the brewed install points toward veclib's lapack:

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin16.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1   
> d <- 5e3
> system.time({ x <- matrix(rnorm(d^2),d,d); tcrossprod(x) })
   user  system elapsed 
 10.088   0.227   3.766 
> system.time({ x <- matrix(rnorm(d^2),d,d); solve(x) })
   user  system elapsed 
 24.642   0.425   6.205 

However, when using the standard R with linking to veclib's BLAS only, I obtain comparable performances:

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tvmisc_0.1.0

loaded via a namespace (and not attached):
[1] compiler_3.4.0 tools_3.4.0    rstudioapi_0.6 fortunes_1.5-4
> d <- 5e3
> system.time({ x <- matrix(rnorm(d^2),d,d); tcrossprod(x) })
   user  system elapsed 
  9.464   0.243   3.568 
> system.time({ x <- matrix(rnorm(d^2),d,d); solve(x) })
   user  system elapsed 
 22.934   0.509   6.545 

@alaminzju
Copy link

After following the commands:

use faster vecLib library

cd /Library/Frameworks/R.framework/Resources/lib
ln -sf /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/libBLAS.dylib libRblas.dylib

Rstudio in my macOS (Monterey, version 12.0.1) is not working. I got the following error:

This site can’t be reached
127.0.0.1 refused to connect.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
Check your Internet connection
Check any cables and reboot any routers, modems, or other network devices you may be using.
Allow Chromium to access the network in your firewall or antivirus settings.
If it is already listed as a program allowed to access the network, try removing it from the list and adding it again.
If you use a proxy server…
Check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server: Go to Applications > System Preferences > Network > Advanced > Proxies and deselect any proxies that have been selected.

Could you please give your kind suggestions regarding this issue?

@ShawnWx2019
Copy link

After following the commands:

use faster vecLib library

cd /Library/Frameworks/R.framework/Resources/lib ln -sf /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/libBLAS.dylib libRblas.dylib

Rstudio in my macOS (Monterey, version 12.0.1) is not working. I got the following error:

This site can’t be reached 127.0.0.1 refused to connect. Try:

Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED Check your Internet connection Check any cables and reboot any routers, modems, or other network devices you may be using. Allow Chromium to access the network in your firewall or antivirus settings. If it is already listed as a program allowed to access the network, try removing it from the list and adding it again. If you use a proxy server… Check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server: Go to Applications > System Preferences > Network > Advanced > Proxies and deselect any proxies that have been selected.

Could you please give your kind suggestions regarding this issue?

cuz macOS 12.0.1 has removed libBLAS.dylib from /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/

so another way to speed up matrix calculation in R is use openblas

so if Homebrew has been installed in your Mac, pls try:

brew tap homebrew/science
brew install openblas

then check the dir

cd /usr/local/opt/openblas/lib

if there is a libopenblas.dylib in this dir,

ln -sf /usr/local/opt/openblas/lib/libopenblas.dylib /Library/Frameworks/R.framework/Resources/lib/libRblas.dylib

sorry for my poor English...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment