功能中的OpenMP

时间:2014-02-28 15:35:05

标签: c++ parallel-processing openmp

我有一个循环,有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编译指示,我不确定这是否会运行良好。
考虑到这一点,考虑到我不能(出于架构原因)改变程序的流程布局,实现这个的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

关于你的问题的更多细节,我们可以帮助你更多(例如,三个向量的每个元素可以并行计算吗?每个向量中的条目数是否在计算期间发生变化?是不同类型/变量的条目类型?...)

哟可以尝试以下方法:

将函数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;
  }
}