110. Summing two arrays unrolled via the Vector API
In this problem, we take the example of summing two arrays from the previous problem and re-write the loop in an unrolled fashion.
Loop unrolling can be applied manually (as we will do here) or by the compiler, and it stands for an optimization technique meant to reduce the loop iteration count.
In our case, in order to reduce the loop iteration count, we use more vectors to repeat the sequence of loop body statements that are responsible for summing the items. If we know that our arrays are long enough to always require at least 4 loop iterations, then rewriting the code as follows will reduce the loop iterations by 4 times:
public static void sumUnrolled(int x[], int y[], int z[]) {
int width = VS256.length();
int i = 0;
for (; i <= (x.length - width * 4); i += width * 4) {
IntVector s1 = IntVector.fromArray(VS256, x, i)
.add(IntVector.fromArray(VS256, y, i));
IntVector s2 = IntVector.fromArray(VS256, x, i + width)
.add(IntVector.fromArray(VS256, y, i + width));
IntVector s3 = IntVector.fromArray(VS256, x, i + width * 2)
.add(IntVector.fromArray(VS256, y, i + width * 2));
IntVector s4 = IntVector.fromArray(VS256, x, i + width * 3)
.add(IntVector.fromArray(VS256, y, i + width * 3));
s1.intoArray(z, i);
s2.intoArray(z, i + width);
s3.intoArray(z, i + width * 2);
s4.intoArray(z, i + width * 3);
}
for (; i < x.length; i++) {
z[i] = x[i] + y[i];
}
}
Consider the following x
and y
vectors:
x = {3, 6, 5, 5, 1, 2, 3, 4, 5, 6, 7, 8, 3, 6, 5, 5, 1, 2, 3,
4, 5, 6, 7, 8, 3, 6, 5, 5, 1, 2, 3, 4, 3, 4};
y = {4, 5, 2, 5, 1, 3, 8, 7, 1, 6, 2, 3, 1, 2, 3, 4, 5, 6, 7,
8, 3, 6, 5, 5, 1, 2, 3, 4, 5, 6, 7, 8, 2, 8};
int[] z = new int[x.length];
Calling the sumPlus(x, y, z)
method written in the previous problem will require 4 loop iterations to complete. Calling sumUnrolled(x, y, z)
will require a single iteration to complete.