我目前正在使用OpenMP进行矩阵计算.我的代码中有几个循环,而是调用每个循环#pragma omp parallel for [...](创建所有线程并在之后销毁它们)我想在开头创建所有这些,并且在程序结束时删除它们以避免开销.我想要的东西:
#pragma omp parallel
{
#pragma omp for[...]
for(...)
#pragma omp for[...]
for(...)
}
Run Code Online (Sandbox Code Playgroud)
问题是我有一些部分必须只由一个线程执行,但是在一个循环中,它包含那些必须并行执行的循环......这就是它的样子:
//have to be execute by only one thread
int a=0,b=0,c=0;
for(a ; a<5 ; a++)
{
//some stuff
//loops which have to be parallelize
#pragma omp parallel for private(b,c) schedule(static) collapse(2)
for (b=0 ; b<8 ; b++);
for(c=0 ; c<10 ; c++)
{
//some other stuff
}
//end of the parallel zone
//stuff to be execute by only one thread
} …Run Code Online (Sandbox Code Playgroud)