R package with C/C++ and openMP: how to make “Makevars” file under “mypackage/src/” folder?

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I am developing an R package on Mac OSX with some low level C/C++ code and openMP support. The C++ code is written using Rcpp package. My global ''Makevars'' file is placed under ~/.R/ folder. The file looks like following.

CC=clang-omp CXX=clang-omp++  PKG_CFLAGS=Wall -pedantic PKG_CFLAGS= -fopenmp PKG_CXXFLAGS= -fopenmp PKG_LIBS= -fopenmp -lgomp 

Everything works great under this configuration!

However, now I want to build package-specific Makevars file for its own compilation to make the package portable. What I tried was simply move the global Makevars file into my R pakcage src folder. However, the compiler complained about that it cannot find the openMP header file omp.h:

** libs clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/bigmemory/include" -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/BH/include"  -fopenmp -fPIC  -Wall -mtune=core2 -g -O2  -c RcppExports.cpp -o RcppExports.o RcppExports.cpp:12:10: fatal error: 'omp.h' file not found #include           ^ 1 error generated. make: *** [RcppExports.o] Error 1 clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/bigmemory/include" -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/BH/include"  -fopenmp -fPIC  -Wall -mtune=core2 -g -O2  -c RcppExports.cpp -o RcppExports.o RcppExports.cpp:12:10: fatal error: 'omp.h' file not found #include           ^ 1 error generated. make: *** [RcppExports.o] Error 1 

As you can see, the compilers become clang and clang++, but not what specified in the Makevars files: CC=clang-omp and CXX=clang-omp++.

Question 1: So how could I fix this issue and build a Makevars file within the R package?

Another thing is that, I noticed from Writing R extensions that,

For example, a package with C code written for OpenMP should have in src/Makevars the lines  PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS) PKG_LIBS = $(SHLIB_OPENMP_CFLAGS) 

Question 2: What is the difference between, for example, macro $(SHLIB_OPENMP_CFLAGS) and flag -fopenmp? which one under which circumstance should I use? I tried to replace the flags with the macros, but still cannot fix the issue.

回答1:

Regarding question, my favourite approach is to copy from working packages. Here is eg the part from (recommended / Core) package mgcv:

PKG_LIBS =  $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) $(SHLIB_OPENMP_CFLAGS) PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS) 

I use the same snippet in the smaller winsorize package (on GitHub) by myself and Andreas.

Regarding question 2: The first form is more general and would allow other OpenMP implementations. It uses what R found to be useable when it was configured.



回答2:

It sounds like you need the package Makevars as Dirk describes; for your local environment, have ~/.R/Makevars setting your C and C++ compilers to your OpenMP enabled versions using CC and CXX.

Your package (if destined for CRAN, and indeed any Mac R users who haven't battled to install an OpenMP version of clang) will need to work without OpenMP, so your code and compiler flags probably shouldn't assume its presence.



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