在M2 Mac上使用OpenMP与R和data.table

0mkxixxg  于 2023-06-03  发布在  Mac
关注(0)|答案(1)|浏览(309)

我正在努力在运行macOS Ventura 13.3.1的M2 Mac上使用R和data.table的OpenMP。我正在按照https://mac.r-project.org/openmp/上的说明操作。任何帮助将不胜感激。
总结如下:

  1. OpenMP似乎已正确安装
    1.我已经按照说明更新了~.R/Makevars
  2. ~.R/Makevars似乎没有在R中加载
  3. R会话信息

OpenMP似乎安装正确

我相信我已经按照以下输出所建议的说明成功安装了OpenMP:

file.exists(
  "/usr/local/lib/libomp.dylib",
  "/usr/local/include/ompt.h",
  "/usr/local/include/omp.h",
  "/usr/local/include/omp-tools.h"
  )
# TRUE TRUE TRUE TRUE

当我尝试编译下面的openmp_test.c

#include <stdio.h>
#include <omp.h>

int main() {
    #pragma omp parallel
    {
        int thread_id = omp_get_thread_num();
        printf("Hello from thread %d\n", thread_id);
    }
    return 0;
}

关于clang -Xclang -fopenmp -lomp openmp_test.c -o openmp_test
我可以用./openmp_test运行程序并获得输出

Hello from thread 2
Hello from thread 0
Hello from thread 3
Hello from thread 8
Hello from thread 1
Hello from thread 4
Hello from thread 11
Hello from thread 7
Hello from thread 10
Hello from thread 6
Hello from thread 9
Hello from thread 5

我已经按照说明更新了~.R/makevars

readLines("~/.R/Makevars")
# [1] "CPPFLAGS += -Xclang -fopenmp"
# [2] "LDFLAGS += -lomp"            
# [3] "TEST = test1234"

在R中似乎没有加载~.R/Makevars

我不确定~.R/Makevars是如何工作的,但似乎没有在R中加载这些环境变量:

Sys.getenv("CPPFLAGS")
# [1] ""
Sys.getenv("LDFLAGS")
# [1] ""
Sys.getenv("TEST")
# [1] ""

在shell中,

ls -al ~/.R/Makevars
# -rw-------  1 chandler  62 May 31 07:08 /Users/chandler/.R/Makevars

我也试过更改文件的权限

chmod 600 ~/.R/Makevars

我尝试将我的.Renviron更新为

R_MAKEVARS_USER=~/.R/Makevars

现在

Sys.getenv("R_MAKEVARS_USER")
# [1] "~/.R/Makevars"

我不确定预期的输出,但这些方法似乎都不能解决这个问题。

R SessionInfo

R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/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: America/Denver
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices
[4] utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] compiler_4.3.0
kmynzznz

kmynzznz1#

我最终按照这里的说明操作,并能够使用OpenMP获得data.table:
https://github.com/Rdatatable/data.table/issues/5419#issuecomment-1465023581
对于data.table安装,我的~/.R/Makevars文件看起来像这样:

LDFLAGS += -L/opt/homebrew/opt/libomp/lib -lomp
CPPFLAGS += -I/opt/homebrew/opt/libomp/include -Xclang -fopenmp

为了让其他软件包与OpenMP一起工作,我安装了GNU Fortran编译器,如下所示:https://mac.r-project.org/tools/
要安装fst包,我首先必须取消注解掉~/.R/Makevars中的CPPFLAGS行,并在此处添加fst的行。
我的~/.R/Makevars看起来像这样:

LDFLAGS += -L/opt/homebrew/opt/libomp/lib -lomp
## CPPFLAGS += -I/opt/homebrew/opt/libomp/include -Xclang -fopenmp

CXX1X=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX98=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX11=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX14=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX17=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp

然后我通过在终端中调用R并使用install.packages("fstcore", type="source")install.packages(“fst”,type=“source”)安装了fst包。 我同样安装了fixest包:install.packages("fixest", type="source")我删除了CPPFLAGS前面的注解,所以最终的~/.R/Makevars`看起来像这样:

LDFLAGS += -L/opt/homebrew/opt/libomp/lib -lomp
CPPFLAGS += -I/opt/homebrew/opt/libomp/include -Xclang -fopenmp

CXX1X=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX98=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX11=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX14=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp
CXX17=/opt/homebrew/Cellar/gcc/13.1.0/bin/g++-13 -fopenmp

最后,一切似乎都奏效了:

library(data.table); library(fst); library(fixest);
data.table::getDTthreads()
# [1] 6
fst::threads_fst()
# [1] 12
fixest::getFixest_nthreads()
# [1] 6

对我来说,在~/.R/Makevars

CPPFLAGS += -I/opt/homebrew/opt/libomp/include -Xclang -fopenmp

必须安装data.table,但需要注解掉才能安装其他也依赖OpenMP的软件包

相关问题