简体   繁体   English

在 Mac OS X 10.11 上安装 OpenMP

[英]Installing OpenMP on Mac OS X 10.11

How can I get OpenMP to run on Mac OSX 10.11, so that I can execute scripts via terminal ?如何让 OpenMP 在 Mac OSX 10.11 上运行,以便我可以通过终端执行脚本?

I have installed OpenMP: brew install clang-omp .我已经安装了 OpenMP: brew install clang-omp

When I run, for example: gcc -fopenmp -o Parallel.b Parallel.c the following expression returns: fatal error: 'omp.h' file not found例如,当我运行时: gcc -fopenmp -o Parallel.b Parallel.c以下表达式返回: fatal error: 'omp.h' file not found

I have also tried: brew install gcc --without-multilib but unfortunately this eventually returned the following (after first installing some dependencies):我也试过: brew install gcc --without-multilib但不幸的是这最终返回了以下内容(在首先安装一些依赖项之后):

The requested URL returned error: 404 Not Found
Error: Failed to download resource "mpfr--patch"

Any recommended work arounds?任何推荐的解决方法?

On a mac, the command gcc is a symlink to Clang.在 Mac 上,命令 gcc 是 Clang 的符号链接。 So by calling gcc -fopenmp -o your_program your_program.c you are in fact using Clang, which until now has not had built-in support for OpenMP.因此,通过调用gcc -fopenmp -o your_program your_program.c您实际上是在使用 Clang,它直到现在还没有对 OpenMP 的内置支持。

The newer versions of Clang do have support for OpenMP according to this post (where you can also find instructions on how to set it up).根据这篇文章,较新版本的 Clang 确实支持 OpenMP(您还可以在其中找到有关如何设置它的说明)。

On the other hand, if you still want to use gcc I can guide you through the steps that worked for me.另一方面,如果您仍然想使用 gcc,我可以指导您完成对我有用的步骤。

  1. Install gcc with brew.用 brew 安装 gcc。 The command you used should work: brew install gcc --without-multilib您使用的命令应该可以工作: brew install gcc --without-multilib

    Alternatively, if brew says that you already have gcc installed you can try brew reinstall gcc --without-multilib或者,如果 brew 说您已经安装了 gcc,您可以尝试brew reinstall gcc --without-multilib

    As you may have noted, if you don't specify --without-multilib brew warns you that OpenMP may not work.您可能已经注意到,如果您不指定--without-multilib brew 会警告您 OpenMP 可能无法工作。

  2. Find the location of the newly installed gcc.找到新安装的 gcc 的位置。 Brew appends the version number to gcc so that it does not conflict with the one installed by Command Line Tools. Brew 将版本号附加到 gcc 以便它不会与命令行工具安装的版本号冲突。 You will find the symlink in usr/local/bin .您将在usr/local/bin找到符号链接。 In my case it's usr/local/bin/gcc-5 .就我而言,它是usr/local/bin/gcc-5 If you right-click and chose "Show original" it should show the gcc-5 executable in /usr/local/Cellar/gcc/5.3.0/bin/gcc-5 (version numbers may differ).如果您右键单击并选择“显示原件”,它应该会在/usr/local/Cellar/gcc/5.3.0/bin/gcc-5显示 gcc-5 可执行文件(版本号可能不同)。

  3. Now you need to tell your system about it.现在你需要告诉你的系统。 When calling a compiler your bash will look into /usr/bin by default and not in /usr/local/bin .调用编译器时,您的 bash 将默认查看/usr/bin而不是/usr/local/bin You need to add this directory to your $PATH .您需要将此目录添加到您的$PATH This can be easily done with the command:这可以使用以下命令轻松完成:

    PATH=/usr/local/bin:$PATH

  4. Now you should be able to compile with OpenMP enabled using:现在,您应该能够使用以下命令在启用 OpenMP 的情况下进行编译:

    gcc-5 -fopenmp -o your_program your_program.c

    Remark: gcc-5 is the version I have installed, yours might differ.备注:gcc-5 是我安装的版本,你的可能会有所不同。

  • install clang-omp安装 clang-omp

     brew install clang-omp
  • make sure you xcode command line tool确保您使用 xcode 命令行工具

    xcode-select --install
  • I actually had one error while running a sample openmp code我实际上在运行示例 openmp 代码时遇到了一个错误

    /usr/local/opt/libiomp/include/libiomp/omp.h:139:21: error: expected ';' after top level declarator extern void __ KAI_KMPC_CONVENTION kmp_set_stacksize_s (size_t);
  • Just remove one space that is present between __ and KAI from the file只需从文件中删除 __ 和 KAI 之间的一个空格

  • Now use the command现在使用命令

    clang-omp -fopenmp helloopenmp.c

    and run the following code并运行以下代码

    #include <omp.h> #include <stdio.h> int main() { #pragma omp parallel printf("Hello from thread %d, nthreads %d\\n", omp_get_thread_num(), omp_get_num_threads()); }
  • You should get output similar to this你应该得到类似的输出

    Hello from thread 3, nthreads 4 Hello from thread 2, nthreads 4 Hello from thread 0, nthreads 4 Hello from thread 1, nthreads 4
  • Worked on OS X 10.11.3 and with brew update dated 18th Feb 2016在 OS X 10.11.3 和 2016 年 2 月 18 日的 brew 更新上工作

macOS High Sierra Version 10.13.6 (17G65) macOS High Sierra 版本 10.13.6 (17G65)

1. Install gcc 1.安装gcc

brew reinstall gcc --without-multilib

2. Compile 2.编译

gcc-8 -Wall -fopenmp your-parallel-program.c

Notice the gcc-8 watch the version brew installs, yours may be gcc-7 or gcc-9注意gcc-8观察 brew 安装的版本,你的可能是gcc-7gcc-9

==> Pouring gcc-8.2.0.high_sierra.bottle.1.tar.gz 🍺 /usr/local/Cellar/gcc/8.2.0: 1,495 files, 344.8MB

That's it!就是这样!

By default, GCC is using clang distribution.默认情况下,GCC 使用 clang 发行版。 Install GCC with Homebrew .使用 Homebrew 安装 GCC Once installed, you can compile the code specifying the GCC version installed by Homebrew and adding the OpenMP flag .安装后,您可以编译代码,指定Homebrew 安装的 GCC 版本添加 OpenMP 标志 A hello_world example is shown below: hello_world 示例如下所示:

  1. brew install gcc酿造安装gcc

  2. brew install libomp brew 安装 libomp

  3. brew info gcc酿造信息gcc

    gcc: stable 10.2.0 (bottled), HEAD gcc:稳定版10.2.0 (瓶装),HEAD

    GNU compiler collection GNU 编译器合集

  4. export OMP_NUM_THREADS=8导出 OMP_NUM_THREADS=8

  5. create hello.c创建 hello.c

#include <stdio.h>
#include <omp.h>
int main(void){
    #pragma omp parallel 
    {
        printf("Hello World! \n");
    }

    return 0;
}
  1. gcc-10 -fopenmp hello.c -o hello gcc-10 -fopenmp hello.c -o hello
  2. Execute: ./hello执行:./hello
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
  1. Install gcc using brew.使用 brew 安装 gcc。

     brew install gcc
  2. Check gcc is installed.检查 gcc 是否已安装。

     $which g++-7 /usr/local/bin/g++-7
  3. Change cmake cxx compiler.更改 cmake cxx 编译器。

     cmake -DCMAKE_CXX_COMPILER=g++-7 make

Maybe this will help you.也许这会帮助你。

brew install cmake
brew install gcc --without-multilib
cmake -DCMAKE_CXX_COMPILER=g++-6 .. 
make -j 

After install gcc:安装 gcc 后:

brew install gcc --without-multilib

and export PATH:并导出路径:

export PATH=/usr/local/bin:$PATH

You may need to export CC, which works for me:您可能需要导出 CC,这对我有用:

export CC=/usr/local/bin/gcc

This maybe gcc-7, or whatever.这可能是 gcc-7 或其他。

base) evry-owned-address-192_68_80_x:Desktop luis$ gcc -fopenmp ejemplo.c -o ejemplo clang: error: unsupported option '-fopenmp' base) evry-owned-address-192_68_80_x:Desktop luis$ gcc -fopenmp ejemplo.c -o ejemplo clang: 错误: 不支持的选项“-fopenmp”

ami no me agarra en mi mac ami no me agarra en mi mac

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM