Ask Your Question

Use openmp with clang

asked 2017-04-18 06:29:46 -0500

Max Linke gravatar image

I'm trying to compile code that uses openmp with clang. I found that the clang options are -fopenomp=libomp. But that doesn't work since clang can't find the openmp headers.

test-openmp.cpp:5:11: fatal error: 'omp.h' file not found
# include <omp.h>
1 error generated.

As a simple example I checked this program from the openmp website. It compiles fine using gcc.

Can I find out where the omp headers are installed on fedora?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-04-22 08:58:33 -0500

vmercuri gravatar image

updated 2017-04-22 10:05:54 -0500

My answer ended up being a bit more elaborated than I thought. While you might have luck by using the OpenMP implementation that comes with GCC (the paths of omp.h and can be found with the commands: gcc --print-file-name=include/omp.h and gcc respectively), I'd suggest to do an out-of-tree build of the OpenMP subproject for the LLVM's exact version you use, as described here: The section "Status" of the same page says that since Clang 3.8, the OpenMP runtime is built as a normal part of the Clang build, so in theory we shouldn't need to do that manually. However, I didn't manage to enable OpenMP support just by installing the clang and llvm packages that come with Fedora. I've made a very small script (along the lines of the instructions in the page linked above), which allows you to locally install the OpenMP version compatible with Clang 3.9.1 (the one that comes with Fedora 25):

cd ~ &&
mkdir -p $HOME/BUILDS/CLANG391_OMP &&
svn co openmp &&
cd openmp/runtime &&
make -j4 && 
make install &&
cd ~ &&
rm -rf $HOME/BUILDS/openmp

you can just save it as and make it executable with chmod +x Before launching the script (with ./, as normal user), make sure you have at least the packages svn, cmake, clang and the group "C Development Tools and Libraries" installed through dnf.

The building process is generally very fast and you will end up having the folder CLANG391_OMP inside $HOME/BUILDS. Now you can build your OpenMP-based test. Type in a terminal:

clang++ -fopenmp -I$HOME/BUILDS/CLANG391_OMP/include -L$HOME/BUILDS/CLANG391_OMP/lib test-openmp.cpp -o test-openmp

and then launch the executable:


which gives:

22 April 2017 03:18:01 PM

  C++ version
  Use Dijkstra's algorithm to determine the minimum
  distance from node 0 to each node in a graph,
  given the distances between each pair of nodes.

  Although a very small example is considered, we
  demonstrate the use of OpenMP directives for
  parallel execution.

  Distance matrix:

    0   40   15  Inf  Inf  Inf
   40    0   20   10   25    6
   15   20    0  100  Inf  Inf
  Inf   10  100    0  Inf  Inf
  Inf   25  Inf  Inf    0    8
  Inf    6  Inf  Inf    8    0

  P0: Parallel region begins with 8 threads.

  P0  P  P  P  P24:  First=3:  First=  Last=0  Last=2-1

  P3:  First=2  Last=52
  P7:  First=5:  First=  Last=1  Last=51
  P6:  First=4  Last=4
:  First=3  Last=3

1:  First=0  Last=0
  P2: Connecting node 2
  P2: Connecting node 1
  P3: Connecting node 5
  P2: Connecting node 3
  P0: Connecting node 4

  P2: Exiting parallel region.

  Minimum distances from node 0:

   0 ...
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-04-18 06:29:46 -0500

Seen: 1,420 times

Last updated: Apr 22 '17