BLAS setup in R on macOS - r

I am quite new to BLAS and Lapack setup. Executing
sessioninfo()
always returns
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
I was wondering how BLAS can be called? Because I see many forks' default matrix products have both BLAS and LAPACK.
Also, how Intel MKI can be called? I have installed it on my macOS, but still wonder how to use it.
Also, my R is 4.0.5 and macOS is macOS Big Sur 11.2.3

Please follow the below instructions, to build R with default BLAS, LAPACK using gnu compiler chain.
$ tar -xzvf R-4.0.5.tar.gz
$ cd R-4.0.5
$ ./configure
(or $./configure --with-readline=no --with-x=no if package readline and X11 is not installed)
$make
(not $ make install, so, we do not pollute system directory)
$ ldd bin/exec/R
(To make sure it will link libRblas.so although it may show that libRblas.so => not found)
For developers who have installed R, please locate the path of libRblas.so and
libRlapack.so (or libR.so), for example, $cd /usr/local/lib64/R
$ cd lib
$ mv libRblas.so libRblas.so.keep
$ln –s $(MKLROOT)/mkl/lib/intel64/libmkl_rt.so libRblas.so
The same way, you can replace the LAPACK libRlapack.so library too
($mv libRlapack.so libRlapack.so.keep
$ln –s $(MKLROOT)/mkl/lib/intel64/libmkl_rt.so libRlapack.so)
If you have prebuilt R with libR.so, replace it with
$(MKLROOT)/mkl/lib/intel64/libmkl_rt.so
Also you can go through the below links
https://software.intel.com/content/www/us/en/develop/articles/quick-linking-intel-mkl-blas-lapack-to-r.html
https://software.intel.com/content/www/us/en/develop/articles/using-intel-mkl-with-r.html

Related

Can't get R to recognize installed libgfortran when installing packages

I'm trying to install packages in R that depend on libgfortran, but I keep getting this error:
dyld: Library not loaded: /usr/local/opt/gcc/lib/gcc/7/libgfortran.4.dylib
Referenced from: /usr/local/Cellar/r/3.4.3_1/lib/libR.dylib
Reason: image not found
I have gfortran installed through homebrew gcc:
❯❯ readlink `which gfortran`
../Cellar/gcc/8.2.0/bin/gfortran
There's a libgfortran available at /usr/local/opt/gcc/lib/gcc/8/libgfortran.5.dylib, but this is not the one R is looking for. How do I convince R to look for gcc 8 instead of gcc 7?
"How do I convince R to look for gcc 8 instead of gcc 7?"
You should not do that! Those libraries are NOT compatible, that's why soname is changed. The ABI is different. You need the correct version of GCC which was used to compile your R package. See very similar (but Linux) questions and answers like R v3.4.0-2 unable to find libgfortran.so.3 on Arch How to install libgfortran.so.4 on ubuntu 16.06
You really need the compatible library or update your R to a version that uses GCC 8.

dyld: Library not loaded: /usr/local/opt/openblas/lib/libopenblasp-r0.2.20.dylib

When I run R I get:
dyld: Library not loaded: /usr/local/opt/openblas/lib/libopenblasp-r0.2.20.dylib
Referenced from: /usr/local/Cellar/r/3.5.0_1/lib/libR.dylib
Reason: image not found
Abort trap: 6
Indeed the file is absent:
ls /usr/local/opt/openblas/lib/libopenblasp-r0.2.20.dylib
ls: cannot access '/usr/local/opt/openblas/lib/libopenblasp-r0.2.20.dylib': No such file or directory
I'm on macOS 10.13.3 and used homebrew to install R like this:
# Java
brew cask install java
# OpenBLAS (installs gcc and other dependencies)
brew install openblas
# R language for statistical computing
brew install r --with-openblas --with-java
# Install XQuartz, needed for R package "Cairo"
brew cask install xquartz
# Needed for R package "RMySQL"
brew install mariadb-connector-c
# Needed for R packages: udunits2, units, ggforce
brew install udunits
Let's see if the installed version of openblas is 0.2.20:
brew info openblas
openblas: stable 0.3.0 (bottled), HEAD [keg-only]
Optimized BLAS library
https://www.openblas.net/
/usr/local/Cellar/openblas/0.3.0 (22 files, 139MB)
Poured from bottle on 2018-05-31 at 20:42:55
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openblas.rb
==> Dependencies
Required: gcc ✔
==> Options
--with-openmp
Enable parallel computations with OpenMP
--HEAD
Install HEAD version
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BLAS and LAPACK in the Accelerate framework.
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/openblas/lib
CPPFLAGS: -I/usr/local/opt/openblas/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/openblas/lib/pkgconfig
We have 0.3.0 but R is looking for 0.2.20
To fix this, we can create a symlink from the installed dylib:
ln -s /usr/local/opt/openblas/lib/libopenblas.dylib \
/usr/local/opt/openblas/lib/libopenblasp-r0.2.20.dylib
It works!
R
R version 3.5.0 (2018-04-23) -- "Joy in Playing"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.4.0 (64-bit)
Another solution is simply to reinstall R:
brew reinstall r
This will relink R against the correct libraries, and you won't have any symlinks that might get orphaned or cause problems later. This won't affect any R packages you have already installed. Also, if you've already installed the most recent version of R with brew and the bottle is still on your computer then you won't have to download the bottle again.
In my case running uninstall and reinstall didn't resolve the issue. Running brew doctor revealed I had unlined kegs including gcc running brew link gcc fixed the issue.
brew upgrade R in most cases will make sure the requirements are installed:
==> Upgrading 1 outdated package:
r 3.5.3 -> 3.6.1
==> Upgrading r
==> Pouring r-3.6.1.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/r/3.6.1: 2,121 files, 56.6MB
Removing: /usr/local/Cellar/r/3.5.3... (2,118 files, 55.8MB)
I had a similar error, first for 'libopenblas' and then for 'libgfortran' on Mac OSX 10.14 using R 3.5.2_2.
Solved it by doing:
brew upgrade R
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/cask and homebrew/bundle).
No changes to formulae.
==> Upgrading 1 outdated package:
r 3.5.2_2 -> 3.6.1
==> Upgrading r
==> Downloading https://homebrew.bintray.com/bottles/r-3.6.1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/1e/1ed9fe16ae694fa3b35a7d979853447eb7b8aec1e804019a1bf4aafa299a6696?__gd
######################################################################## 100.0%
==> Pouring r-3.6.1.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/r/3.6.1: 2,121 files, 56.6MB
Removing: /usr/local/Cellar/r/3.4.1_2... (2,113 files, 55.2MB)
Removing: /usr/local/Cellar/r/3.5.1... (2,116 files, 55.6MB)
Removing: /usr/local/Cellar/r/3.5.2_2... (2,119 files, 56.0MB)
R works perfectly after that:
R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin18.6.0 (64-bit)

Problems compiling rJava with RStudio on macOS Sierra 10.12.6

I am trying to install the rJava package into Rstudio using these instructions:
https://github.com/MTFA/CohortEx/wiki/Run-rJava-with-RStudio-under-OSX-10.10,-10.11-(El-Capitan)-or-10.12-(Sierra)
However, when I run this command:
unset JAVA_HOME R --quiet -e 'install.packages("rJava", type="source", repos="http://cran.us.r-project.org")'
I am getting this error:
clang: error: unsupported option '-fopenmp'
Any workarounds or suggestions?
Take a look here. Lots depends on R version. It might be that you need to recompile rJava
R 3.4, rJava, macOS and even more mess ;)
R, Java, rJava and macOS adventures
You need compiler with OpenMP support - thus, you need to use different clang version comparing to what you get out of the box.

How to set up conda-installed R for use with RStudio?

I've been trying to set up my R using conda (eventually to use with Beaker Notebook) and I want to be able to use RStudio with my conda-installed version of R.
My method of installing R:
conda install -c r r
conda install -c r r-essentials
conda install -c r r-rserve
conda install -c r r-devtools
conda install -c r r-rcurl
conda install -c r r-RJSONIO
conda install -c r r-jpeg
conda install -c r r-png
conda install -c r r-roxygen2
conda install --channel https://conda.anaconda.org/bioconda bioconductor-edger
I ran that version of R (I only installed this version)
> version
_
platform x86_64-apple-darwin11.0.0
arch x86_64
os darwin11.0.0
system x86_64, darwin11.0.0
status
major 3
minor 3.1
year 2016
month 06
day 21
svn rev 70800
language R
version.string R version 3.3.1 (2016-06-21)
nickname Bug in Your Hair
Running R in Jupyter is kind of buggy. For example, when it outputs errors, it outputs to stdout and splits every character in the string with a linebreak. I want to use RStudio but I don't want to install another version of R.
How can I route my conda version of R into RStudio?
Here's my .bash_profile not sure if this will be useful:
$ cat ~/.bash_profile
# added by Anaconda3 4.0.0 installer
export PATH="/Users/jespinoz/anaconda/bin:$PATH"
export RSTUDIO_WHICH_R=/Users/jespinoz/anaconda/bin/R
I've been trying to follow these tutorials but I am lost. I'm really not too familiar with environment variables and such things.
(1) https://support.rstudio.com/hc/en-us/community/posts/207830688-Using-RStudio-with-conda
(2) Launch mac eclipse with environment variables set
when I looked for my R it directed me to:
$ which R
/Users/jespinoz/anaconda/bin/R
but the directions from (1) is using this path which is very confusing:
/Users/jespinoz/anaconda/lib/R/bin/R
I tried doing what this guy did and added this to my .bash_profile but it didn't work. I even made a .bashrc but it still didn't work (I sourced both after I added the lines)
export RSTUDIO_WHICH_R=/Users/jespinoz/anaconda/bin/R
How to tell RStudio to use R version from Anaconda
Unfortunately, anaconda has no tutorial for this in https://docs.continuum.io/anaconda/ide_integration
See https://anaconda.org/r/rstudio:
$ conda install -c r rstudio
Then from command line:
$ rstudio
(It is how I installed it and it works.)
So long as which R shows up a working R interpreter (which it should do if you have installed the r package from conda and activated your environment) then launching rstudio from that same environment should pick it up just fine.
For a test, on ArchLinux, I built and installed: https://aur.archlinux.org/packages/rstudio-desktop-git/
.. then force removed the R interpreter (pacman -Rdd r), then installed r from conda (conda install -c r r) and it worked fine. I then closed my terminal and opened a new one (so that the correct conda environment was not activated and successfully launched RStudio with the following command: RSTUDIO_WHICH_R=/home/ray/r_3_3_1-x64-3.5/bin/R rstudio
I think the crux is to launch RStudio from the right environment? Your ~/.bash_profile and ~/.bashrc are only sourced when you run bash. For environment variables to be set so that the your desktop environment knows about them, on Linux, you should put them in ~/.profile or else in /etc/pam.d (you may need to logout or shutdown after making those changes) and on OS X, you should check out https://apple.stackexchange.com/q/57385
Update: ADD THIS TO ~/.bash_profile !
export RSTUDIO_WHICH_R="/Users/jespinoz/anaconda/bin/R"
launchctl setenv RSTUDIO_WHICH_R $RSTUDIO_WHICH_R
Credits to #Z-Shiyi for the last line https://github.com/conda/conda/issues/3316#issuecomment-241246755
An addition to what #Ray Donnelly said above. Basically, it has to be executed from the correct environment (i.e. run it from the terminal).
You can either:
(A) Put this in your ~/.bash_profile
export RSTUDIO_WHICH_R=/Users/[yourusername]/anaconda/bin/R (if youre using conda but you could put any R path)
(B) then type this in the terminal after it's been sourced (either restart terminal or do source .bash_profile): open -a RStudio
That should work.
or you can do what I did:
(A) open up automator (sorry if you're not on a mac; this will only work on mac)
(B) use a Run Shell Script
(C) then delete cat that's already in there and put in:
export RSTUDIO_WHICH_R=/Users/[yourusername]/anaconda/bin/R
open -a RStudio
(D) Save it as something like run_rstudio.app then just run that and it should work:
Launch RStudio from Activated Conda Environment
At least for Mac OS X, I find that it is sufficient to activate the environment in a shell session, then launch RStudio.
$ conda activate my_r_env
$ /Applications/RStudio.app/Contents/MacOS/RStudio
Once in R, one can verify that values of R.home() and .libPaths() point to the environment-specific locations.
The advantage here is that you aren't fixed to whatever was last set in the environment variables, e.g., via .bash_profile. Instead, one can have many R-based environments and switch between them (or run multiple ones simultaneously) without tampering with global settings.
Suggested Alias for Convenience
Perhaps the only global setting I might recommend is to add an alias for rstudio to your .bash_profile so you don't have to type the full path every time, like
alias rstudio='/Applications/RStudio.app/Contents/MacOS/RStudio &'
which enables one to then do
$ conda activate my_r_env
$ rstudio
$
where the & enables one to continue using the shell, or close it, without affecting the RStudio instance.
Update: The Anaconda Distribution now has packages for RStudio so you should be able to use that and not have to jump through any hoops at all. You can also install it directly the Anaconda Navigator.
Making a soft link works for me:
ln -s /opt/miniconda3/envs/r-4.2/bin/R /usr/bin/R
if is up to any good (now)... conda has the package rstudioapi which brings Rstudio to your local environment, and picks up the local/default r-base installed of your active environment.
you can install it (once your environment is activated) by typing:
conda install -c conda-forge r-rstudioapi
then you just type (inside your environment): rstudio

How do I linking to a non-standard library when building an R package

I'm trying to build the R openssl package and link it to an openssl library that is a newer version than the one in the systemwide lib64 directory.
The Makevars file simply has:
KG_CPPFLAGS=#cflags#
PKG_LIBS=#libs#
I can specify my local library by passing a custom library path in the configure script. But when I issue R CMD INSTALL, the linking command begins with
gcc -std=gnu99 -shared -L/usr/local/lib64 -o openssl.so [...objects...] -Lmy/path/here -lssl -lcrypto
As a result, the linker finds the wrong library (the one in /usr/local/lib64) instead of mine.
How do I adjust the linking command so that it looks for the library first in my directory, instead of the system folder.
(Background: I don't have root access and this older version of CentOS (5) doesn't support openssl v1, so I built v1 myself just to compile the R package. But I'm stuck at the linking stage due to whatever magic is happening by R CMD INSTALL.)
I solved this by passing arguments to R CMD INSTALL. I.e.:
R CMD INSTALL . --configure-vars="INCLUDE_DIR=$HOME/local/include LIB_DIR=$HOME/local/lib64"
But this varies for each package. There's no general solution.
Instead, read config and src/Makefile.in.

Resources