问题背景
由于Mac自带的c++编译器不支持-fopenmp,我参考这个网站的建议,创建了~/.R/Makevars
文件,并写入了
# Newly installed Homebrew is located in
# - /opt/homebrew for ARM Macs (M1 and its successors)
# - /usr/local for Intel Macs
HOMEBREW_LOC=/opt/homebrew
# If you downloaded llvm manually above, replace with your chosen NEW_PATH/clang
LLVM_LOC=$(HOMEBREW_LOC)/opt/llvm
CC=$(LLVM_LOC)/bin/clang -fopenmp
CXX=$(LLVM_LOC)/bin/clang++ -fopenmp
# -O3 should be faster than -O2 (default) level optimisation ..
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(HOMEBREW_LOC)/opt/gettext/lib -L$(LLVM_LOC)/lib -Wl,-rpath,$(LLVM_LOC)/lib
CPPFLAGS=-I$(HOMEBREW_LOC)/opt/gettext/include -I$(LLVM_LOC)/include
CPPFLAGS=-I$(HOMEBREW_LOC)/opt/gettext/include -I$(LLVM_LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
从而能够使用支持-fopenmp的编译器
问题描述
在配置完成后,我的Cpp file能够成功编译,成功Source,并且能够在R语言中正常调用相应的函数。
但是R studio一直提示一些报错,比如
我猜测产生这些报错的源头应该是Rstudio没法识别Rcpp里头的这些关键字,然后我发现重新安装Rcpp包能够使得这些报错消失,但是一旦我重启R session,这些报错会重新出现(再次安装Rcpp还是可以使其消失),但是实际上代码能够正常编译并运行。
此外,如果我删除所有~/.R/Makevars
文件中的内容,代码也是不会有任何报错的。但是为了能够使用openmp(不是在这个file里),我感觉还是需要这样的配置。
总之,想请问一下大家,有没有办法能够彻底解决这个报错问题,而不用每次打开Rstudio都安装一遍Rcpp。
代码
虽然代码应该没有什么问题,但是也可以放上来参考一下。
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace arma;
// [[Rcpp::export]]
vec splineRegression_direct(vec Y, mat X,
double lambda, mat Omega){
vec beta;
beta = solve(X.t() * X + lambda * Omega ,X.t() * Y);
return beta;
}
// [[Rcpp::export]]
vec splineRegression_fast(vec Y, vec alpha, vec beta,
mat U, mat Z, double lambda){
int p = beta.n_elem;
uvec ind = regspace<uvec>(0, p - 1);
vec betahat;
mat Zbeta;
mat Ut = U.t();
vec alphaSub = alpha(ind);
vec nums = alphaSub/(alphaSub % alphaSub + lambda* beta % beta);
Zbeta = Ut.rows(ind) * Y % nums;
betahat = solve(Z,Zbeta);
return betahat;
}
系统环境
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin23.0.0 (64-bit)
Running under: macOS Sonoma 14.0
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /opt/homebrew/Cellar/r/4.3.2/lib/R/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] geigen_2.3 RcppArmadillo_0.12.6.6.0 Rcpp_1.0.11
loaded via a namespace (and not attached):
[1] compiler_4.3.2 tools_4.3.2