9

So I tried to use OpenMP with one of the latest version of clang, clang version 3.4.2 (tags/RELEASE_34/dot2-final). Followed the procedure to compile and add the PATHs of omp.h, then Compiling my hello.c using :

clang -fopenmp hello.c

and then running it, still it can't use more than 1 threads:

Bash-4.1$ ./a.out 
Hello from thread 0, nthreads 1

P.S: I tried to manually export export OMP_NUM_THREADS=8 but that didn't solve anything as well. Any ideas?

UPDATE: This is the hello.c:

#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());}
Pietro
  • 12,086
  • 26
  • 100
  • 193
Amir
  • 1,348
  • 3
  • 21
  • 44

3 Answers3

5

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

Amir
  • 1,348
  • 3
  • 21
  • 44
0

Try setting number of threads using omp_set_num_thread() function. If it doesn't work, try setting up clang again.

#include <omp.h>
#include <stdio.h>
int main() {
      omp_set_num_threads(4);
      #pragma omp parallel
      {
      printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(),       omp_get_num_threads());
      }    
}

Also try calling mp_get_max_threads() in both parallel and serial region, and see what you get

coder hacker
  • 4,819
  • 1
  • 25
  • 50
  • interesting thing is when I execute this : `clang -g -emit-llvm -S -c -fopenmp hello.c -o hello.ll` i am getting this warning: `clang: warning: argument unused during compilation: '-fopenmp'`, do you know why ?? – Amir Nov 07 '14 at 21:53
  • If you don't use -fopenmp, does compiler throw error about undefined reference to functions of openMP. If not the library might be included by default – coder hacker Nov 07 '14 at 21:59
  • no I have carried out the procedures here : `http://openmp.llvm.org/` and install the library and exported the path. before that I had error but now not anymore. – Amir Nov 07 '14 at 22:02
  • so if you don't use -fopenmp, it compiles succesfully? – coder hacker Nov 07 '14 at 22:02
  • yes. wait a moment. did I have to download a new version of clang to use with opennmp? i used my current clang one – Amir Nov 07 '14 at 22:06
  • try downloading new one.btw what does omp_get_max_threads() give in parallel region – coder hacker Nov 07 '14 at 22:07
  • yes, i forgot. it manually set the max to 4 : `Hello from thread 0, nthreads 1, maxThreads is 4` – Amir Nov 07 '14 at 22:20
  • 1
    strange behaviour. sorry i have no further idea about it. maybe u should have better chance on openmp forum – coder hacker Nov 07 '14 at 22:22
  • cheers buddy ;) let me try this other clang version and report this. – Amir Nov 07 '14 at 22:23
0

I was only seeing one thread too, with clang version 3.8.0 (trunk 252425). I then read some recent news at https://clang-omp.github.io:

November 27, 2015 - Further development of OpenMP support in clang/llvm compiler moved to www.llvm.org. This site is maintained for archival purposes only. Thank you to everyone who contributed all these years!

...and so I compiled LLVM/Clang from trunk; compiled the OpenMP runtime library using the excellent instructions here; and now it does work.

user2023370
  • 10,488
  • 6
  • 50
  • 83