【问题标题】:OMP For parallel thread ID hello worldOMP 用于并行线程 ID hello world
【发布时间】:2015-03-05 20:49:38
【问题描述】:

我正在尝试开始在 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_NUM_THREADS 环境变量来请求OpenMP 使用特定数量的线程。最大线程数可以从代码内部用omp_get_max_threads()查询到

标签: c multithreading for-loop openmp parallel-for


【解决方案1】:
#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

【讨论】:

  • 这是一个很好的教育示例,表明当有多个线程时,事情会按照 OP 的预期工作。然而对于 real 代码,在代码中显式强制线程数量通常是一个坏主意,因为在某些时候你会购买一台新机器,或者其他人会想要使用它,然后您会花一两天的时间想知道为什么它的内核数量是原来的两倍,但运行速度却没有提高!
  • @Jim Cownie。好的。你能展示一下代码吗? TIA
  • 我认为private(i) 在这里是多余的。 i 默认为私有。
【解决方案2】:

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

没有办法强制 OMP 在更多线程中执行某些操作。而且您不会希望这样做,因为 OMP 会自动设置所有内容以使其以最快的速度运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多