我正在构建一个R包,其中包含一些.c
文件,这些文件的代码使用OpenMP,并且从.cpp
文件中调用了这些C函数,但是.cpp
文件本身却没有不要使用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
:
PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS)
PKG_LIBS = $(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++
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).
那么,如果我改用
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).
即使我同时使用两个标志:
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
个文件。
答案 0 :(得分:3)
通常,我们可以在R包中混合使用C和C ++代码。
一旦OpenMP进入,CRAN现在似乎更加倾向于一致地使用SHLIB_OPENMP_CFLAGS
或 SHLIB_OPENMP_CXXFLAGS
。
现在,假设将输入g++
进行链接,似乎您应该始终使用SHLIB_OPENMP_CXXFLAGS
-这建议将C文件重命名为C ++文件。
一切似乎都有些过分,但这就是我会尝试的。而且CRAN检查通常都是有充分理由的,因此经验教训使大多数人都可以一起使用。