我正在尝试并行化由第三方编写的大型程序。我不能透露代码,但我会尝试给出我想要做的最接近的例子。 根据下面的代码。正如你所看到的那样,由于“并行”这个子句在while循环内部,线程的创建/销毁是在每次迭代中完成的,这是昂贵的。 鉴于我无法将Initializors ...等移到“while”循环之外。如何让OpenMP在每次运行程序时只创建一次线程?
--base代码
void funcPiece0()
{
// many lines and branches of code
}
void funcPiece1()
{
// also many lines and branches of code
}
void funcCore()
{
funcInitThis();
funcInitThat();
#pragma omp parallel
{
#pragma omp sections
{
#pragma omp section
{
funcPiece0();
}//omp section
#pragma omp section
{
funcPiece1();
}//omp section
}//omp sections
}//omp parallel
}
int main()
{
funcInitThis();
funcInitThat();
#pragma omp parallel
{
while(1)
{
funcCore();
}
}
}
我寻求做是为了避免每次迭代的创建/销毁,并在节目的开始/结束让它一次。我对“平行”条款的置换做了许多变化。我基本上有相同的本质是(只有一个线程创建/销毁每个程序运行) - 我试过了,但在初始化函数中失败了“非法访问”。
void funcPiece0()
{
// many lines and branches of code
}
void funcPiece1()
{
// also many lines and branches of code
}
void funcCore()
{
funcInitThis();
funcInitThat();
//#pragma omp parallel
// {
#pragma omp sections
{
#pragma omp section
{
funcPiece0();
}//omp section
#pragma omp section
{
funcPiece1();
}//omp section
}//omp sections
// }//omp parallel
}
int main()
{
funcInitThis();
funcInitThat();
while(1)
{
funcCore();
}
}
-
任何帮助,将不胜感激! 谢谢!
请注意,OpenMP网站有一个论坛,您可以在其中发布有关OpenMP的问题 - 请访问http://openmp.org/forum/ – 2011-11-21 04:26:38