OpenMP在我的Mac上只能单线程运行。

4
我正在尝试在Mac上使用openmp并行化程序,但我无法使其多线程运行。 我尝试从源代码构建llvm/clang/openmp 3.7.1(经过svn co)如所述,我还尝试使用llvm项目提供的预构建版本的clang和OpenMP 3.7.0。 在每种情况下,生成的编译器可以使用-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

有任何想法吗?

嗯,尝试使用num_threadsomp_set_num_threads来设置线程数。问题仍然存在吗?(我想会的 :( ) - John_West
我尝试了两种方法,但都没有成功。不过omp_get_num_procs和omp_get_max_threads都返回4。 - rndblnch
1
尝试在不使用private的情况下使用parallel:尽可能简化它。如果没有帮助,可能是clang的一个bug - openmp支持仅在2015年5月加入,并且仍存在许多问题,我想。另外,请尝试使用export而不是env(虽然不认为会有帮助)。 - John_West
我已经尝试了你的建议,但没有成功(关于使用 export 而不是 env,我正在使用 tcsh)。 - rndblnch
然后尝试以下两个命令:setenv OMP_NUM_THREADS 2,然后是./hello。 - Harald
env 功能正常,这是标准的 tcsh。这不是问题,在代码中使用 omp_set_num_threads(4) 导致相同的结果。 - rndblnch
1个回答

3

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接