简体   繁体   English

在 OSX Big Sur(编辑:和 Apple M1)上安装 R 以与 Rcpp 和 openMP 一起使用

[英]Installing R on OSX Big Sur (EDIT: and Apple M1) for use with Rcpp and openMP

There's probably a gazillion threads on OSX+Rcpp+openMP, but the bottom line right now appears to be this (per coatless ): OSX+Rcpp+openMP 上可能有无数个线程,但现在的底线似乎是这个( percoatless ):

Unfortunately, with R 4.0.0 the CRAN distributed version of R loses the ability to use OpenMP without a custom setup.不幸的是,在 R 4.0.0 中,R 的 CRAN 分布式版本失去了在没有自定义设置的情况下使用 OpenMP 的能力。

I came across other ideas, including compiling llvm yourself, using homebrew or macports to install R and/or llvm and/or gcc, and then figuring out how to use the right compiler and/or flags with (R)cpp.我遇到了其他想法,包括自己编译llvm ,使用homebrewmacports安装 R 和/或 llvm 和/或 gcc,然后弄清楚如何使用正确的编译器和/或带有 (R)cpp 的标志。 However, I find this all very confusing.然而,我觉得这一切都非常令人困惑。

I am not a mac user, but it seems to me that setting up a mac to compile Rcpp packages or code snippets with openMP seems to be too difficult for most mac users.我不是 mac 用户,但在我看来,设置 mac 以使用 openMP 编译 Rcpp 包或代码片段对于大多数 mac 用户来说似乎太困难了。 However, I would like my R package on github to be used by more users, and since it relies on openMP, I am losing that audience.但是,我希望我在 github 上的 R 包被更多用户使用,而且由于它依赖于 openMP,我正在失去那些观众。

Can someone provide the necessary steps to set up R on mac in a way that it can compile Rcpp code with openMP?有人可以提供必要的步骤来在 mac 上设置 R 以使用 openMP 编译 Rcpp 代码吗? I'd like to turn that into a quick tutorial.我想把它变成一个快速教程。

EDIT: I should have added - on Apple Silicon, because there are some extra confusions where things go - /usr/local vs /opt编辑:我应该添加 - 在 Apple Silicon 上,因为有一些额外的混乱 - /usr/local vs /opt

I spent a day figuring this out (original post here );我花了一天的时间来解决这个问题(原帖在这里); here are the steps I used to compile R packages from source with openMP:以下是我使用 openMP 从源代码编译 R 包的步骤:

  1. Install xcode from the app store ( instructions for installing xcode ) then install/reinstall the xcode command line tools from the terminal:从应用商店安装 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
  1. Install gcc via Homebrew ( instructions for installing Homebrew ) or, if you already have gcc installed, skip to step 3.通过 Homebrew 安装 gcc(安装 Homebrew 的说明),或者,如果您已经安装了 gcc,请跳到第 3 步。
# WARNING: This can take several hours
brew install gcc
  1. To avoid "legacy" version issues:为避免“遗留”版本问题:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
  1. Link some headers into /usr/local/include将一些头文件链接到 /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
  1. Check your version of gfortran ( cd /usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/; ls ) then edit your ~/.R/Makevars file (if you don't have a file called Makevars in your ~/.R/ directory) and include only these lines:检查您的 gfortran 版本( cd /usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/; ls )然后编辑您的~/.R/Makevars文件(如果您的文件中没有名为Makevars的文件) ~/.R/目录)并仅包含以下~/.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++
  1. Open R and install a package to test that it compiles with openMP enabled (when asked, compile from source = "Yes"):打开 R 并安装一个包来测试它是否在启用 openMP 的情况下编译(当被问到时,从源代码编译=“是”):
install.packages("data.table")

Unfortunately, I do not believe a more "simple" setup exists.不幸的是,我不相信存在更“简单”的设置。

Eventually, I found a process that works on a M1 mac with Big Sur.最终,我找到了一个可以在带有 Big Sur 的M1 mac上运行的过程。

  • Head over to https://mac.r-project.org/ , it contains most things you will need前往https://mac.r-project.org/ ,它包含您需要的大部分内容
  • Download and install R via R-4.1-branch.pkg .通过R-4.1-branch.pkg下载并安装 R。 The CRAN version might also work, but I used the installer from mac.r-project.org, which required opening the osx security settings to allow the installation. CRAN 版本也可能工作,但我使用了来自 mac.r-project.org 的安装程序,这需要打开osx 安全设置以允许安装。
  • Install RStudio , start it, and let it install the developer tools .安装 RStudio ,启动它,让它安装开发者工具 Alternatively, run sudo xcode-select --install in Terminal.或者,在终端中运行sudo xcode-select --install
  • Head to https://mac.r-project.org/openmp/ .前往https://mac.r-project.org/openmp/ Download openmp-11.0.1-darwin20-Release.tar.gz and install it (see Terminal commands below).下载openmp-11.0.1-darwin20-Release.tar.gz并安装它(参见下面的终端命令)。
curl -O https://mac.r-project.org/openmp/openmp-11.0.1-darwin20-Release.tar.gz
sudo tar fvx openmp-11.0.1-darwin20-Release.tar.gz -C /
  • Now we need to add compiler flags so that clan uses openMP.现在我们需要添加编译器标志,以便氏族使用 openMP。 In Terminal, create the Makevars file.在终端中,创建Makevars文件。
cd ~
mkdir .R
nano .R/Makevars

in nano, paste these additional compiler flags into the Makevars file:在 nano 中,将这些额外的编译器标志粘贴到Makevars文件中:

CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp

Hit Control+O, Control+X to save and close按 Control+O、Control+X 保存并关闭

  • Head over to the gfortran page: https://github.com/fxcoudert/gfortran-for-macOS/releases前往 gfortran 页面: https : //github.com/fxcoudert/gfortran-for-macOS/releases
  • Use the installer gfortran-ARM-11.0-BigSur.pkg to install gfortran .使用安装程序gfortran-ARM-11.0-BigSur.pkg安装 gfortran
  • For some reason it appears to install in /usr/local/gfortran , but R expects it in /opt .出于某种原因,它似乎安装在/usr/local/gfortran ,但 R 期望它安装在/opt The mac-R team likes to separate arm64 and intel related files. mac-R 团队喜欢将 arm64 和 intel 相关文件分开。 We could go and fix paths, or simply also install gfortran under /opt .我们可以去修复路径,或者简单地在/opt下安装 gfortran。 Download the tar file gfortran-ARM-11.0-BigSur.tar.xz .下载 tar 文件gfortran-ARM-11.0-BigSur.tar.xz You can use curl, or just download it and point tar in the command line to it.您可以使用 curl,或者只是下载它并在命令行中将 tar 指向它。
cd /opt/R/arm64/
sudo mkdir gfortran
sudo tar -xzyf gfortran-ARM-11.0-BigSur.tar.xz -C /opt/R/arm64/

(replace gfortran-ARM-11.0-BigSur.tar.xz with /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz ) (用gfortran-ARM-11.0-BigSur.tar.xz替换gfortran-ARM-11.0-BigSur.tar.xz /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz

Now it should work.现在它应该可以工作了。

Not an expert in OSX, but doing this so others can figure out how to use my R package.不是 OSX 专家,但这样做是为了让其他人可以弄清楚如何使用我的 R 包。 I'd like to streamline the process some more, but wiping the mac, reinstalling osx and testing it takes so much time.我想进一步简化这个过程,但是擦拭 mac、重新安装 osx 并测试它需要很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在带有 Apple M1 芯片的 MacBook 上安装 gfortran 以用于 R - Installing gfortran on MacBook with Apple M1 chip for use in R R 包“马赛克”无法在 MacOS Big Sur M1 上安装 - R package 'mosaic' fails to install on MacOS Big Sur M1 在 Mac M1(Big Sur,Monterey)上为 Rcpp 和其他工具配置编译器 - Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools MacOS Big Sur M1 芯片,R devtool monocle3 安装过程中出现 cmath 错误 - MacOS Big Sur M1 silicon, cmath error during R devtool monocle3 install 无法在 R(大苏尔)中安装 package“sf” - Trouble installing package "sf" in R (Big Sur) 使用 Apple M1 芯片从源安装特定的 R 包导致存储库索引或 -lquadmath 错误 - Installing specific R packages from source with Apple M1 chip causing repository index or -lquadmath error 在OSX Yosemite上使用intel composer编译的R上安装Rcpp - installing Rcpp on R compiled with intel composer on OSX Yosemite 在 M1 mac 上安装 R package “nloptr”时出现问题 - Problems installing R package "nloptr" on M1 mac 是否可以在不安装Rtools的情况下在R中使用Rcpp代码? - Can I use the Rcpp code in R without installing Rtools? 我正在尝试在运行 macOS Big Sur 的 r 中安装包 glmnet 但它不起作用 - I'm trying to install package glmnet in r running macOS Big Sur but it's not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM