- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有没有一种聪明的方法可以减少 CUDA 内核中 if 语句
的数量?
我正在编写一个应用程序来计算多体哈密顿量(量子系统的模拟)。计算高度依赖于条件表达式
。
我之所以要减少这些语句,是因为它们会带来性能开销。 (整个 warp
进入 if(){} else if(){}
语句的每个选项 - 如果条件不满足,thread
,在给定的时间内,保持不活动状态)。
问题:
1. switch()
语句能解决问题吗?
2. 下面的代码代表了一个总体思路:
class tag_option_1 {};
class tag_option_2 {};
class tag_option_3 {};
template<typename T> __device__
int cal_something(int ab, int cd)
{
return -12345; // error value. default case is an error.
};
template<> __device__
int cal_something<tag_option_1>(int ab, int cd)
{
// return something
}
template<> __device__
int cal_something<tag_option_2>(int ab, int cd)
{
// return something
}
template<> __device__
int cal_something<tag_option_3>(int ab, int cd)
{
// return something
}
////////////////////////////////
// version #1:
__global__
void calc_hamiltonian(int * foo, int * bar)
{
unsigned int tid = /* calce thread index*/;
// do something...
if (/* condition */)
{
cal_something<tag_option_1>(foo[tid], bar[tid]);
}
else if(/* condition */)
{
cal_something<tag_option_2>(foo[tid], bar[tid]);
}
else if(/* condition */)
{
cal_something<tag_option_3>(foo[tid], bar[tid]);
}
// no default case.
// do something...
}
////////////////////////////////
// version #2:
// "magical" way to select a version:
// variant is meant to be something like "boost::variant" but implementented without a single "if" statement.
// This "magical" step is meant to be resolved at compile time.
__devcie__
variant <tag_option_1, tag_option_2, tag_option_3>
version_selector(int ab, int cd)
{
// magic happens here.
}
__global__
void calc_hamiltonian(int * foo, int * bar)
{
unsigned int tid = /* calce thread index*/;
// do something...
cal_something <version_selector(foo[tid], bar[tid])> (foo[tid], bar[tid]);
// do something...
}
有没有办法实现上面示例的version #2
,或者在 CUDA C/C++
中是不可能的?
最佳答案
我总体上同意@njuffa 的建议,即不要试图人为地扭曲您的自然编码风格,并且您应该追求性能(以及可读性和可维护性)而不是计算源代码中的分支。特别是因为编译器有时可能会让它们消失。
话虽如此......
一些常识性的“聪明”方法来减少分支数量(在 CUDA 中和一般情况下):
举例说明。版本 1:
void foo(int* a, bool cond) {
...
for(int i = 0; i < lots; i++) {
if (cond) do_stuff()
else do_other_stuff();
}
...
}
bool cond = check_stuff();
foo(data, cond);
版本 2:
void foo(int* a, bool cond) {
...
if (cond) {
for(int i = 0; i < lots; i++) { do_stuff(); }
}
else {
for(int i = 0; i < lots; i++) { do_other_stuff(); }
}
...
}
bool cond = check_stuff();
foo(data, cond);
版本 3:
template <bool Cond>
void foo(int* a) {
...
if (cond) {
for(int i = 0; i < lots; i++) { do_stuff(); }
}
else {
for(int i = 0; i < lots; i++) { do_other_stuff(); }
}
...
}
bool cond = check_stuff();
if (cond) foo<true>(data) else foo<false>(data);
版本 4:
template <bool Cond>
void foo(int* a) {
...
for(int i = 0; i < lots; i++) {
if (cond) do_stuff()
else do_other_stuff();
}
...
}
bool cond = check_stuff();
if (cond) foo<true>(data) else foo<false>(data);
版本 3 和版本 4 的好处在于,虽然它看起来有一个分支,但实际上并没有——编译器要么只接受“then”语句,要么只接受“else”语句,但不在同一个函数中。
从版本 1 到版本 2 是编译器可以为您做的事情;但有时它并不像示例中那么简单,您必须自己处理。从版本 2 到版本 3 是编译器永远不会为你做的事情。转到版本 4 有点像绕了一圈,因为它看起来像版本 1,没有代码重复 - 但分支实际上仍然没有。
这并不总是——事实上,并不经常——有用,但马克哈里斯在他的 presentation 中给出了一个著名的例子。关于使用 CUDA 优化并行缩减。查看幻灯片 24-27 中的优化 #6。但是不要尝试那样的事情 - 它丑陋而且有点脆弱 - 除非你仔细地计时你的执行以确保它是值得的。
版本 1:
void foo(int* a, int *b) {
...
if (check(a[global_thread_index]) { b[global_thread_index]++; }
}
版本 2:
void foo(int* a, int *b) {
...
b[global_thread_index] += check(a[global_thread_index]);
}
(假设检查返回一个 bool 值,或者失败时返回整数 0,成功时返回 1。)
在这里我不太确定 CUDA 编译器会做什么;另外,您通过编写此代码并可能破坏 "principle of least astonishment" 来支付可读性损失.但您可以找到不那么做作的例子。
还有一个版本 3:
void foo(int* a, int *b) {
...
b[global_thread_index] = check(a[global_thread_index]) ? 1 : 0;
}
现在,这仍然有一个分支——三元运算符只是“if”的简写,但如果你能让你的代码达到这种状态,分歧将仅限于单个语句和每个分支,甚至可能更少,如果 CUDA 编译器设法使用 slct PTX statement :
slct
Select one source operand, based on the sign of the third operand.
这将分支的语义“包装”到单个指令的组合逻辑中。
当然,编译器在其他情况下可能会使用slct
;这不取决于你。
(另请参阅@RobertCrovella 的相同效果的评论。)
再次,举例说明。
void foo(int* a, int *b) {
...
if (threadIdx.x % 2 == 0) { do_stuff(); }
else { do_other_stuff(); }
...
}
版本 2:
void foo(int* a, int *b) {
...
if (threadIdx.x >= blockDim.x / 2) { do_stuff(); }
else { do_other_stuff(); }
...
}
这可确保所有经线(可能中间的经线除外)要么所有 channel 都满足条件,要么所有 channel 都不满足条件。这意味着在其他 channel 执行其他分支时,这些扭曲中的任何 channel 都将不得不闲置等待。
有关真实示例,请查看 Mark Harris 的 presentation 中的幻灯片 7-13。我在上面提到过。
关于c++ - 如何减少内核中分支/if 语句的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45570739/
我有几个带有视频和图像的 Bootstrap slider 。在 slider 之外,我想要一个可以转到包含视频的幻灯片的按钮。包含视频的幻灯片的数量因 slider 而异。我想做的是获取幻灯片的数量
我在编写一个查询时遇到了一些问题。 我有一个由文件及其大小(以字节为单位)组成的表。它看起来像这样: FileUrl | FileSize ------------------ xyz.docx |
我有一个带 iframe 的网站和一个带另一个 iframe 的网站,所以它是一个 iframe 内嵌另一个 iframe(都在不同的域上)。那么有没有办法从父div或父主div的url(parent
以下表达式在 JavaScript 中给出了特殊的结果。 typeof (5 + "7") // Gives string typeof (5 - "7") // Gives number 如
我有一个名为“交易”的表,每当有人在我的网站上进行购买时,我都会在其中输入用户 ID、购买类型和金额。 我想向每个用户显示过去 7 天的这些统计信息。 目前,我有这个: $data = array()
我一整天都在努力寻找解决这一挑战的办法。 我有一张 table : id | amount | type | date |
我正在尝试在 10 个数据节点的集群中测试 Map reduce 程序的性能。在此过程中,我使用了 5 个 Reducers,然后是 10 个等等。 我在想增加 reducer 的数量也会使工作完成得
我正在使用 html5 输入 type="number"。我想监视此输入的变化,但是: 因为在支持它的浏览器中 它有旋转控件 我不能只监视 .keyup, 因为我不想等待它失去焦点,所以我不能只监视
我的购物车表格有问题。我创建了一个如下所示的表格: SQL Fiddle 我的问题是我希望能够选择产品 ID,并计算该产品 ID 在表格中重复的次数,以便我可以显示用户在购物车中拥有的商品数量。 寻找
我使用许多包含来显示我网站的一小部分。使用许多 include 是否合适,或者我应该减少它们(尽可能多)。包含函数要多花多少时间? 我的主页加载速度很慢。有什么方法可以让它加载更快。 (我的主页每天在
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: length of array in function argument 我的数组大小是5。例如: arrC
是否有标准的 Python 方法来处理 Python 中的物理单位/数量?我看到了来自不同领域(如物理学或神经科学)的不同模块特定解决方案。但我更愿意使用标准方法而不是“孤岛”解决方案,因为其他人应该
基本上就像标题所说的那样,有没有办法从 JavaScript 程序中查看事件循环中当前存在多少个 promise ?最好在 Deno 上。 最佳答案 Deno v1.26 添加了一个内部 API,可用
我只是想知道大型项目-比如说航空公司的预订系统,它可能有多少类/对象。 对象:客户,飞机,机场,路线,机票,订单。这就是我能想到的。该项目可能是成千上万的代码行,那么是否可能会有更多的类(执行与对象无
如果有办法限制Scala中未处理的 future 数量,我将无法提供资金。 例如下面的代码: import ExecutionContext.Implicits.global for (i
从昨天开始,我一直在努力做到这一点,尽管还没有运气。我找到了解决方案,在我想要完成的事情上总是有细微的差别。 我试图获得所有可能的组合,稍微像这样:combination_k ,但我也希望相同的项目与
我正在尝试更新 1500 个 QuickBooks Online 库存项目的现有数量。我可以从商店中提取 1500 种产品。 这个更新可以做吗?我看到手头没有数量的物品: https://develo
我想与工作人员一起扩展应用程序。 可能有 1 名 worker 或 100 名 worker ,我想无缝扩展它们。 这个想法是使用副本集。然而,由于特定领域的原因,扩展它们的适当方法是让每个工作人员知
Android Studio 有没有办法显示 XML 布局中存在的 View 数量?众所周知,布局应该包含 <=80 个 View ,因此超过此值就会出现此警告,因此告知数量会非常有帮助。 Layou
虽然编码时总是出现有关 IBOutlet 保留计数的相同问题:从 NIB 取消归档对象后保留计数?何时对 IBOutlet 使用 @property?设置时保留还是分配? Mac 和 iPhone 之
我是一名优秀的程序员,十分优秀!