How to install OpenMP on Mac M1?
Asked Answered
M

4

11

I am currently studying to become a computer engineer and I need to work with OpenMP. After some research, I'm still having trouble installing it (#include <omp.h> is still not recognized). I tried libomp and llvm (with Homebrew), but I must have made a mistake along the way. Has anyone been able to use OpenMP on mac M1?

Monomial answered 10/2, 2022 at 8:27 Comment(1)
most libraries are split into two packages. The normal runtime library in one, and the headers in a separate dev or devel package. For development you need both.Slantwise
J
10

On macOS 13.2.1 and up-to-date Xcode command line toolset, on M2 chip I'm able to use OpenMP based on libomp from Homebrew (brew install libomp) BUT with the Apple provided clang, by running:

clang -Xclang -fopenmp -L/opt/homebrew/opt/libomp/lib -I/opt/homebrew/opt/libomp/include -lomp omptest.c -o omptest 

Where omptest.c is given as:

#include <omp.h>
 
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
 
    // Beginning of parallel region
    #pragma omp parallel
    {
 
        printf("Hello World... from thread = %d\n",
               omp_get_thread_num());
    }
    // Ending of parallel region
}

Summing up, if you don't like, you don't need to install full LLVM or GCC from Homebrew. Only libomp is needed and you should be good to go!

PS. The output of running omptest on my machine (M2 Max) is:

./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
Jetta answered 10/3, 2023 at 15:29 Comment(1)
Finally an answer that worked for me. Thank you, Witwold! (I have the M1 chip)Savagism
A
4

I know this is an older thread, but this is what worked for me on an M2 MacBook Pro using C++, and g++-12:

  1. Install Brew: https://brew.sh/

  2. Once Brew is installed, install GCC with the following Terminal command:

    brew install gcc
    
  3. Once GCC is installed, use the following command make your system aware of the GCC stuff, keeping in mind the 12.2.0 folder may change as time goes on:

    PATH=/opt/homebrew/Cellar/gcc/12.2.0/bin:$PATH
    

I found I had to append the actual GCC file path for it to work. For some reason, it didn't add any aliases/symlinks to /usr/local/bin like it did on my older Intel MacBook Pro.

And that should be all you need! For compiling C++ code with OpenMP:

g++-12 -fopenmp progName.cpp -o prog

Note that the g++ part of the command will change with time. So check what version of g++ is installed with Brew. if the version changes from 12, then change the part of the Terminal command (e.g. g++-13, or something like that).

Hope this helps someone out!

Aram answered 5/3, 2023 at 18:7 Comment(2)
Currently working with c++ on apple m2! Thanks a lot!Jukebox
It's worth noting if your brew path isn't the default and it has to build from source the brew step here takes many hours. There's nothing wrong with your machine, it's just painfully slow.Pevzner
S
1

A simple approach is to use brew https://brew.sh/ to install GCC or LLVM (clang), and then use that compiler. You need to tread carefully, though, since the MacOS environment includes X86 emulation which can be confusing.

https://cpufun.substack.com/p/setting-up-the-apple-m1-for-native might help, though it's now nearly a year old...

Sivas answered 10/2, 2022 at 16:9 Comment(10)
I forgot to mention it but when I tried to install libomp and llvm I did it using homebrew. They are installed fine but impossible to import omp.h. I can compile any kind of file in C but not with this libraryDulci
If you install LLVM you should not need to separately install libomp. Are you completely sure that you are using the brew-installed clang? The Apple development environment also includes an alias that means it can appear as clang too... (You might also find the hack at the end of #65293799 useful)Sivas
I have recently after several tests launched this command: "clang -Xpreprocessor -v -fopenmp fichier.c -lomp" and only -lomp could not be executed. I think that the post you sent me must contain the solution to my problem but I don't have the necessary knowledge to solve this problem. Is a flag, is a link that allows us to find some missing library at the origin. And yes it is clear that I must have problems between the clang provided by apple and that installed via homebrew.Dulci
First, work out which clang you are executing, % which clang. If it's not the brew installed one, then fix your PATH so that it is and try again.Sivas
theosouchon@MacBook-Pro-de-Theo ~ % which clang /opt/homebrew/opt/llvm/bin/clangDulci
OK, so it is the right compiler. In which case forcing the library path (as outlined near the end of the previous answer where we create a shell script to invoke the compiler (tagged with "I am not recommending this particularly")) may be what you need to do.Sivas
When you say you don't particularly recommend it, are you referring to your post you shared your link to? Otherwise I don't really understand what I should do now.Dulci
In that post I am saying that using a script like that to force additional arguments to the compiler is a hack, and therefore not to be particularly recommended. However, it is a hack which works. So it can be useful, but remains a hack to be treated with care.Sivas
ok, so apart from this hack there is no viable solution that you know of?Dulci
The hack is all I need, so I stopped looking. (Which doesn't mean there isn't a better solution, just that I ran out of energy to look for one and got back to doing more interesting things)Sivas
U
1

M1 chip seems doesn't install llvm in the proper location.

brew install llvm
cd /opt/homebrew/opt/libomp/lib

if libomp.dylib is in the folder /opt/homebrew/opt/libomp/lib:

cd /usr/local/lib
sudo ln -s /opt/homebrew/opt/libomp/lib/libomp.dylib libomp.dylib
Upbringing answered 19/4, 2022 at 0:37 Comment(2)
This does link the correct file, but I now get the following error from LightGBM (that wants to use libomp.dylib: '/usr/local/lib/libomp.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')). Just linking the file does not seem to fix it.Vertebral
this doesn't do the job on my side sadly :(Gorgon

© 2022 - 2024 — McMap. All rights reserved.