5

我正在尝试开始在 C 中使用基本的 OpenMP 功能。我对“omp parallel for”的基本理解使我相信以下内容应该在线程之间分配以下循环迭代,并且应该同时执行。我得到的输出如下。代码如下。我的 hello world 示例中是否缺少一些微妙的东西?

来自 omp 线程的 Hello World 0 来自 omp 线程的 Hello World 0 来自 omp 线程的 Hello World 0 来自 omp 线程的 Hello World 0 来自 omp 线程的 Hello World 0 来自 omp 线程 0 的 Hello World 等等。

 int HelloFunc()
{
    int i;
    int numthreads = 8;
#pragma omp parallel for default(none) num_threads(numthreads) private(i)
    for (i = 0; i < 100; i++)
    {
        int tid = omp_get_thread_num();
        printf("Hello world from omp thread %d\n", tid);
    }
    return -1;
}

int main()
{
    int result = HelloFunc();
}
4

2 回答 2

6
#include <omp.h>
#include <stdio.h>


 int HelloFunc()
{
    int i;
    int numthreads = 8;
#pragma omp parallel for default(none) num_threads(numthreads) private(i)
    for (i = 0; i < 100; i++)
    {
        int tid = omp_get_thread_num();
        printf("Hello world from omp thread %d\n", tid);
    }
    return -1;
}

int main()
{
    HelloFunc();

  return 0;
}

then compile :

gcc t.c -fopenmp -Wall

and run :

./a.out

output :

Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 2
于 2015-03-05T20:59:30.617 回答
2

Your computer is likely only using one thread to run this program. OMP doesn't force it to run with multiple threads, it just tells the compiler that it can and sets up the necessary environment to make it happen.

There is no way to force OMP to do something in more threads than it would otherwise do. And you wouldn't want to since OMP automatically sets everything up to make run the fastest it can.

于 2015-03-05T20:57:38.460 回答