OpenMP - Mac M1 gcc和libomp不工作

fv2wmkja  于 2023-01-09  发布在  Mac
关注(0)|答案(1)|浏览(309)

我需要为我的课程设置OpenMP,但我对C & C还是新手
到目前为止,我一直在使用苹果的内置Clang和GCC编译器,
我以为这将有开箱即用的OpenMP支持。
我在这里读到一些答案,但它们要么不完整,要么我觉得它们非常混乱
我安装了llvm,但我不知道这有什么意义,
我按照说明将其添加到路径中,但仍然没有任何区别。
在支持OpenMP的Mac M1上设置C/C
环境的最佳方法是什么?
下面是基本程序:

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

#define THREADS 8
int main()
{
    int tid, nthreads;

    omp_set_num_threads(THREADS);

    // start of parallel section
    // Fork a team of threads with each thread having a private tid variable
    #pragma omp parallel private(tid)
    {
        tid=omp_get_thread_num();
        printf("Hello world from thread %d\n", tid);
        /* Only master thread does this */
        if (tid == 0) {
            nthreads = omp_get_num_threads();
            printf("Number of threads = %d\n", nthreads);
        }

    }//end of parallel section
    // All threads join master thread and terminate

    return 0;
}  // end main()

我还做了:

brew install libomp

这很好用,但是我应该如何让文件中的OpenMP工作呢?似乎没有任何进一步的细节
我看过这个视频,我想她是讲西班牙语,虽然我不能理解正在说什么,我跟着它,我没有得到新的GCC已经安装:https://www.youtube.com/watch?v=54S0tw0UrUg
我已经下载了gcc,但它仍然显示相同的苹果叮当声:

gcc -v
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: arm64-apple-darwin21.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

我已经设法安装了gcc和libomp
当我运行程序时,我得到了这个错误:

Undefined symbols for architecture arm64:
  "_omp_get_num_threads", referenced from:
      _main in ccK3z6BU.o
  "_omp_get_thread_num", referenced from:
      _main in ccK3z6BU.o
  "_omp_set_num_threads", referenced from:
      _main in ccK3z6BU.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
nhn9ugyo

nhn9ugyo1#

有类似的问题后,安装与brew在MacBook空气与M1芯片。运行g++安装与homebrew而不是默认的一个解决了我。

  • 酿酒公司

g++的完整路径为

  • /opt/自制/酒窖/gcc/12.2.0/bin/克++-12

现在我可以像这样用-fopenmp编译了:

  • /opt/自制程序/Cellar/gcc/12.2.0/bin/g++-12 -打开mp编译指示_测试. cpp

示例程序:

#include <stdio.h>
#include <iostream>
#include "/opt/homebrew/Cellar/libomp/15.0.6/include/omp.h"

int main() {
    #pragma omp parallel
    printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}

它的工作原理是:

Hello from thread 1, nthreads 8
Hello from thread 6, nthreads 8
Hello from thread 2, nthreads 8
Hello from thread 5, nthreads 8
Hello from thread 0, nthreads 8
Hello from thread 4, nthreads 8
Hello from thread 3, nthreads 8
Hello from thread 7, nthreads 8

相关问题