如何在 Cg 中实现高效的并行 SIMD 比较和选择?

发布于 2024-12-04 07:51:56 字数 358 浏览 4 评论 0原文

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

例如,鉴于此标量代码,是否可以编写它,因此CG编译器将使代码在并行 / SIMD中执行(并且还使用BranchFree Selection使用电位)。

            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 ;

How do you do parallel selection efficiently ?

For example, given this scalar code, is there a way to write it so the Cg compiler will make the code execute in parallel / SIMD (and potential using a branchfree selection as well).

            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 ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2024-12-11 07:51:56

显然,我错过了 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 操作。

Apparently, I missed these line in the Cg manual:

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

So I tried this out and it seems to work:

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

I guess I didn't expect the simplest solution to just work!

My coworker who is a graphics programmer also suggested that on some platforms, the Cg compiler might be intelligent enough to optimize the original source code for me but that it's not guaranteed and it is always better to explicitly specify parallel SIMD operations if possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文