Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable OpenMP support in clang in Mac OS X (sierra & Mojave)

I am using Mac OS X Sierra, and I found that clang (LLVM version 8.1.0 (clang-802.0.38)) does not support OpenMP: when I run clang -fopenmp program_name.c, I got the following error:

clang: error: unsupported option '-fopenmp'

It seems that clang does not support -fopenmp flag.

I could not find any openmp library in homebrew. According to LLVM website, LLVM already supports OpenMP. But I could not find a way to enable it during compiling.

Does this mean that the default clang in Mac does not support OpenMP? Could you provide any suggestions?

(When I switch to GCC to compile the same program (gcc is installed using brew install gcc --without-multilib), and the compilation is successful.)

like image 841
Starry Avatar asked Apr 22 '17 04:04

Starry


People also ask

How do I enable OpenMP in clang?

1) You need to use -fopenmp=libomp to enable OpenMP in clang.

Does macOS come with clang?

Clang is released as part of regular LLVM releases. You can download the release versions from https://llvm.org/releases/. Clang is also provided in all major BSD or GNU/Linux distributions as part of their respective packaging systems. From Xcode 4.2, Clang is the default compiler for Mac OS X.

How do I update clang on Mac?

Enter the command clang --version to see if the Clang compilers are already installed. If you want to install or update the Clang compilers, enter the command command xcode-select --install The following pop-up windout should appear on your screen (in this example I have placed it withing the Terminal window).


2 Answers

  1. Try using Homebrew's llvm:

    brew install llvm 
  2. You then have all the llvm binaries in /usr/local/opt/llvm/bin.

    Compile the OpenMP Hello World program. Put omp_hello.c

    /******************************************************************************      * FILE: omp_hello.c      * DESCRIPTION:      *   OpenMP Example - Hello World - C/C++ Version      *   In this simple example, the master thread forks a parallel region.      *   All threads in the team obtain their unique thread number and print it.      *   The master thread only prints the total number of threads.  Two OpenMP      *   library routines are used to obtain the number of threads and each      *   thread's number.      * AUTHOR: Blaise Barney  5/99      * LAST REVISED: 04/06/05      ******************************************************************************/      #include <omp.h>      #include <stdio.h>      #include <stdlib.h>       int main (int argc, char *argv[])       {      int nthreads, tid;       /* Fork a team of threads giving them their own copies of variables */      #pragma omp parallel private(nthreads, tid)        {         /* Obtain thread number */        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);          }         }  /* All threads join master thread and disband */       } 

    in a file and use:

    /usr/local/opt/llvm/bin/clang -fopenmp -L/usr/local/opt/llvm/lib omp_hello.c -o hello 

    You might also have to set the CPPFLAGS with -I/usr/local/opt/llvm/include.

    The makefile should look like this:

    CPP = /usr/local/opt/llvm/bin/clang CPPFLAGS = -I/usr/local/opt/llvm/include -fopenmp LDFLAGS = -L/usr/local/opt/llvm/lib  omp_hello: omp_hello.c      $(CPP) $(CPPFLAGS) $^ -o $@ $(LDFLAGS) 

Update

In macOS 10.14 (Mojave) you might get an error like

/usr/local/Cellar/llvm/7.0.1/lib/clang/7.0.1/include/omp.h:118:13: fatal error: 'stdlib.h' file not found 

If this happens, the macOS SDK headers are missing from /usr/include. They moved into the SDK itself with Xcode 10. Install the headers into /usr/include with

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg 
like image 140
Dirk Avatar answered Sep 28 '22 00:09

Dirk


Other people have given one solution (using Homebrew llvm). You can also use OpenMP with Apple Clang and Homebrew libomp (brew install libomp). Just replace a command like clang -fopenmp test.c with clang -Xpreprocessor -fopenmp test.c -lomp.

like image 24
Yongwei Wu Avatar answered Sep 28 '22 02:09

Yongwei Wu