什么是“矢量化"? [英] What is "vectorization"?

查看:69
本文介绍了什么是“矢量化"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在好几次,我在 matlab、fortran ……其他一些……中遇到过这个术语,但我从来没有找到解释它是什么意思,它有什么作用?所以我在这里问,什么是矢量化,例如循环被矢量化"是什么意思?

Several times now, I've encountered this term in matlab, fortran ... some other ... but I've never found an explanation what does it mean, and what it does? So I'm asking here, what is vectorization, and what does it mean for example, that "a loop is vectorized" ?

推荐答案

许多 CPU 具有向量"或SIMD"指令集,它们同时将相同的操作应用于两个、四个或更多个数据段.现代x86芯片有SSE指令,很多PPC芯片有Altivec"指令,甚至有些ARM芯片有向量指令集,称为NEON.

Many CPUs have "vector" or "SIMD" instruction sets which apply the same operation simultaneously to two, four, or more pieces of data. Modern x86 chips have the SSE instructions, many PPC chips have the "Altivec" instructions, and even some ARM chips have a vector instruction set, called NEON.

向量化"(简化)是重写循环的过程,这样它不是处理数组的单个元素 N 次,而是同时处理(比如说)数组的 4 个元素 N/4 次.

"Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.

(我选择 4 是因为它是现代硬件最有可能直接支持的;术语向量化"也用于描述更高级别的软件转换,您可以完全抽象出循环并只描述对数组的操作组成它们的元素)

(I chose 4 because it's what modern hardware is most likely to directly support; the term "vectorization" is also used to describe a higher level software transformation where you might just abstract away the loop altogether and just describe operating on arrays instead of the elements that comprise them)

矢量化和循环展开的区别:考虑以下非常简单的循环,它将两个数组的元素相加并将结果存储到第三个数组中.

The difference between vectorization and loop unrolling: Consider the following very simple loop that adds the elements of two arrays and stores the results to a third array.

for (int i=0; i<16; ++i)
    C[i] = A[i] + B[i];

展开这个循环会将它变成这样:

Unrolling this loop would transform it into something like this:

for (int i=0; i<16; i+=4) {
    C[i]   = A[i]   + B[i];
    C[i+1] = A[i+1] + B[i+1];
    C[i+2] = A[i+2] + B[i+2];
    C[i+3] = A[i+3] + B[i+3];
}

另一方面,对其进行矢量化会产生如下结果:

Vectorizing it, on the other hand, produces something like this:

for (int i=0; i<16; i+=4)
    addFourThingsAtOnceAndStoreResult(&C[i], &A[i], &B[i]);

其中addFourThingsAtOnceAndStoreResult"是您的编译器用来指定向量指令的任何内在函数的占位符.请注意,某些编译器能够自动矢量化像这样的非常简单的循环,这通常可以通过编译选项启用.更复杂的算法仍然需要程序员的帮助才能生成好的向量代码.

Where "addFourThingsAtOnceAndStoreResult" is a placeholder for whatever intrinsic(s) your compiler uses to specify vector instructions. Note that some compilers are able to auto vectorize very simple loops like this, which can often be enabled via a compile option. More complex algorithms still require help from the programmer to generate good vector code.

这篇关于什么是“矢量化"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆