4

I am trying to parallelize a program using openmp on a Mac, but I can not manage to make it multi-threaded. I've tried building llvm/clang/openmp 3.7.1 from source (after a svn co) as documented, I have also tried using the prebuild versions of clang and OpenMP 3.7.0 given by the llvm project. In each case, the resulting compiler works fine with the -fopenmp flag and produce an executable that links to the openmp runtime.

I use the following openmp 'hello world' program:

#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 */
}

I compile using:

clang -fopenmp hello.c -o hello

and then run the resulting program with:

env OMP_NUM_THREADS=2 ./hello

which gives:

Hello World from thread = 0
Number of threads = 1

Any idea?

rndblnch
  • 183
  • 1
  • 10
  • Hm, try `num_threads` or `omp_set_num_threads` to set number of threads. Does the issue persist? (I think, it would :( ) – John_West Jan 28 '16 at 14:40
  • I tried both with no luck. But omp_get_num_procs and omp_get_max_threads both return 4. – rndblnch Jan 28 '16 at 14:45
  • 1
    Try `parallel` without `private`: make it simpler as you can. If not helping, maybe it is `clang` bug - `openmp` support is added only in May, 2015, and there is still many issues, I think. Also, try `export` instead of `env` (do not think to help though). – John_West Jan 28 '16 at 14:51
  • i have tried your suggestions with no luck (as for export instead of env, I am using tcsh). – rndblnch Jan 28 '16 at 15:01
  • Then try the following two commands: setenv OMP_NUM_THREADS 2 followed by ./hello – Harald Jan 28 '16 at 15:04
  • `env` works just fine, that's standard tcsh. that's not the problem, using `omp_set_num_threads(4)` in the code leads to the same result. – rndblnch Jan 28 '16 at 15:11

1 Answers1

3

clang < 3.8.0 requires -fopenmp=libomp to generate OpenMP code. clang >= 3.8.0 also supports -fopenmp (-fopenmp=libomp can be used also).

Alexey Bataev
  • 428
  • 3
  • 11