openmp在我的mac上运行单线程

时间:2016-01-28 14:24:51

标签: multithreading macos openmp llvm-clang

我正在尝试在Mac上使用openmp并行化程序,但我无法使其成为多线程。 我已尝试从源代码(在svn co之后)构建llvm / clang / openmp 3.7.1作为documented,我也尝试使用prebuild versions of clang and OpenMP 3.7.0 given by the llvm project。 在每种情况下,生成的编译器都可以正常使用-fopenmp标志,并生成一个链接到openmp运行时的可执行文件。

我使用以下openmp' hello world'程序:

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

我使用编译:

clang -fopenmp hello.c -o hello

然后运行生成的程序:

env OMP_NUM_THREADS=2 ./hello

给出:

Hello World from thread = 0
Number of threads = 1

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

clang&lt; 3.8.0需要-fopenmp = libomp来生成OpenMP代码。 clang&gt; = 3.8.0也支持-fopenmp(-fopenmp =也可以使用libomp)。