我正在为 OpenMP 的一个非常小的子集实现一个运行时库,但我遇到了 Task 和 Taskwait 构造的语义。
为了便于理解,我创建了下面的代码示例。那么系统不应该进入活锁状态吗?对于“task2”任务正在等待消耗“task1”任务生成的数据,但是“task1”正在“等待”其所有子任务完成?当我使用 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 dependent
子句仅适用于同级任务。在这种情况下,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.
关于c++ - OpenMP 任务和 Taskwait 构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313318/