openmp在我的mac上运行单线程 [英] openmp runs single threaded on my mac

查看:168
本文介绍了openmp在我的mac上运行单线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Mac上使用openmp来并行化程序,但是我无法使它成为多线程的. 我已经尝试根据记录从源代码(在svn co之后)构建llvm/clang/openmp 3.7.1,我还尝试使用llvm项目提供的 clang和OpenMP 3.7.0的预构建版本. 在每种情况下,生成的编译器都可以在-fopenmp标志下正常工作,并生成链接到openmp运行时的可执行文件.

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.

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

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

给出:

Hello World from thread = 0
Number of threads = 1

有什么主意吗?

推荐答案

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

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

这篇关于openmp在我的mac上运行单线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆