OpenMP的,对于内环路部分 [英] OpenMP, for loop inside section
问题描述
我想运行下面的code(下图)。我想产卵两个独立的线程,每一个都将运行循环并行。不幸的是,我得到一个错误。显然,并行为
不能部分
里面催生。如何解决?
的#include< omp.h>
的#includestdio.h中诠释的main()
{OMP_SET_NUM_THREADS(10);OMP的#pragma并行
OMP的#pragma节
{
OMP的#pragma节
OMP的#pragma为
的for(int i = 0;我小于5;我++){
的printf(X%d个\\ N,I);
}OMP的#pragma节
OMP的#pragma为
的for(int i = 0;我小于5;我++){
的printf(%d个\\ n,I);
}
} //结束并行和端部
}
和错误:
main.cpp中:在函数'廉政的main():
main.cpp中:14:9:警告:工作共享区域不可以紧密嵌套的工作分担内,临界,有序,船长或明确的任务区域[默认启用]
main.cpp中:20:9:警告:工作共享区域不可以紧密嵌套的工作分担内,临界,有序,船长或明确的任务区域[默认启用]
的OpenMP不能创建并行区域内并行区域。这是由于这样的事实,即OpenMP的在程序NUM_THREADS并行线程的开始建立,在非平行的区域的其他未使用和睡眠。他们做到了这一点,因为频繁产生新的线程相比,苏醒沉睡线程是相当缓慢。
因此,你应该只并行的循环:
的#include< omp.h>
的#includestdio.h中诠释的main()
{OMP_SET_NUM_THREADS(10);OMP的#pragma为平行
的for(int i = 0;我小于5;我++){
的printf(X%d个\\ N,I);
}OMP的#pragma为平行
的for(int i = 0;我小于5;我++){
的printf(%d个\\ n,I);
}
}
I would like to run the following code (below). I want to spawn two independent threads, each one would run a parallel for loop. Unfortunately, I get an error. Apparently, parallel for
cannot be spawned inside section
. How to solve that?
#include <omp.h>
#include "stdio.h"
int main()
{
omp_set_num_threads(10);
#pragma omp parallel
#pragma omp sections
{
#pragma omp section
#pragma omp for
for(int i=0; i<5; i++) {
printf("x %d\n", i);
}
#pragma omp section
#pragma omp for
for(int i=0; i<5; i++) {
printf(". %d\n", i);
}
} // end parallel and end sections
}
And the error:
main.cpp: In function ‘int main()’:
main.cpp:14:9: warning: work-sharing region may not be closely nested inside of work-sharing, critical, ordered, master or explicit task region [enabled by default]
main.cpp:20:9: warning: work-sharing region may not be closely nested inside of work-sharing, critical, ordered, master or explicit task region [enabled by default]
OpenMP cannot create parallel regions inside parallel regions. This is due to the fact that OpenMP create at the beginning of the program num_threads parallel threads, in non parallel regions the others are not used and sleep. They have done this, as the frequent generation of new threads is quite slow compared to waking sleeping threads.
Therefore you should parallelize only the loops:
#include <omp.h>
#include "stdio.h"
int main()
{
omp_set_num_threads(10);
#pragma omp parallel for
for(int i=0; i<5; i++) {
printf("x %d\n", i);
}
#pragma omp parallel for
for(int i=0; i<5; i++) {
printf(". %d\n", i);
}
}
这篇关于OpenMP的,对于内环路部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!