在OSXBigSur(编辑:和AppleM1)上安装R以与Rcpp和openMP一起使用
OSX+Rcpp+openMP 上可能有无数个线程,但现在的底线似乎是这个(percoatless):
不幸的是,在 R 4.0.0 中,R 的 CRAN 分布式版本失去了在没有自定义设置的情况下使用 OpenMP 的能力。
我遇到了其他想法,包括自己编译llvm,使用homebrew或macports安装 R 和/或 llvm 和/或 gcc,然后弄清楚如何使用正确的编译器和/或带有 (R)cpp 的标志。然而,我觉得这一切都非常令人困惑。
我不是 mac 用户,但在我看来,设置 mac 以使用 openMP 编译 Rcpp 包或代码片段对于大多数 mac 用户来说似乎太困难了。但是,我希望我在 github 上的 R 包被更多用户使用,并且由于它依赖于 openMP,我正在失去那些观众。
有人可以提供必要的步骤来在 mac 上设置 R 以使用 openMP 编译 Rcpp 代码吗?我想把它变成一个快速教程。
编辑:我应该添加 - 在 Apple Silicon 上,因为事情发生的地方有一些额外的混乱 - /usr/localvs/opt
回答
我花了一天的时间来解决这个问题(原帖在这里);以下是我使用 openMP 从源代码编译 R 包的步骤:
- 从应用商店安装 xcode(安装 xcode 的说明),然后从终端安装/重新安装 xcode 命令行工具:
# To delete an existing command line tools installation:
sudo rm -rf /Library/Developer/CommandLineTools
# To install the command line tools
sudo xcode-select --install
- 通过 Homebrew 安装 gcc(安装 Homebrew 的说明),或者,如果您已经安装了 gcc,请跳到第 3 步。
# WARNING: This can take several hours
brew install gcc
- 为避免“遗留”版本问题:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
- 将一些头文件链接到 /usr/local/include
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
# You can ignore warnings like this:
#ln: /usr/local/include//tcl.h: File exists
#ln: /usr/local/include//tclDecls.h: File exists
#ln: /usr/local/include//tclPlatDecls.h: File exists
#ln: /usr/local/include//tclTomMath.h: File exists
#ln: /usr/local/include//tclTomMathDecls.h: File exists
#ln: /usr/local/include//tk.h: File exists
#ln: /usr/local/include//tkDecls.h: File exists
#ln: /usr/local/include//tkPlatDecls.h: File exists
- 检查您的 gfortran (
cd /usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/; ls)版本,然后编辑您的~/.R/Makevars文件(如果您Makevars的~/.R/目录中没有调用的文件)并仅包含以下几行:
LOC = /usr/local/gfortran
CC=$(LOC)/bin/gcc -fopenmp
CXX=$(LOC)/bin/g++ -fopenmp
CXX11 = $(LOC)/bin/g++ -fopenmp
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
# (check that the version of gfortran - in this case 10.2.0 - matches the version specified in FLIBS)
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX1X=/usr/local/gfortran/bin/g++
CXX98=/usr/local/gfortran/bin/g++
CXX11=/usr/local/gfortran/bin/g++
CXX14=/usr/local/gfortran/bin/g++
CXX17=/usr/local/gfortran/bin/g++
- 打开 R 并安装一个包来测试它是否在启用 openMP 的情况下编译(当被问到时,从源代码编译=“是”):
install.packages("data.table")
不幸的是,我不相信存在更“简单”的设置。
THE END
二维码