使用Intel C/C++ Compiler搭配Intel MKL编译R

Posted CompBioLab

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Intel C/C++ Compiler搭配Intel MKL编译R相关的知识,希望对你有一定的参考价值。

1. 安装环境

名称
OS Ubuntu 18.04
Disk space > 30 GiB
Memory ≥ 1G

2. 安装Intel oneAPI (含C/C++编译器、MKL库)



  
    
    
  

1

wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB

2

sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB

3

echo "deb https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list

4

sudo add-apt-repository "deb https://apt.repos.intel.com/oneapi all main"

5

sudo apt-get install intel-basekit intel-hpckit -y


3. 安装R编译依赖



  
    
    
  

1

sudo apt-get install subversion ccache texlive texlive-fonts-extra texlive-latex-extra libx11-dev libpcre2-dev libjpeg-dev libpng-dev libtiff-dev libxmu-dev libxt-dev libreadline-dev default-jre libbz2-dev libcurl4-gnutls-dev texinfo default-jdk -y


4. 设置编译前环境

环境变量/传递给编译器的参数			
   
     
     
   

1

export PATH=/opt/intel/oneapi/compiler/latest/linux/bin/intel64:$PATH

2

source /opt/intel/oneapi/mkl/latest/env/vars.sh intel64

3

export _intel_arch=intel64

4

export _intel_lib=mkl_intel_lp64

5

export MKL="-L${MKLROOT}/lib/intel64 -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -lmkl_rt -lpthread -lm -ldl"

6

export _intel_cc_opt="-O3 -fPIC -m64 -march=native -fp-model precise -fp-model source -I${MKLROOT}/include"

FLAGS for compiler



    
      
      
    

1

export LDFLAGS="-qopenmp"

2

export CFLAGS="${_intel_cc_opt}"

3

export CXXFLAGS="${_intel_cc_opt}"

4

export FFLAGS="${_intel_cc_opt}"

5

export FCFLAGS="${_intel_cc_opt}"
Redirect compiler prerequisite to Intel C/C++/Fortran Compiler



  
    
    
  

1

export CC="icc"

2

export CXX="icpc"

3

export F77="ifort"

4

export FC="ifort"

5

export AR="xiar"

6

export LD="xild"

7

export LD_LIBRARY_PATH=/opt/intel/oneapi/compiler/latest/linux/compiler/lib/intel64

8

source /opt/intel/oneapi/mkl/latest/env/vars.sh ${_intel_arch}

According to this bug report[6], the javah binary has been removed from OpenJDK 10 and the java header generator should be called "javac -h". If we search for "JAVAH" in R source tree, we could find this in /configure.ac:



   
     
     
   

1

custom_JAVA_LIBS="${JAVA_LIBS}"

2

custom_JAVA_CPPFLAGS="${JAVA_CPPFLAGS}"

3

custom_JAVA_LD_LIBRARY_PATH="${JAVA_LD_LIBRARY_PATH}"

4

custom_JAVA="${JAVA}"

5

custom_JAVAC="${JAVAC}"

6

custom_JAVAH="${JAVAH}"

7

custom_JAR="${JAR}"

which means we could manually do this


    
      
      
    

1

export JAVAH="/usr/bin/javac -h"

PS: It works

5. 开始编译R

PS: 因为本次同时编译了4.0.5和development branch的R,因此图混着用了,除了dev branch性能稍好一些以外大概的东西应该都展示清楚了(



  
    
    
  

1

cd /tmp/

2

wget "https://cran.r-project.org/src/base-prerelease/R-latest.tar.gz" -q

3

tar zxf R-latest.tar.gz && rm R-latest.tar.gz

4

cd R-latest/

5

sed -i '/^#undef HAVE_MATHERR/d' src/include/config.h.in

6

./configure --prefix=/usr --libdir=/usr/lib/R/ --sysconfdir=/etc/R --datarootdir=/usr/share rsharedir=/usr/share/R/ rincludedir=/usr/include/R/ rdocdir=/usr/share/doc/R/ --with-x --enable-R-shlib --with-blas="$MKL" --with-lapack --with-tcltk LIBnn=lib

7

make



使用Intel C/C++ Compiler搭配Intel MKL编译R

使用Intel C/C++ Compiler搭配Intel MKL编译R

6. 使用

		
  
    
    
  

1

pwd ##先把R-devel文件夹放入用户目录,再将当前目录加入环境变量PATH里

2

export PATH=/home/username/R-devel/bin:$PATH


7. 测试

跑分试试,左侧为4.0.5,右侧为dev branch r80254 (2021-05-01)


  
    
    
  

1

Rscript ./dgemm.R



References

[1]  https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Build-R-with-Intel-MKL-shared-library/td-p/1034642?profile.language=en
[2] https://community.intel.com/t5/Intel-C-Compiler/Error-when-compiling-R-from-source-code-ubuntu-18-04/td-p/1176401
[3]  https://aur.archlinux.org/packages/r-mkl/
[4]  https://cran.r-project.org/doc/manuals/r-release/R-admin.html
[5]  https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Build-R-with-Intel-MKL-shared-library/td-p/1034642?profile.language=en

[6] bug report: https://bugs.launchpad.net/ubuntu/+source/ceph/+bug/1766995

以上是关于使用Intel C/C++ Compiler搭配Intel MKL编译R的主要内容,如果未能解决你的问题,请参考以下文章

intel visual fortran compiler干嘛用的

intel compiler的表现

可以将 Python 与 Intel 的 Atom Developer SDK (C/C++) 一起使用吗?

我可以将 x86 程序集的 Intel 语法与 GCC 一起使用吗?

linux下intel的mkl编程代码,怎么样编译。C++和C语言代码

高级CGNU C/C++ 内联汇编——Intel与ATT汇编语法对比