Skip to content

Instantly share code, notes, and snippets.

@crabba
Last active December 14, 2023 14:55
Show Gist options
  • Save crabba/ea7466c56047e3009da225204ff40b46 to your computer and use it in GitHub Desktop.
Save crabba/ea7466c56047e3009da225204ff40b46 to your computer and use it in GitHub Desktop.
# Process to build R with MKL support on Amazon Linux 2
# Install R v4
sudo amazon-linux-extras install R4
wget https://mac.r-project.org/benchmarks/R-benchmark-25.R
which R # /usr/bin/R
R --version # 4.0.2
# Install Intel MKL library
# https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-download.html?operatingsystem=linux&distributions=yumpackagemanager
tee > /tmp/oneAPI.repo << EOF
[oneAPI]
name=Intel® oneAPI repository
baseurl=https://yum.repos.intel.com/oneapi
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://yum.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
EOF
sudo mv /tmp/oneAPI.repo /etc/yum.repos.d
sudo yum install -y intel-basekit # Not necessary?
sudo yum install -y intel-oneapi-mkl
# Build tools
sudo yum groupinstall -y "Development Tools"
sudo yum install -y readline-devel libXt-devel libcurl-devel
# Hack for make install. Need to be present before ./configure.
sudo mkdir -p /opt/intel/oneapi/{2024.0/lib,redist/lib}
sudo ln -s /opt/intel/oneapi/mkl/2024.0/lib/libmkl_gf_lp64.so.2 /opt/intel/oneapi/2024.0/lib/libmkl_gf_lp64.so.2
sudo ln -s /opt/intel/oneapi/mkl/2024.0/lib/libmkl_gf_lp64.so.2 /opt/intel/oneapi/redist/lib/libmkl_gf_lp64.so.2
# Build R with MKL support
# https://www.intel.com/content/www/us/en/developer/articles/technical/using-onemkl-with-r.html
wget https://cran.r-project.org/src/base/R-4/R-4.3.2.tar.gz
gunzip --force R-4.3.2.tar.gz
tar xf R-4.3.2.tar
cd R-4.3.2
source /opt/intel/oneapi/setvars.sh intel64 ## NOTE key difference from Intel instructions on above page
MKL="-Wl,--no-as-needed -lmkl_gf_lp64 -Wl,--start-group -lmkl_gnu_thread -lmkl_core -Wl,--end-group -fopenmp -ldl -lpthread -lm"
./configure --with-blas="$MKL" --with-lapack
make || exit
sudo make install
# Tests
cd
time /usr/bin/R < R-benchmark-25.R --save # System R
time /usr/local/bin/R < R-benchmark-25.R --save # Compiled R
# Are we using MKL?
/usr/bin/R -q -e 'sessionInfo()' # System R
/usr/local/bin/R -q -e 'sessionInfo()' # Compiled R
@crabba
Copy link
Author

crabba commented Dec 4, 2023

  • Output from systemInfo():
    • Building MKL from source
      • BLAS/LAPACK: /opt/intel/oneapi/mkl/2024.0/lib/libmkl_gf_lp64.so.2; LAPACK version 3.10.1
    • Installing MKL from Yum
      • BLAS: /usr/local/lib64/R/lib/libRblas.so
      • LAPACK: /usr/local/lib64/R/lib/libRlapack.so; LAPACK version 3.11.0
    • Without MKL
      • BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

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