Using OpenMP with clang
Update
Building the latest trunk of LLVM/Clang (clang-3.8), installing libiomp5, and specifying the location of the gomp omp header files worked. Note that the Ubuntu package for libiomp5 isn't quite correct, so you will need to add a symlink in /usr/lib from /usr/lib/libiomp5.so to /usr/lib/libiomp5.so.5.
./clang++ -I/usr/lib/gcc/x86_64-linux-gnu/4.9/include -fopenmp=libiomp5 -o test test.cpp
I'm using g++-5.1 and clang++-3.6 on Linux Mint 17.2 (essentially Ubuntu trusty) and I see the same results with the following code.
#include <iostream>
#include <omp.h>
int main() {
#pragma omp parallel num_threads(4)
{
#pragma omp critical
std::cout << "tid = " << omp_get_thread_num() << std::endl;
}
}
Running this under ltrace reveals the issue:
g++
$ g++ -fopenmp -o test test.cpp
$ ./test
tid = 0
tid = 3
tid = 2
tid = 1
$ ltrace ./test
__libc_start_main(0x400af6, 1, 0x7ffc937b8198, 0x400bc0 <unfinished ...>
_ZNSt8ios_base4InitC1Ev(0x6021b1, 0xffff, 0x7ffc937b81a8, 5) = 0
__cxa_atexit(0x4009f0, 0x6021b1, 0x602090, 0x7ffc937b7f70) = 0
GOMP_parallel(0x400b6d, 0, 4, 0 <unfinished ...>
GOMP_critical_start(0, 128, 0, 0) = 0
tid = 3
tid = 2
omp_get_thread_num(0x7f9fe13894a8, 1, 0, 0x493e0) = 0
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(0x6020a0, 0x400c44, 0, 0x493e0) = 0x6020a0
_ZNSolsEi(0x6020a0, 0, 0x7f9fe1a03988, 0x203d2064) = 0x6020a0
_ZNSolsEPFRSoS_E(0x6020a0, 0x400920, 0x7f9fe1a03988, 0 <unfinished ...>
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(0x6020a0, 0x400920, 0x7f9fe1a03988, 0) = 0x6020a0
<... _ZNSolsEPFRSoS_E resumed> ) = 0x6020a0
GOMP_critical_end(0x7f9fe0d2d400, 0x7f9fe0d2e9e0, 0, -1) = 0
tid = 1
tid = 0
<... GOMP_parallel resumed> ) = 0
_ZNSt8ios_base4InitD1Ev(0x6021b1, 0, 224, 0x7f9fe0d2df50) = 0x7f9fe1a08940
+++ exited (status 0) +++
clang
$ clang++ -fopenmp -o test test.cpp
$ ./test
tid = 0
$ ltrace ./test
__libc_start_main(0x4009a0, 1, 0x7ffde4782538, 0x400a00 <unfinished ...>
_ZNSt8ios_base4InitC1Ev(0x6013f4, 0x7ffde4782538, 0x7ffde4782548, 5) = 0
__cxa_atexit(0x400830, 0x6013f4, 0x6012c8, 0x7ffde4782310) = 0
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(0x6012e0, 0x400a84, 0x7ffde4782548, 6) = 0x6012e0
omp_get_thread_num(0x7f3e4698c006, 0x7f3e4698c000, 0x7f3e46764988, 1024) = 0
_ZNSolsEi(0x6012e0, 0, 0x7f3e46764988, 1024) = 0x6012e0
_ZNSolsEPFRSoS_E(0x6012e0, 0x4007a0, 0x7f3e46764988, 0 <unfinished ...>
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(0x6012e0, 0x4007a0, 0x7f3e46764988, 0) = 0x6012e0
tid = 0
<... _ZNSolsEPFRSoS_E resumed> ) = 0x6012e0
_ZNSt8ios_base4InitD1Ev(0x6013f4, 0, 224, 0x7f3e45886f50) = 0x7f3e46769940
+++ exited (status 0) +++
You can immediately see the problem: clang++ never calls GOMP_parallel, so you always get one thread. This is crazy behavior on the part of clang. Have you tried building and using the "special" OpenMP version of clang?
Using OpenMP with llvm-clang
Despite the fact that its kinda late regarding the time-stamp of my original question, but I would like to mention the answer here so at-least it saves people's time facing similar issue.
LLVM itself currently doesn't support Openmp
right out-of-the-box. You can make it compile and run the omp
tagged code with Intel Runtime Support. However, if you want to have a clean clang
supporting OpenMP, there is a trunk of the project at OpenMP-Clang which you can clone and build. The current support is OpenMP 3.1 specification and they will reach to support OpenMP 4.0 specification soon:
$ git clone https://github.com/clang-omp/llvm_trunk llvm
$ git clone https://github.com/clang-omp/compiler-rt_trunk llvm/projects/compiler-rt
$ git clone https://github.com/clang-omp/clang_trunk llvm/tools/clang
Don't forget to build the Intel® OpenMP* Runtime Library
after this as you need omp.h
and /path/to/llvm/projects/openmp/runtime/lin_32e/lib/libomp.so
LLVM / Clang 8 Compilation of OpenMP Code in Windows
To compile and link OpenMP code with clang on Windows, you will have to pass -fopenmp
to both the compiler and the linker:
clang -fopenmp -o bla.obj -c bla.c
clang -fopenmp -o bla.exe bla.obj
clang 10 & OpenMP on range-based for error (docs say should be ok)
Support for range-based for loops was added to OpenMP 5.0, and, as is also described in the Clang 10 Release Notes that you link to, you need to explicitly use the -fopenmp-version=50
option to activate support for it:
OpenMP Support in Clang
Use
-fopenmp-version=50
option to activate support for OpenMP 5.0.
Thus, if we expand you compilation command to clang++-10 -fopenmp -fopenmp-version=50 -std=c++17
, the OMP pragma accepts the range based for loop that follows it.
DEMO.
Related Topics
Why Does This Simple Std::Thread Example Not Work
Reading an Application's Manifest File
Replace Multiple Spaces with One Space in a String
When Is a Vtable Created in C++
Template Specialization Based on Inherit Class
Avx2: Computing Dot Product of 512 Float Arrays
C++ Format MACro/Inline Ostringstream
Boost-Python How to Pass a C++ Class Instance to a Python Class
Convert Eigen Matrix to C Array
How to Create Std::Array with Initialization List Without Providing Size Directly
Memory Allocation Profiling in C++
Problems Using Member Function as Custom Deleter with Std::Shared_Ptr
How to Pass Derived Classes by Reference to a Function Taking Base Class as a Parameter
Pros & Cons of Putting All Code in Header Files in C++
What Is Monomorphisation with Context to C++
What Should I Know About Structured Exceptions (Seh) in C++
How Does the Linker Handle Identical Template Instantiations Across Translation Units