Pau*_*l R 11
使用SIMD,如果你有一个例程,其中一些元素需要与其他元素不同地处理,那么你需要明确地处理屏蔽操作,以便它们仅应用于正确的元素.使用CUDA的SIMT架构,您可以在每个线程上获得控制流的错觉,因此您不需要明确屏蔽操作 - 当然,这仍然在"引擎盖下"发生,但是负担从程序员中解脱出来.
示例:假设您要将所有负数元素设置为零.在CUDA中:
if (X[tid] < 0)
X[tid] = 0; // NB: CUDA core steps through this instruction but only executes
// it if the preceding condition was true
Run Code Online (Sandbox Code Playgroud)
在SIMD(SSE)中:
__m128 mask = _mm_cmpge_ps(X, _mm_set1_ps(0)); // generate mask for all elements >= 0
X = _mm_and_ps(X, mask); // clear all elements which are < 0
Run Code Online (Sandbox Code Playgroud)