我试图在Mac上使用openmp并行化一个程序,但是我无法使它成为多线程的。我尝试将llvm/clang/openmp 3.7.1从源代码(在svn之后)构建为记录在案,我也尝试使用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
有什么想法吗?
发布于 2016-01-29 00:27:30
clang < 3.8.0要求-fopenmp=libomp生成OpenMP代码。clang >= 3.8.0也支持-fopenmp (也可以使用-fopenmp=libomp)。
https://stackoverflow.com/questions/35064093
复制相似问题