CRAN 可接受的链接到 OpenMP 的一些从 Rcpp 调用的 C 代码的方式 [英] CRAN-acceptable way of linking to OpenMP some C code called from Rcpp

查看:41
本文介绍了CRAN 可接受的链接到 OpenMP 的一些从 Rcpp 调用的 C 代码的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 R 包,其中包含一些带有使用 OpenMP 的代码的 .c 文件,这些 C 函数是从 .cpp 文件中调用的,但是 .cppcode>.cpp 文件本身不使用 OpenMP.

I’m building an R package that has some .c files with code that uses OpenMP, and these C functions are called from .cpp files, but the .cpp files themselves don’t make any use of OpenMP.

例如cfile.c:

int parallel_function(double *x, int n)
{   
    int i;
    #pragma omp parallel for firstprivate(x, n)
    for (i = 0; i < n; i++){ x[i] *= 2; }
}

cppfile.cpp:

#include <Rcpp.h>
using namespace Rcpp;
extern "C" {
    int parallel_function(double *x, int n);
}
// [[Rcpp::export]]
void multiply_by_two(NumericVector x, int n){parallel_function(x.begin(), n);}

为了为 C 文件启用 OpenMP,我的第一个想法是按照 R 扩展手册构建一个 Makevars:

In order to enable OpenMP for the C files, my first though was to construct a Makevars like this, following the R extensions manual:

PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CFLAGS)

但这会给我带来以下问题:

But that will throw me the following:

Check: use of SHLIB_OPENMP_*FLAGS in Makefiles, Result: NOTE
    src/Makevars: SHLIB_OPENMP_CFLAGS is included in PKG_LIBS but linking is by C++
  Use of these macros is discussed in sect 1.2.1.1 of 'Writing R
  Extensions'. The macros for different languages may differ so the
  matching macro must be used in PKG_CXXFLAGS (etc) and match that used
  in PKG_LIBS (except for Fortran: see the manual).

那么,如果我改为尝试:

So then, if I try instead:

PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)

我会得到:

Check: use of SHLIB_OPENMP_*FLAGS in Makefiles, Result: NOTE
    src/Makevars: SHLIB_OPENMP_CFLAGS is included in PKG_CFLAGS but not in PKG_LIBS
    src/Makevars: SHLIB_OPENMP_CXXFLAGS is included in PKG_LIBS but not in PKG_CXXFLAGS
  Use of these macros is discussed in sect 1.2.1.1 of 'Writing R
  Extensions'. The macros for different languages may differ so the
  matching macro must be used in PKG_CXXFLAGS (etc) and match that used
  in PKG_LIBS (except for Fortran: see the manual).

即使我像这样使用两个标志:

Even if I use both flags like this:

PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS)
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(SHLIB_OPENMP_CFLAGS)

我会得到:

Check: use of SHLIB_OPENMP_*FLAGS in Makefiles, Result: NOTE
    src/Makevars: SHLIB_OPENMP_CFLAGS is included in PKG_LIBS but linking is by C++
    src/Makevars: it is not portable to include multiple SHLIB_OPENMP_*' macros in PKG_LIBS
  Use of these macros is discussed in sect 1.2.1.1 of 'Writing R
  Extensions'. The macros for different languages may differ so the
  matching macro must be used in PKG_CXXFLAGS (etc) and match that used
  in PKG_LIBS (except for Fortran: see the manual).

因此,由于 .cpp 代码不需要 omp 链接,我想知道链接 .c 的 正确和简约 方式是什么 文件,但不是 .cpp 文件.

So, since the .cpp code does not require omp linkage, I’m wondering what would be the correct and minimalist way of linking the .c files but not the .cpp files.

推荐答案

通常,我们可以在 R 包中混合 C 和 C++ 代码.

In general, we can mix C and C++ code in an R package.

一旦 OpenMP 进入,CRAN 现在似乎更喜欢一致使用 SHLIB_OPENMP_CFLAGS SHLIB_OPENMP_CXXFLAGS.

Once OpenMP enters, CRAN now seems to stronly prefer consistent use of either SHLIB_OPENMP_CFLAGS or SHLIB_OPENMP_CXXFLAGS.

现在,鉴于 g++ 将进入链接,您似乎应该始终使用 SHLIB_OPENMP_CXXFLAGS —— 这建议将您的 C 文件重命名为 C++ 文件.

Now, given that g++ will enter for linking, it would seem that you should use SHLIB_OPENMP_CXXFLAGS throughout -- which suggests renaming your C files to be C++ files.

这一切似乎有点过分,但这就是我会尝试的.而且 CRAN 检查通常都是有充分理由的,因此经验教会了大多数人与他们一起玩.

It all seems a little over the top, but that is what I would try. And the CRAN checks are usually all there for a good reason so experience has taught most of to play along with them.

这篇关于CRAN 可接受的链接到 OpenMP 的一些从 Rcpp 调用的 C 代码的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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