如何在 Mac M1 上安装 OpenMP?

Thé*_*éoS 8 c macos openmp apple-m1

我目前正在学习成为一名计算机工程师,我需要使用 OpenMP。经过一番研究,我在安装它时仍然遇到问题(#include <omp.h>仍然无法识别)。我尝试了 libomp 和 llvm (使用 Homebrew),但我一定是在这个过程中犯了一个错误。有人能够在 mac M1 上使用 OpenMP 吗?

小智 7

在 macOS 13.2.1 和最新的 Xcode 命令行工具集上,在 M2 芯片上,我可以使用基于 Homebrew 中的 libomp 的 OpenMP (brew install libomp),但使用 Apple 提供的 clang,通过运行:

clang -Xclang -fopenmp -L/opt/homebrew/opt/libomp/lib -I/opt/homebrew/opt/libomp/include -lomp omptest.c -o omptest 
Run Code Online (Sandbox Code Playgroud)

其中 omptest.c 给出为:

clang -Xclang -fopenmp -L/opt/homebrew/opt/libomp/lib -I/opt/homebrew/opt/libomp/include -lomp omptest.c -o omptest 
Run Code Online (Sandbox Code Playgroud)

总而言之,如果您不喜欢,则无需从 Homebrew 安装完整的 LLVM 或 GCC。只需要 libomp,您就可以开始了!

附言。在我的机器(M2 Max)上运行 omptest 的输出是:

./omptest 
Hello World... from thread = 0
Hello World... from thread = 8
Hello World... from thread = 4
Hello World... from thread = 2
Hello World... from thread = 3
Hello World... from thread = 11
Hello World... from thread = 1
Hello World... from thread = 10
Hello World... from thread = 7
Hello World... from thread = 9
Hello World... from thread = 6
Hello World... from thread = 5
Run Code Online (Sandbox Code Playgroud)