OpenMP 4中的任务依赖性

编程入门 行业动态 更新时间:2024-10-27 14:24:15
本文介绍了OpenMP 4中的任务依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码基于OpenMP 4.0规范工作:

The following code works based on the OpenMP 4.0 specification:

out和 inout 依赖类型.生成的任务将是 所有先前生成的同级任务的从属任务 在in,out或inout中引用至少一个列表项 依赖类型列表.

The out and inout dependence-types. The generated task will be a dependent task of all previously generated sibling tasks that reference at least one of the list items in an in, out, or inout dependence-type list.

这意味着task3依赖于task2.正确的?但这没有道理!为什么输入输出依赖项任务应该是输入依赖项任务的依赖项?

This means that task3 becomes dependent of task2. Right? but it does not make sense! Why should an input-output dependency task be a dependent of an input dependency task?

要使它们独立,我需要做什么? p.s:在Linux上使用g ++ 4.9进行测试的代码.

What do I need to do in order to make them independent? p.s: code tested with g++ 4.9 on Linux.

#include <stdio.h> #include <omp.h> #include <unistd.h> int main() { int x,y; #pragma omp parallel num_threads(10) { #pragma omp single nowait { #pragma omp task depend (out:x) //task1 { x=1; } #pragma omp task depend(in:x) depend(out:y) //task2 { sleep(2); //Does task3 wait for us? Yes! y=x+1; } #pragma omp task depend (inout:x) //task3 { x++; printf("task3(x): %d\n" , x); } #pragma omp task depend (in:x,y) //task4 { printf("task4 (x+y): %d\n" , x+y); } } } return 0; }

推荐答案

问题1 :这意味着task3成为task2的依赖者.对吧?

根据条款(强调我的条款):

According to the OpenMP 4.0 standard on the depend clause (emphasis mine):

任务依赖关系从depend的依赖关系类型派生 子句及其列表项,其中dependency-type是以下项之一 以下:

Task dependences are derived from the dependence-type of a depend clause and its list items, where dependence-type is one of the following:

处于依赖类型.生成的任务将是依赖的 以前引用的所有同级任务的任务 out或inout依赖类型中的至少一个列表项 列表.

The in dependence-type. The generated task will be a dependent task of all previously generated sibling tasks that reference at least one of the list items in an out or inout dependence-type list.

出入依赖类型.生成的任务将是 所有先前生成的同级任务的相关任务,这些同级任务引用了 输入,输出或输入依赖类型列表.

The out and inout dependence-types. The generated task will be a dependent task of all previously generated sibling tasks that reference at least one of the list items in an in, out, or inout dependence-type list.

根据此描述,其内容如下:

From this description follows that:

  • 子句depend(in:x)将生成一个任务,该任务依赖于所有先前使用depend(out:x)或depend(inout:x)
  • 生成的任务
  • depend(out:x)子句或depend(inoout:x)子句将生成一个任务,该任务取决于先前在depend子句中提到x的所有先前生成的任务
  • the clause depend(in:x) will generate a task dependent on all the previously generated tasks with depend(out:x) or depend(inout:x)
  • the clause depend(out:x) or the clause depend(inoout:x) will generate a task dependent on all the previously generated tasks mentioning x in a depend clause

将其应用于您的特定情况会产生一连串的这种依赖关系:

Applying this to your specific case gives a chain of dependencies of this kind:

task1 (out:x) -> task2 (in:x,out:y) -> task4 (in:x,y) | ^ | | > task3 (inout:x)

因此,任务3取决于任务2的完成.

问题2 :为什么输入输出依赖项任务应该是输入依赖项任务的依赖项?

我只想让您注意到,使用此规则,您将在运行结束时获得变量x和y的确定性值(假设您负责同步对内存的访问).如果task3依赖于task1而不是task2,则该确定性将不成立(并且inout依赖关系将等效于in依赖关系).

I would just let you notice that with this rule you will have a deterministic value of your variables x and y at the end of the run (assuming you take care of synchronizing accesses to memory). If task3 was dependent on task1 instead of task2, this determinism wouldn't hold (and an inout dependency would have been equivalent to an in dependency).

问题3 :我需要做什么以使其独立?

将inout:x依赖项转换为in:x依赖项,并通过atomic子句同步对x的访问.这样一来,您将可以运行以下两种方式之一:

Turn the inout:x dependency into an in:x dependency and synchronize accesses to x via atomic clauses. That way you will have runs in which either:

  • x == 2和y == 2
  • x == 2和y == 3
  • x == 2 and y == 2
  • x == 2 and y == 3

取决于task2是否在task3之前执行.

depending on whether task2 executes before task3 or not.

更多推荐

OpenMP 4中的任务依赖性

本文发布于:2023-11-30 21:17:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:依赖性   OpenMP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!