【问题标题】:Parallelize function using OpenMP使用 OpenMP 并行化函数
【发布时间】:2013-09-19 06:27:30
【问题描述】:

我正在尝试并行运行代码,但我对与 openmp 相关的私有/共享等内容感到困惑。我正在使用 c++(msvc12 或 gcc)和 openmp。

代码迭代循环,该循环由一个应该并行运行的块组成,然后是一个应该在所有并行工作完成后运行的块。处理并行内容的顺序无关紧要。代码如下所示:

// some X, M, N, Y, Z are some constant values
const int processes = 4;
std::vector<double> vct(X);
std::vector<std::vector<double> > stackVct(processes, std::vector<double>(Y));
std::vector<std::vector<std::string> > files(processes, M)
for(int i=0; i < N; ++i)
{
  // parallel stuff
  for(int process = 0; process < processes; ++process)
  {
    std::vector<double> &otherVct = stackVct[process];
    const std::vector<std::string> &my_files = files[process];

    for(int file = 0; file < my_files.size(); ++file)
    { 
      // vct is read-only here, the value is not modified
      doSomeOtherStuff(otherVct, vct);

      // my_files[file] is read-only
      std::vector<double> thirdVct(Y);
      doSomeOtherStuff(my_files[file], thirdVct(Y));

      // thirdVct and vct are read-only
      doSomeOtherStuff2(thirdVct, otherVct, vct);
    }
  }
  // when all the parallel stuff is done, do this job
  // single thread stuff
  // stackVct is read-only, vct is modified
  doSingleTheadStuff(vct, stackVct)
}

如果性能更好,“doSingleThreadSuff(...)”可以移到并行循环中,但需要单线程处理。最内层循环中的函数顺序不能更改。

我应该如何声明#pragma omp 的东西以使其正常工作? 谢谢!

【问题讨论】:

    标签: c++ openmp


    【解决方案1】:

    并行运行for循环只是for循环语句上方的#pragma omp parallel for,并且在for循环之外声明的任何变量都由所有线程共享,并且在for循环内声明的任何变量都是私有的线程。

    请注意,如果您正在并行执行文件 IO,则可能不会看到太多加速(如果您所做的只是文件 IO,则几乎没有),除非至少有一些文件位于不同的物理硬盘驱动器上。

    【讨论】:

    • 我正在“doSomeOtherStuff(...)”中做一些 IO 的东西,但我可以将所有内容预加载到内存中
    • 并且谢谢... >
    【解决方案2】:

    也许是这样的(请注意,这只是一个草图,我没有验证它,但你可以理解):

    // some X, M, N, Y, Z are some constant values
    const int processes = 4;
    std::vector<double> vct(X);
    std::vector<std::vector<double> > stackVct(processes, std::vector<double>(Y));
    std::vector<std::vector<std::string> > files(processes, M)
    for(int i=0; i < N; ++i)
    {
        // parallel stuff
        #pragma omp parallel firstprivate(vct, files) shared(stackVct)
        {
            #pragma omp for
            for(int process = 0; process < processes; ++process)
            {
                std::vector<double> &otherVct = stackVct[process];
                const std::vector<std::string> &my_files = files[process];
    
                for(int file = 0; file < my_files.size(); ++file)
                {
                    // vct is read-only here, the value is not modified
                    doSomeOtherStuff(otherVct, vct);
    
                    // my_files[file] is read-only
                    std::vector<double> thirdVct(Y);
                    doSomeOtherStuff(my_files[file], thirdVct(Y));
    
                    // thirdVct and vct are read-only
                    doSomeOtherStuff2(thirdVct, otherVct, vct);
                }
            }
            // when all the parallel stuff is done, do this job
            // single thread stuff
            // stackVct is read-only, vct is modified
            #pragma omp single nowait
            doSingleTheadStuff(vct, stackVct)
        }
    }
    
    • 我将vct 和files 标记为第一个私有,因为它们是只读的并且我认为它们不应被修改,因此每个线程都会为自己获取这些变量的副本。
    • stackVct 被标记为在所有线程之间共享,因为它们对其进行了修改。
    • 最后只有一个线程会执行doSingleTheadStuff函数,而不会强制其他线程等待。

    【讨论】:

    • "不强制其他线程等待。"等等-我不确定我是否理解你所说的。 “doSingleThread”应该在所有并行工作完成后处理......但是下一个并行工作(即在下一次迭代中)应该在单线程工作完成后继续。会这样吗?
    • btw using this: "#pragma omp parallel firstprivate(vct, files) shared(stackVct)" 似乎很奇怪...如果我使用它,它会运行更多进程...跨度>
    • for 指令的末尾有一个隐式屏障,这意味着所有工作线程都保证在离开内部 for 循环之前完成它们的工作。之后只有一个线程将执行doSingleThreadStuff,nowait 导致其他线程不等待执行线程并继续消耗外部for循环的新迭代的一些工作。如果您真的希望他们等到单线程完成,您可以删除 nowait 选项。
    • 好的,非常感谢。如果我使用 firstprivate,您能否解释一下为什么 openmp 会多次运行并行循环?
    • 我不知道。 firstprivate 的目的是为每个线程制作变量的私有副本,其中新副本使用原始变量的值进行初始化。它不应该影响线程团队的大小。您可以随时使用omp_get_num_threads函数查看正在运行的线程数,也可以使用omp_get_thread_num获取正在运行的线程ID。使用它们来调试和验证此行为。
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2015-08-10
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多