需要Rcpp软件包中使用的OpenMP可用性 [英] Requiring OpenMP availability for use in an Rcpp package
问题描述
我已经使用RcppArmadillo
和OpenMP
库以及以下命令在R中准备了一个软件包:
I have prepared a package in R by using RcppArmadillo
and OpenMP
libraries and following commands:
RcppArmadillo.package.skeleton("mypackage")
compileAttributes(verbose=TRUE)
此外,在我添加的DESCRIPTION
文件中:
Also, in the DESCRIPTION
file I added:
Imports: Rcpp (>= 0.12.8), RcppArmadillo
LinkingTo:Rcpp, RcppArmadillo
Depends: RcppArmadillo
并在我添加的NAMESPACE
文件中:
import(RcppArmadillo)
importFrom(Rcpp, evalCpp)
然后我在cmd
中运行以下代码:
Then I run the following codes in cmd
:
R CMD build mypackage
R CMD INSTALL mypackage.tar.gz
我在计算机上生成并安装了该软件包,现在就使用它.但是我的大学和朋友无法安装该软件包.错误消息是有关RcppArmadillo
和OpenMP
库的.
I build and install the package in my computer and I use it now. But my colleges and friends are not able to install the package. The error messages is about RcppArmadillo
and OpenMP
libraries.
例如:
严重错误:找不到'omp.h'文件
fatal error: 'omp.h' file not found
有人在这种情况下有过经验吗?我必须在包装中执行哪种类型的设置才能解决此问题?
Does anyone have previous experience in this case? Which type of settings I have to do in my package for solving this problem?
推荐答案
恭喜!您最有可能偶然发现了macOS缺乏OpenMP
支持的情况. 在Rcpp FAQ中已记录为:条目2.10.3 .
Congratulations! You've most likely stumbled across macOS' lack OpenMP
support. This has been documented in the Rcpp FAQ as entry 2.10.3.
明显的错误原因是您没有适当地保护OpenMP
代码...例如
The reason for the error being apparent is you did not protect the OpenMP
code appropriately... e.g.
标头包含项受以下保护:
Header inclusions are protected with:
#ifdef _OPENMP
#include <omp.h>
#endif
代码具有以下保护:
#ifdef _OPENMP
// multithreaded OpenMP version of code
#else
// single-threaded version of code
#endif
这假设您不是使用预处理器#omp
标记,而是更深入的omp函数调用.如果是以前的代码,则代码保护并不重要,因为预处理器标签将被丢弃.
This assumes you are not using the preprocessor #omp
tags but more indepth omp function calls. If it is the prior, then the code protection is not important as the preprocessor tags will be discarded.
(对于那些长期使用上面的宏方案出现在这里,请注意,从R 3.4.0开始,完全删除了SUPPORT_OPENMP
定义,而取而代之的是_OPENMP
.)
(For those long time users of the above macro schemes coming here, please note that as of R 3.4.0, the SUPPORT_OPENMP
definition was removed completely in favor of _OPENMP
.)
但是,以上只是很好的防御性编码.如果您的软件包需要特定功能,那么也许该考虑使用名为configure.ac
的autoconf
文件生成configure
脚本了.
However, the above is just good defensive coding. If your package requires a specific feature, then it may be time to consider using an autoconf
file called configure.ac
to generate a configure
script.
将configure.ac
放在包装的顶层.
packagename/
|- data/
|- inst/
|- man/
|- src/
|- Makevars
|- HelloWorld.cpp
|- DESCRIPTION
|- NAMESPACE
|- configure.ac
|- configure
configure.ac
应包含以下内容:
AC_PREREQ(2.61)
AC_INIT(your_package_name_here, m4_esyscmd_s([awk -e '/^Version:/ {print $2}' DESCRIPTION]))
AC_COPYRIGHT(Copyright (C) 2017 your name?)
## Determine Install Location of R
: ${R_HOME=$(R RHOME)}
if test -z "${R_HOME}"; then
AC_MSG_ERROR([Could not determine R_HOME.])
fi
## Setup RBin
RBIN="${R_HOME}/bin/R"
CXX=`"${RBIN}" CMD config CXX`
CPPFLAGS=`"${RBIN}" CMD config CPPFLAGS`
CXXFLAGS=`"${RBIN}" CMD config CXXFLAGS`
## Package Requires C++
AC_LANG(C++)
AC_REQUIRE_CPP
## Compiler flag check
AC_PROG_CXX
## Borrowed from BHC/imager/icd/randomForest
# Check for OpenMP
AC_OPENMP
# since some systems have broken OMP libraries
# we also check that the actual package will work
ac_pkg_openmp=no
if test -n "${OPENMP_CFLAGS}"; then
AC_MSG_CHECKING([OpenMP detected, checking if viable for package use])
AC_LANG_CONFTEST([AC_LANG_PROGRAM([[#include <omp.h>]], [[ return omp_get_num_threads (); ]])])
"$RBIN" CMD SHLIB conftest.c 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD && "$RBIN" --vanilla -q -e "dyn.load(paste('conftest',.Platform\$dynlib.ext,sep=''))" 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD && ac_pkg_openmp=yes
AC_MSG_RESULT([${ac_pkg_openmp}])
fi
# if ${ac_pkg_openmp} = "yes" then we have OMP, otherwise it will be "no"
if test "${ac_pkg_openmp}" = no; then
AC_MSG_WARN([No OpenMP support. If using GCC, upgrade to >= 4.2. If using clang, upgrade to >= 3.8.0])
AC_MSG_ERROR([Please use a different compiler.])
fi
# Fin
AC_OUTPUT
要生成configure
脚本,请运行:
autoconf
完成此操作后,您将需要重建软件包.请注意,如果您可能需要在 Windows 和macOS上安装autoconf
通过自制软件安装.
Once this is done, you will need to rebuild your package. Note, you may need to install autoconf
if on Windows and on macOS you likely need to install it via homebrew.
现在,您可能要确保您的同事能够从启用OpenMP
的代码中获得加速收益.为此,必须让您的同事从使用默认系统编译器转移到使用"true" gcc
或可行的启用omp
的clang
编译器,从而启用OpenMP
.
Now, you may want to ensure your colleagues are able to get the speedup gain from your OpenMP
-enabled code. To do so, one must enable OpenMP
by having your colleagues shift away from using the default system compiler to either a "true" gcc
or a viable omp
enabled clang
compiler.
有关macOS的说明,请参见此处:
Instructions for both on macOS are given here:
http://thecoatlessprofessor.com/programming/openmp-in -r-on-os-x/
这篇关于需要Rcpp软件包中使用的OpenMP可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!