Summing two arrays unrolled via Vector API – Arrays, collections and data structures

0 Comments 6:58 AM

104. Summing two arrays unrolled via 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 it 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 to sum 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 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 *
      .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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Benchmarking Vector API – Arrays, collections and data structuresBenchmarking Vector API – Arrays, collections and data structures

105. Benchmarking Vector API Benchmarking Vector API can be accomplished via JMH. Let’s consider three Java arrays (x, y, z) each of 50,000,000 integers, and the following computation: z[i] =

Adding more artifacts in a record Certification Exams of Java Java Exams Tackling guarded record patterns Tackling records in Spring Boot Understanding records serialization

Multiplying matrices via Vector API – Arrays, collections and data structuresMultiplying matrices via Vector API – Arrays, collections and data structures

107. Multiplying matrices via Vector API Let’s consider two matrices of 4×4 denoted as X and Y. The Z=X*Y is: Figure 5.10 – Multiplying two matrices (X * Y =

Certification Exams of Java Getting a list from a stream Java Exams Tackling guarded record patterns Tackling records in Spring Boot Understanding records serialization