OMP 对于并行线程 ID hello world

OMP For parallel thread ID hello world

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

来自 omp 线程 0 的 Hello World 来自 omp 线程 0 的 Hello World 来自 omp 线程 0 的 Hello World 来自 omp 线程 0 的 Hello World 来自 omp 线程 0 的 Hello World 来自 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();
}

您的计算机可能只使用一个线程来 运行 这个程序。 OMP 不会强制它使用多线程 运行,它只是告诉编译器它可以并设置必要的环境来实现它。

无法强制 OMP 在更多线程中执行某些操作。而且您不希望这样做,因为 OMP 会自动设置所有内容以使 运行 尽可能快。

#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;
}

然后编译:

gcc t.c -fopenmp -Wall

和运行:

./a.out

输出:

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