Java Vector API
Java Vector API lets you explicitly write SIMD computations that compile to vector instructions on supported CPUs. While the JVM's auto-vectorization can also use these instructions, it is constrained by what it can infer about the algorithm - explicit vectorization gives you full control.
The API has been an incubator module since Java 16, and as of Java 24 it is in its ninth incubation iteration. It is deliberately kept in incubator status while the final API design awaits value type support from Project Valhalla. You need to explicitly include the module with the following JVM argument:
--add-modules jdk.incubator.vector
Matrix Multiplication & Transform
Assignment
Your task is to vectorize matrix multiplication with an additional transform step at the end. The scalar implementation is available in MatrixMultiplyScalar.java.
The transform zeroes out every element that is strictly below the average of its row. For example, given:
$$A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, \quad B = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}$$
The product is:
$$A \times B = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}$$
Applying the transform row by row:
- Row 0: $\text{avg} = \frac{19 + 22}{2} = 20.5$, since $19 < 20.5$, zeroed $\Rightarrow [0,\ 22]$
- Row 1: $\text{avg} = \frac{43 + 50}{2} = 46.5$, since $43 < 46.5$, zeroed $\Rightarrow [0,\ 50]$
Final result:
$$\begin{pmatrix} 0 & 22 \\ 0 & 50 \end{pmatrix}$$
Submit only zipped MatrixMultiplyVectorized.java.
Points are awarded based on how close your solution is to the reference implementation, so partial credit is given. Outperforming the reference earns a bonus point.
Your solution must achieve a speedup through vectorization. Scalar implementations and parallelized (non-vectorized) algorithms will not be accepted.
Automatic evaluation runs a lot of iterations to ensure stable and fair speed comparisons, so results may take a few minutes to appear.
Materials
The documentation can be seen in the appropriate JEPs: 338, 414, 417, 426, 438.
Or you can look at this video.
Slides from the seminar slides.