在GPU编程中使用条件语句时,工作项执行什么? [英] What do work items execute when conditionals are used in GPU programming?

查看:232
本文介绍了在GPU编程中使用条件语句时,工作项执行什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您的工作项目在波前执行,并且有一个条件,例如:

If you have work items executing in a wavefront and there is a conditional such as:

  if(x){
        ...
  }
  else{
       ....
  }

工作项执行什么?是这样的情况,其中波前中的所有工作项将执行第一分支(即 x == true )。如果没有x为false的工作项,那么跳过其余的条件语句?

What do the work-items execute? is it the case whereby all workitems in the wavefront will execute the first branch (i.e. x == true). If there are no work-items for which x is false, then the rest of the conditional is skipped?

如果一个工作项采用备用路径会发生什么。我告诉所有工作项将执行备用路径(因此执行两个路径?)。为什么是这种情况,它是如何不搞乱程序执行

What happens if one work-item takes the alternative path. Am I told that all workitems will execute the alternate path as well (therefore executing both paths?). Why is this the case and how does it not mess up the program execution

推荐答案

NVIDIA gpus使用条件执行来处理分支SIMD组(warp)。在 if..else 示例中,两个分支都由分支warp中的每个线程执行,但是那些不遵循给定分支,并执行null操作。这是经典的分支发散惩罚 - interwarp分支发散需要两次通过代码段来退役。这不是理想的,这是为什么面向性能的代码试图最小化这。经常抓住人的一件事是做出关于发散路径的哪个部分被第一执行的假设。已经有一些非常微妙的错误由第二次猜测内部执行顺序在分歧warp。

NVIDIA gpus use conditional execution to handle branch divergence within the SIMD group ("warp"). In your if..else example, both branches get executed by every thread in the diverging warp, but those threads which don't follow a given branch are flagged and perform a null op instead. This is the classic branch divergence penalty - interwarp branch divergence takes two passes through the code section to retire for warp. This isn't ideal, which is why performance oriented code tries to minimize this. One thing which often catches out people is making an assumption about which section of a divergent path gets executed "first". The have been some very subtle bugs cause by second guessing the internal order of execution within a divergent warp.

对于更简单的条件,NVIDIA GPU支持在ALU条件评估,没有发散,并且对于整个翘曲遵循相同路径的条件,也显然没有惩罚。

For simpler conditionals, NVIDIA GPUs support conditional evaluation at the ALU, which causes no divergence, and for conditionals where the whole warp follows the same path, there is also obviously no penalty.

这篇关于在GPU编程中使用条件语句时,工作项执行什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆