gpt4 book ai didi

cuda - 分支指令和谓词指令

转载 作者:行者123 更新时间:2023-12-03 22:29:54 24 4
gpt4 key购买 nike

Section 5.4.2 CUDA C 编程指南的第 1 部分指出分支分歧由“分支指令”处理,或者在某些条件下由“预测指令”处理。我不明白两者之间的区别,以及为什么一个比另一个带来更好的性能。

This comment表明分支指令会导致更多的执行指令,由于“分支地址解析和获取”而停滞,以及由于“分支本身”和“为发散而记账”的开销,而谓词指令仅导致“指令执行”延迟进行条件测试并设置谓词”。为什么?

最佳答案

指令谓词是指线程根据谓词有条件地执行指令。谓词为真的线程执行指令,其余的什么都不做。

例如:

var = 0;

// Not taken by all threads
if (condition) {
var = 1;
} else {
var = 2;
}

output = var;

会导致(不是实际的编译器输出):
       mov.s32 var, 0;       // Executed by all threads.
setp pred, condition; // Executed by all threads, sets predicate.

@pred mov.s32 var, 1; // Executed only by threads where pred is true.
@!pred mov.s32 var, 2; // Executed only by threads where pred is false.
mov.s32 output, var; // Executed by all threads.

总而言之,这是 if 的 3 条指令,无 fork 。非常有效率。

带有分支的等效代码如下所示:
       mov.s32 var, 0;       // Executed by all threads.
setp pred, condition; // Executed by all threads, sets predicate.

@!pred bra IF_FALSE; // Conditional branches are predicated instructions.
IF_TRUE: // Label for clarity, not actually used.
mov.s32 var, 1;
bra IF_END;
IF_FALSE:
mov.s32 var, 2;
IF_END:
mov.s32 output, var;

注意它有多长( if 的 5 条指令)。条件分支需要禁用部分经线,执行第一条路径,然后回滚到经线发散的点并执行第二条路径,直到两者收敛。它需要更长的时间,需要额外的簿记,更多的代码加载(特别是在有很多指令要执行的情况下),因此需要更多的内存请求。所有这些都使分支比简单的预测更慢。

实际上,在这种非常简单的条件赋值的情况下,编译器可以做得更好,只有 2 条指令用于 if :
mov.s32 var, 0;       // Executed by all threads.
setp pred, condition; // Executed by all threads, sets predicate.
selp var, 1, 2, pred; // Sets var depending on predicate (true: 1, false: 2).

关于cuda - 分支指令和谓词指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com