我有一个包含2-3个经常被调用的函数的循环。
假设是这样的:
while(keepRunning())
{
doLongOne();
doLongTwo();
doLongThree();
}而doLong#本质上是:
for(it=collection.begin(); it!=collection.end(); it++)
{
(*it).doLong();
}我想做的是,为循环创建一个#pragma omp parallel sections (每个函数一个部分),并为每个函数中的循环创建一个#pragma omp parallel for。
然而,考虑到OMP不能很好地支持多个parallel编译指示,我不确定这是否能很好地工作。
考虑到这一点,考虑到我不能(由于架构原因)改变程序的流程布局,实现这一点的最佳方式是什么?
发布于 2014-03-01 03:03:24
有了关于您问题的更多详细信息,我们可以为您提供更多帮助(例如,三个向量中的每个元素是否可以并行计算?在计算过程中,每个向量中的条目数量是否会发生变化?条目是否为不同种类/变量类型?...)
Yo可以尝试以下操作:
将函数doLongOne的所有局部变量作为ClassOne中的成员变量,将函数doLongTwo的所有局部变量作为ClassTwo中的成员变量,并将doLongThree中的所有局部变量作为成员变量
while(keepRunning())
{
ClassOne one;
ClassTwo two;
ClassThree three;
#pragma omp sections
{
#pragma omp section
{
one.prepare(); // do all the staff before the "great loop" in function doLongOne
}
#pragma omp section
{
two.prepare(); // do all the staff before the "great loop" in function doLongTwo
}
#pragma omp section
{
three.prepare(); // do all the staff before the "great loop" in function doLongThree
}
}
int index[4];
index[0] = 0;
index[1] = index[0] + one.get_loop_num();
index[2] = index[1] + one.get_loop_num();
index[3] = index[2] + one.get_loop_num();
#pragma omp parallel for
for (int i=0; i<index[3]; i++)
{
int j=1;
while (j<4 && i>index[j]) j++;
if (j==4) {}// DO PANIC!
switch (j)
{
case 1:
one.doLong(i-index[j-1]); // call directly (*it).doLong();
break;
case 2:
two.doLong(i-index[j-1]); // call directly (*it).doLong();
break;
case 3:
three.doLong(i-index[j-1]); // call directly (*it).doLong();
break;
default:
// DO PANIC !
break;
}
}https://stackoverflow.com/questions/22099373
复制相似问题