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

And using vectorized code looks like this:

t = 0:01:10;

y= sin (t);
While the basic principle is the same in both cases, we are looking at both vectorizations with different goals: Using SIMD instructions for vector accelerators can speed up the execution of a program by a factor of 10 or more. On the other hand, we are currently working on ways to use vectorization in MATLAB® to reduce the code size of the generated C code. Enforcing the use of more loops and data structures reduces the number of instructions and thus the size of the actual code.
Cookie Consent with Real Cookie Banner