在Microsoft R Open中将取决于RcppEigen的R程序包与MKL链接 [英] Link the R Package Depending on RcppEigen with MKL in Microsoft R Open

查看:163
本文介绍了在Microsoft R Open中将取决于RcppEigen的R程序包与MKL链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一些用RcppEigen编写的功能构建了一个自定义程序包.我还启用了Microsoft MKL并打开了MicrosoftR.如何将R软件包链接到英特尔MKL功能?

I have built a custom package with some functions written in RcppEigen. I also have Microsoft R open with Intel MKL enabled. How could I link the R package to the Intel MKL feature?

设置1 :

以下是我尝试将程序包与普通R中的MKL链接但失败的过程:

Below are procedures that I have tried to link the package with MKL in the normal R, but failed:

本征文档说我需要:

1. #define EIGEN_USE_MKL_ALL
2. link your program to MKL libraries 

( MKL链接顾问 )

基于2,在我的文件Makevars中

Based on 2, in my file Makevars

PKG_CXXFLAGS = -I/opt/intel/mkl/include 
PKG_LIBS = ${LAPACK_LIBS} ${BLAS_LIBS} ${FLIBS}  -L/opt/intel/mkl/lib/intel64 -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl

编译软件包时出现错误:

I got the errors when compiling the package:

Error in dyn.load(dllfile) : 
unable to load shared object '/home/path/RPackageName.so':
libmkl_intel_lp64.so: cannot open shared object file: No such file or directory

根据Ralf的评论进行更新:在Makevars文件中添加选项<,-rpath,'path'>,错误消失了.

Update based on Ralf's comment: in the Makevars file add the option <,-rpath,'path'>, the errors are gone.

PKG_CXXFLAGS = -DMKL_LP64 -m64  -I/opt/intel/mkl/include 
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 

该软件包已成功编译,但下面带有这些消息,我不理解.

The package compiled successfully, but with these messages below, which I do not understand.

/home/shen/R/x86_64-pc-linux-gnu-
library/3.4/RcppEigen/include/Eigen/src/Core/Assign_MKL.h: In 
instantiation of ‘static void Eigen::internal::Assignment<DstXprType, 
Eigen::CwiseUnaryOp<Eigen::internal::scalar_log_op<double>, 
SrcXprNested>, Eigen::internal::assign_op<double, double>, 
Eigen::internal::Dense2Dense, typename 
Eigen::internal::enable_if<Eigen::internal::vml_assign_traits<Dst, 
Src>::EnableVml>::type>::run(DstXprType&, const SrcXprType&, const 
Eigen::internal::assign_op<double, double>&) [with DstXprType = 
Eigen::Matrix<double, -1, 1>; SrcXprNested = const 
Eigen::ArrayWrapper<const Eigen::Matrix<double, -1, -1> >; 
Eigen::internal::Assignment<DstXprType, 
Eigen::CwiseUnaryOp<Eigen::internal::scalar_log_op<double>, 
SrcXprNested>, Eigen::internal::assign_op<double, double>, 
Eigen::internal::Dense2Dense, typename 
Eigen::internal::enable_if<Eigen::internal::vml_assign_traits<Dst, 
Src>::EnableVml>::type>::SrcXprType = 
Eigen::CwiseUnaryOp<Eigen::internal::scalar_log_op<double>, const 
Eigen::ArrayWrapper<const Eigen::Matrix<double, -1, -1> > >]’:
/home/shen/R/x86_64-pc-linux-gnu-
library/3.4/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:836:49:   
required from ‘void Eigen::internal::call_assignment_no_alias(Dst&, 
const Src&, const Func&) [with Dst = Eigen::Matrix<double, -1, 1>; Src 
= Eigen::CwiseUnaryOp<Eigen::internal::scalar_log_op<double>, const 
Eigen::ArrayWrapper<const Eigen::Matrix<double, -1, -1> > >; Func = 
Eigen::internal::assign_op<double, double>]’

设置2 我直接在MRO中编译,文件Makevars中没有任何特殊参数,但出现以下错误:

Setup 2 I compiled directly in MRO without any special arguments in the file Makevars, got the errors below:

/home/shen/R/x86_64-pc-linux-gnu-
library/3.3/RcppEigen/include/Eigen/src/Core/util/MKL_support.h:57:21: 
fatal error: mkl.h: No such file or directory

PS:我使用Microsoft R open的经验是,它们无需执行任何操作即可加速Armadillo中的常规R脚本和功能.只需在Microsoft R Open中正常运行它们即可.

PS: My experiences with the Microsoft R open are that they could accelerate the normal R scripts and functions in Armadillo without doing anything. Just run them normally in the Microsoft R Open.

推荐答案

部分答案记录了我的发现.也许其他人可以以此为基础.

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

对于此设置,我使用了已安装R 3.5.0的Debian稳定机.我通过此脚本安装了MKL.对于编译,我使用了一个add-hoc插件,该插件具有更新问题中提供的编译标志以及

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

此方法没有任何警告,因此我得出结论,当R与MKL作为外部BLAS/LAPACK链接时,它可以工作.

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

对于此设置,我使用了基于Ubuntu的Docker映像:

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")'

MRO的问题在于它不包括MKL标头,而仅包括MKL库的一部分:

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

尤其是上面的链接命令中要求的libmkl_intel_lp64.so丢失.因此,无法将上述配方与MRO一起使用.除了MRO之外,还可以安装MKL并与其链接,但这可能会起作用,但是我尚未对此进行测试.

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.

但是,只要Eigen退回到BLAS/LAPACK方法,就将使用MKL.与使用参考BLAS/LAPACK的R构建(Windows的默认设置)相比,这将加快操作速度.

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).

这篇关于在Microsoft R Open中将取决于RcppEigen的R程序包与MKL链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆