簡體   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

OSX+Rcpp+openMP 上可能有無數個線程,但現在的底線似乎是這個( percoatless ):

不幸的是,在 R 4.0.0 中,R 的 CRAN 分布式版本失去了在沒有自定義設置的情況下使用 OpenMP 的能力。

我遇到了其他想法,包括自己編譯llvm ,使用homebrewmacports安裝 R 和/或 llvm 和/或 gcc,然后弄清楚如何使用正確的編譯器和/或帶有 (R)cpp 的標志。 然而,我覺得這一切都非常令人困惑。

我不是 mac 用戶,但在我看來,設置 mac 以使用 openMP 編譯 Rcpp 包或代碼片段對於大多數 mac 用戶來說似乎太困難了。 但是,我希望我在 github 上的 R 包被更多用戶使用,而且由於它依賴於 openMP,我正在失去那些觀眾。

有人可以提供必要的步驟來在 mac 上設置 R 以使用 openMP 編譯 Rcpp 代碼嗎? 我想把它變成一個快速教程。

編輯:我應該添加 - 在 Apple Silicon 上,因為有一些額外的混亂 - /usr/local vs /opt

我花了一天的時間來解決這個問題(原帖在這里); 以下是我使用 openMP 從源代碼編譯 R 包的步驟:

  1. 從應用商店安裝 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. 通過 Homebrew 安裝 gcc(安裝 Homebrew 的說明),或者,如果您已經安裝了 gcc,請跳到第 3 步。
# WARNING: This can take several hours
brew install gcc
  1. 為避免“遺留”版本問題:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
  1. 將一些頭文件鏈接到 /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. 檢查您的 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. 打開 R 並安裝一個包來測試它是否在啟用 openMP 的情況下編譯(當被問到時,從源代碼編譯=“是”):
install.packages("data.table")

不幸的是,我不相信存在更“簡單”的設置。

最終,我找到了一個可以在帶有 Big Sur 的M1 mac上運行的過程。

  • 前往https://mac.r-project.org/ ,它包含您需要的大部分內容
  • 通過R-4.1-branch.pkg下載並安裝 R。 CRAN 版本也可能工作,但我使用了來自 mac.r-project.org 的安裝程序,這需要打開osx 安全設置以允許安裝。
  • 安裝 RStudio ,啟動它,讓它安裝開發者工具 或者,在終端中運行sudo xcode-select --install
  • 前往https://mac.r-project.org/openmp/ 下載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 /
  • 現在我們需要添加編譯器標志,以便氏族使用 openMP。 在終端中,創建Makevars文件。
cd ~
mkdir .R
nano .R/Makevars

在 nano 中,將這些額外的編譯器標志粘貼到Makevars文件中:

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

按 Control+O、Control+X 保存並關閉

  • 前往 gfortran 頁面: https : //github.com/fxcoudert/gfortran-for-macOS/releases
  • 使用安裝程序gfortran-ARM-11.0-BigSur.pkg安裝 gfortran
  • 出於某種原因,它似乎安裝在/usr/local/gfortran ,但 R 期望它安裝在/opt mac-R 團隊喜歡將 arm64 和 intel 相關文件分開。 我們可以去修復路徑,或者簡單地在/opt下安裝 gfortran。 下載 tar 文件gfortran-ARM-11.0-BigSur.tar.xz 您可以使用 curl,或者只是下載它並在命令行中將 tar 指向它。
cd /opt/R/arm64/
sudo mkdir gfortran
sudo tar -xzyf gfortran-ARM-11.0-BigSur.tar.xz -C /opt/R/arm64/

(用gfortran-ARM-11.0-BigSur.tar.xz替換gfortran-ARM-11.0-BigSur.tar.xz /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz

現在它應該可以工作了。

不是 OSX 專家,但這樣做是為了讓其他人可以弄清楚如何使用我的 R 包。 我想進一步簡化這個過程,但是擦拭 mac、重新安裝 osx 並測試它需要很多時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM