Search code examples
cmacosopenmpapple-m1

How to install OpenMP on Mac M1?


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?


Solution

  • 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