OpenMP Task 和 Taskwait 结构
OpenMP Task and Taskwait constructs
我正在为 OpenMP 的一个非常小的子集实现一个运行时库,但我被 Task 和 Taskwait 构造的语义困住了。
为了便于理解,我创建了下面的代码示例。其中,系统不应该进入活锁状态吗?因为 'task2' 任务正在等待使用 'task1' 任务产生的数据,但是 'task1' 是 "taskwaiting" 它的所有子任务完成?当我用 GOMP 和 Intel OMP 尝试这个片段时,程序正常完成执行。
#include <stdio.h>
#include <math.h>
#include <omp.h>
#include <time.h>
#include <cstdlib>
int result;
void task2(int* res) {
printf("Task2... %p\n", res);
}
void task1(int* res) {
printf("Task1... %p\n", &result);
#pragma omp task depend(in:result)
task2(&result);
#pragma omp taskwait
printf("Task1 finishing...\n");
}
int main() {
int res = 0;
#pragma omp parallel
#pragma omp single
{
printf("Res addr = %p\n", &result);
#pragma omp task depend(inout:result)
task1(&result);
}
return 0;
}
task depend
子句仅适用于同级任务。在那种情况下 task2
不是兄弟任务,而是 task1
.
的子任务
引用OpenMP 4.5的相关部分:
[2.13.9] For the in dependence-type, if the storage location of at least one of
the list items is the same as the storage location of a list item
appearing in an out or inout dependence-type list of a task
construct from which a sibling task was previously generated, then the
generated task will be a dependent task of that sibling task.
我正在为 OpenMP 的一个非常小的子集实现一个运行时库,但我被 Task 和 Taskwait 构造的语义困住了。
为了便于理解,我创建了下面的代码示例。其中,系统不应该进入活锁状态吗?因为 'task2' 任务正在等待使用 'task1' 任务产生的数据,但是 'task1' 是 "taskwaiting" 它的所有子任务完成?当我用 GOMP 和 Intel OMP 尝试这个片段时,程序正常完成执行。
#include <stdio.h>
#include <math.h>
#include <omp.h>
#include <time.h>
#include <cstdlib>
int result;
void task2(int* res) {
printf("Task2... %p\n", res);
}
void task1(int* res) {
printf("Task1... %p\n", &result);
#pragma omp task depend(in:result)
task2(&result);
#pragma omp taskwait
printf("Task1 finishing...\n");
}
int main() {
int res = 0;
#pragma omp parallel
#pragma omp single
{
printf("Res addr = %p\n", &result);
#pragma omp task depend(inout:result)
task1(&result);
}
return 0;
}
task depend
子句仅适用于同级任务。在那种情况下 task2
不是兄弟任务,而是 task1
.
引用OpenMP 4.5的相关部分:
[2.13.9] For the in dependence-type, if the storage location of at least one of the list items is the same as the storage location of a list item appearing in an out or inout dependence-type list of a
task
construct from which a sibling task was previously generated, then the generated task will be a dependent task of that sibling task.