Ask Your Question
0

Use openmp with clang

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

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
0

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

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

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 libgomp.so can be found with the commands: gcc --print-file-name=include/omp.h and gcc --print-file-name=libgomp.so 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: http://openmp.llvm.org. 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):

#!/bin/bash
cd ~ &&
rm -rf $HOME/BUILDS/CLANG391_OMP &&
mkdir -p $HOME/BUILDS/CLANG391_OMP &&
cd $HOME/BUILDS && 
svn co http://llvm.org/svn/llvm-project/openmp/tags/RELEASE_391/final openmp &&
cd openmp/runtime &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/BUILDS/CLANG391_OMP ../ &&
make -j4 && 
make install &&
cd ~ &&
rm -rf $HOME/BUILDS/openmp

you can just save it as build_clang391_omp.sh and make it executable with chmod +x build_clang391_omp.sh. Before launching the script (with ./build_clang391_omp.sh, 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:

LD_LIBRARY_PATH=$HOME/BUILDS/CLANG391_OMP/lib:$LD_LIBRARY_PATH ./test-openmp

which gives:

22 April 2017 03:18:01 PM

DIJKSTRA_OPENMP
  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 ...
(more)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 1,086 times

Last updated: Apr 22 '17