社区首页 >问答首页 >openmp在我的mac上运行单线程。

openmp在我的mac上运行单线程。
EN

Stack Overflow用户
提问于 2016-01-28 06:24:50
回答 1查看 1.8K关注 0票数 3

我试图在Mac上使用openmp并行化一个程序,但是我无法使它成为多线程的。我尝试将llvm/clang/openmp 3.7.1从源代码(在svn之后)构建为记录在案,我也尝试使用llvm项目给出的clang和OpenMP 3.7.0的预构建版本。在每种情况下,生成的编译器都可以很好地处理-fopenmp标志,并生成一个链接到openmp运行时的可执行文件。

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

代码语言:javascript
代码运行次数:0
复制
#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 */
}

我使用以下方法编译:

代码语言:javascript
代码运行次数:0
复制
clang -fopenmp hello.c -o hello

然后使用以下方法运行生成的程序:

代码语言:javascript
代码运行次数:0
复制
env OMP_NUM_THREADS=2 ./hello

这意味着:

代码语言:javascript
代码运行次数:0
复制
Hello World from thread = 0
Number of threads = 1

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-29 00:27:30

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35064093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档