How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
问题描述
以下代码只是并行化第一个(外部)循环,还是并行化整个嵌套循环?
Does the following code just parallelize the first (outer) loops, or it parallelize the entire nested loops?
#pragma omp parallel for
for (int i=0;i<N;i++)
{
for (int j=0;j<M;j++)
{
//do task(i,j)//
}
}
我只想确定上面的代码是否会并行化整个嵌套的 for 循环(因此一个线程直接与 task(i,j) 相关),或者它只并行化外部 for 循环(从而确保,对于每个循环索引为 i 的并行线程,其内部循环将在单个线程中依次完成,这非常重要).
I just want to make sure if the above code will parallelize the entire nested for-loops (thus one thread directly related task(i,j)), or it only parallelizes the outer for-loop (thus it ensures that, for each parrallel thread with loop index i, its inner loop will be done sequentially in a single thread, which is very import).
推荐答案
您编写的行将仅并行化外循环.要并行化两者,您需要添加一个 collapse
子句:
The lines you have written will parallelize only the outer loop. To parallelize both you need to add a collapse
clause:
#pragma omp parallel for collapse(2)
for (int i=0;i<N;i++)
{
for (int j=0;j<M;j++)
{
//do task(i,j)//
}
}
您可能需要查看 OpenMP 3.1 规范(第 2.5.1 节)以了解更多详细信息.
You may want to check OpenMP 3.1 specifications (sec 2.5.1) for more details.
这篇关于OpenMP 如何处理嵌套循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenMP 如何处理嵌套循环?


基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01