1

您如何有效地进行并行选择?

例如,给定这个标量代码,有没有办法编写它,以便 Cg 编译器使代码并行/ SIMD 执行(并且可能使用无分支选择)。

            Out.x = ( A.x <= threshold) ? B.x : C.x ;
            Out.y = ( A.y <= threshold) ? B.y : C.y ;
            Out.z = ( A.z <= threshold) ? B.z : C.z ;
            Out.w = ( A.w <= threshold) ? B.w : C.w ;
4

1 回答 1

0

显然,我错过了 Cg 手册中的这些行:

The ?:, ||, &&, &, and comparison operators can
be used with bool vectors to perform multiple
conditional operations simultaneously.

所以我尝试了这个,它似乎工作:

Out.xyzw = ( A.xyzw <= threshold) ? B.xyzw : C.xyzw ;

我想我没想到最简单的解决方案会起作用!

我的图形程序员同事还建议,在某些平台上,Cg 编译器可能足够智能,可以为我优化原始源代码,但不能保证,如果可能,最好明确指定并行 SIMD 操作。

于 2011-09-12T20:48:42.903 回答