OpenMP -Mac M1 GCC和Libomp无法正常工作

发布于 2025-02-09 13:49:32 字数 2105 浏览 1 评论 0原文

我需要为我的课程设置OpenMP,但我仍然是C& 到目前为止,

我一直在使用Apple内置的Clang和GCC编译器,

我认为这将支持OpenMP开箱即用。

我在这里阅读了一些答案,但它们要么不完整,要么发现它们非常混乱,

我安装了LLVM,但是我不确定这是什么意义,

我遵循说明并将其添加到我的路上,但仍然没有不同之处。

在Mac M1上设置C/C ++环境的最佳方法是支持OpenMP的?

这是基本程序:

#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()

”我将其添加到路径变量并检查.zshrc“

我也做过:

brew install libomp

哪个工作正常,但是我应该如何将openMP在文件中工作? 我看过这段视频的任何地方似乎都没有任何其他细节

,我认为她在说西班牙语,尽管我不明白说什么,但我遵循了它,但我没有安装新的GCC:< a href =“ https://www.youtube.com/watch?v=54S0TW0URUG” rel =“ nofollow noreferrer”> 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

我设法安装了海湾室高管以及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

I need to setup OpenMP for my coursework and I am still new to C & C++

So far I have been using Apple's built in Clang and GCC compilers,

I assumed this would have support for OpenMP out of the box.

I have read a few answers here but they are either incomplete or I find them very confusing

I installed llvm, but Im not sure what's the point of this,

I followed the instructions and added it to my path but it still doesn't make a difference.

What's the best way to setup C/C++ environment on Mac M1, that has support for OpenMP?

Here is the basic program:

#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()

I added that to the path variable and checked .zshrc

I have also done:

brew install libomp

Which works fine, but how am I supposed to get the OpenMP in the file to work?
There doesn't seem to be any further details anywhere

I have watched this video, I assume She is speaking Spanish, although I can't understand what is being said, I followed it and I am not getting the new gcc already installed: https://www.youtube.com/watch?v=54S0tw0UrUg

I have downloaded gcc but it is still showing the same apple clang:

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

I have managed to get the gcc installed as well as libomp

When I ran the program I got this error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-02-16 13:49:32

使用M1芯片在MacBook Air上安装Brew后也有类似的问题。
运行使用Homebrew安装的G ++,而不是默认一个为我解决了它。

  • Brew LS GCC

G ++的完整路径是

  • /Opt/homebrew/cellar/gcc/12.2.2.0/bin/g++-12

现在我可以与这样的-fopenmp进行编译:

  • /popenmp: //opt/homebrew/cellar/cellar/gcc/gcc/12.2.0/ bin/g ++ -12 -fopenmp pragma_test.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

Had similar issues after installing with brew on MacBook Air with M1 chip.
Running the g++ installed with homebrew instead of default one solved it for me.

  • brew ls gcc

The full path for g++ was

  • /opt/homebrew/Cellar/gcc/12.2.0/bin/g++-12

Now I can compile with -fopenmp like this:

  • /opt/homebrew/Cellar/gcc/12.2.0/bin/g++-12 -fopenmp pragma_test.cpp

Sample program:

#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());
}

And it works as it should:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文