C语言 OMP 对于并行线程 ID hello world
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28887444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OMP For parallel thread ID hello world
提问by Oliver
I'm trying to get started with using basic OpenMP functionality in C. My basic understanding of 'omp parallel for' leads me to believe the following should distribute the following iterations of the loop between threads and should execute concurrently. The output I am getting is as follows. Code Below. Is there something subtle I am missing in my hello world example?
我正在尝试开始在 C 中使用基本的 OpenMP 功能。我对“omp parallel for”的基本理解使我相信以下应该在线程之间分配以下循环迭代并且应该并发执行。我得到的输出如下。代码如下。在我的 hello world 示例中是否缺少一些微妙的东西?
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 etc..
来自 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();
}
采纳答案by Adam
#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
回答by randomusername
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.
您的计算机可能只使用一个线程来运行此程序。OMP 不强制它运行多线程,它只是告诉编译器它可以并设置必要的环境来实现它。
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.
没有办法强制 OMP 在比其他情况下更多的线程中执行某些操作。而且您不会想要这样做,因为 OMP 会自动设置所有内容以使其运行速度最快。

