我想知道是否有任何技术可以使用 for 循环在 OpenMp 中创建并行部分。
例如,我不想创建 n 个不同的 #pragma omp 部分,而是使用 n 次迭代来创建它们for-loop每个部分都有一些变化的参数。
#pragma omp parallel sections
{
#pragma omp section
{
/* Executes in thread 1 */
}
#pragma omp section
{
/* Executes in thread 2 */
}
#pragma omp section
{
/* Executes in thread n */
}
}
对于显式 OpenMP 任务:
#pragma omp parallel
{
// Let only one thread create all tasks
#pragma omp single nowait
{
for (int i = 0; i < num_tasks; i++)
#pragma omp task
{
// Code for task with parameters, based on i
}
}
// Let the threads process all tasks
#pragma omp taskwait
// Further parallel processing ...
}
OpenMP 后面的代码块task
指令是一项明确的任务。显式任务排队并稍后执行。这taskwait
指令行为类似于barrier
,但是对于任务。另请参阅这个答案 https://stackoverflow.com/a/13789119/1374437类似的问题。
任务可以递归地创建其他任务。因此显式任务分配可用于处理图和树。但要注意开销 - 它比大多数其他结构的开销更大,并且与来自循环的开销非常相似schedule(dynamic)
。默认情况下,任务内部引用的外部范围的变量也是如此firstprivate
.
请注意,显式任务是 OpenMP 3.0 中添加的功能。符合早期 OpenMP 版本的编译器可能不支持task
指示。几乎所有现代编译器都支持 OpenMP 3.0 或更高版本,但 Microsoft Visual C++ 是一个明显的例外,它仅支持 OpenMP 2.0(即使在 VS2012 中也是如此)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)