Two Kinds of Vectorization
emmtrix Tech Posts
Category: Vectorization
In our field of compilers, programming languages and code generation we are working close to (embedded) hardware but also on the more abstract modelling of algorithms. Interestingly, the term ‘vectorization’ can be used here for two different things. Our most common usage is to describe the conversion of code to make use of SIMD (single instruction multiple data) instructions. For instance, a vector accelerator with 512 bit can execute 8 operations with two operands of 32 bits with a single instruction. Replacing a loop with these 8 operations by a single instruction is called (automatic) vectorization.
An example: The following loop
for (i = 0u; i < 16; i = i + 1) {
sum[i] = 0.0;
}
is replaced by the following instruction
simd_store_linear(&sum[(int)0u], simd_broadcast<16>((float)0.0));
When working with array-based programming languages like MATLAB®, the term vectorization is used to describe the use of vectors directly instead of modelling the desired behavior using loops. In the following example, the code computes the sine of 1,001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end