C语言 openMP:为什么我在使用“#pragma omp parallel num_threads(4)”时没有得到不同的线程 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13209686/
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
openMP:why am I not getting different thread ids when i uses " #pragma omp parallel num_threads(4)"
提问by jayesh hathila
Why am I not getting different thread ids when I uses " #pragma omp parallel num_threads(4)". All the thread ids are 0 in this case. But when I comment the line and use default number of threads, I got different thread ids. Note:- variable I used variable tid to get thread id.
为什么我在使用“#pragma omp parallel num_threads(4)”时没有得到不同的线程 ID。在这种情况下,所有线程 id 都是 0。但是当我注释该行并使用默认线程数时,我得到了不同的线程 ID。注意:- 变量我使用变量 tid 来获取线程 ID。
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
int x = 0;
#pragma omp parallel num_threads(4)
#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);
}
}
}
Output of above code:-
上述代码的输出:-
Hello World from thread = 0
Hello World from thread = 0
Number of threads = 1
Hello World from thread = 0
Number of threads = 1
Hello World from thread = 0
Number of threads = 1
Number of threads = 1
Output when I comment the line mentioned above:-
当我评论上面提到的行时输出:-
Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2
采纳答案by Vaughn Cato
You are creating two nested parallel regions. It is the same as doing this:
您正在创建两个嵌套的并行区域。这与执行此操作相同:
#pragma omp parallel num_threads(4)
{
#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);
}
}
}
omp_get_num_threads()returns the number of threads in the innermost region. So you are executing four threads, each of which is executing one thread.
omp_get_num_threads()返回最内部区域中的线程数。所以你正在执行四个线程,每个线程都在执行一个线程。
The inner parallel region is only executing one thread, because you haven't enabled nested parallelism. You can enable it by calling omp_set_nested(1).
内部并行区域仅执行一个线程,因为您尚未启用嵌套并行性。您可以通过调用启用它omp_set_nested(1)。
http://docs.oracle.com/cd/E19205-01/819-5270/aewbi/index.html
http://docs.oracle.com/cd/E19205-01/819-5270/aewbi/index.html
If instead of making two nested parallel regions, you wanted to make a single parallel region and specify two properties, you can do this:
如果您不想创建两个嵌套的并行区域,而是想创建一个并行区域并指定两个属性,则可以执行以下操作:
#pragma omp parallel num_threads(4) private(nthreads,tid)
{
.
.
.
}
回答by Jithu
Nesting can also be enabled with by setting the environment variable OMP_NESTED to true
也可以通过将环境变量 OMP_NESTED 设置为 true 来启用嵌套

