我想运行下面的代码(下面)。我想产生两个独立的线程,每个线程都会运行一个并行for循环。不幸的是,我得到一个错误。显然,在section
内不能生成并行for
。如何解决这个问题?OpenMP,for循环里面部分
#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
}
和错误:
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]
但是那么你应该可能omp_set_num_threads()为5或更少... –
你在这种情况下的权利应该设置为5,但它应该是没有问题,如果它是10,因为其他5什么都不做。 – tune2fs
我同意,它不会破坏任何东西。 –