Link the R Package Depending on RcppEigen with MKL in Microsoft R Open

*爱你&永不变心* 提交于 2019-12-04 13:43:49

Partial answer to record my findings. Maybe others can build on this.

Linux with R linked against MKL

For this setup I used a Debian stable machine with R 3.5.0 installed. I installed MKL via this script. For compilation I used an add-hoc plugin with the compilation flags provided in the updated question plus -Wno-ignored-attributes to silence some unrelated warnings:

library(Rcpp)

registerPlugin(
  name = "mkl",
  plugin = function(x) {
    list(
      includes = "#define EIGEN_USE_MKL_ALL",
      env = list(PKG_CXXFLAGS = "-DMKL_LP64 -m64  -I/opt/intel/mkl/include -Wno-ignored-attributes",
                 PKG_LIBS = "${LAPACK_LIBS} ${BLAS_LIBS} ${FLIBS}  -L/opt/intel/mkl/lib/intel64 -Wl,--no-as-needed,-rpath,'/opt/intel/mkl/lib/intel64' -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl")
    )
  }
)

cppFunction('
Eigen::VectorXd mkl_sin(Eigen::VectorXd x) {
  return x.array().sin();
}
', plugins = "mkl", depends = "RcppEigen")
mkl_sin((1:10)/10)
#>  [1] 0.09983342 0.19866933 0.29552021 0.38941834 0.47942554 0.56464247] 0.64421769 0.71735609 0.78332691 0.84147098

This works without any warnings, so I conclude that it works when R is linked with MKL as external BLAS/LAPACK.

Linux with MRO

For this setup I used a Ubuntu based Docker image:

FROM ubuntu:16.04

RUN apt-get update \
 && apt-get install --yes --no-install-recommends \
    apt-transport-https \
    build-essential \
    ca-certificates \
    curl \
    gfortran \
 && curl -O https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb \
 && dpkg -i packages-microsoft-prod.deb \
 && apt-get update \
 && apt-get install --yes --no-install-recommends \
    microsoft-r-open-foreachiterators-3.4.3 \
    microsoft-r-open-mkl-3.4.3 \
    microsoft-r-open-mro-3.4.3 \
 && Rscript -e 'install.packages("RcppEigen")'

The problem with MRO is that it does not include the MKL headers and only part of the MKL library:

ls /opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_*
/opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_core.so
/opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_gf_ilp64.so
/opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_gf_lp64.so
/opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_gnu_thread.so
/opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_vml_def.so
/opt/microsoft/ropen/3.4.3/lib64/R/lib/libmkl_vml_mc3.so

In particular libmkl_intel_lp64.so, which is requested in the linking command above, is missing. It is therefore not possible to use the above recipe with MRO. It might work to install MKL in addition to MRO and link with that, but I have not tested this.

However, MKL will be used whenever Eigen falls back to BLAS/LAPACK methods. This will speed up operations when compared to an R build using reference BLAS/LAPACK (default for Windows).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!