Story 008 - I Thought AVX-512 Would Always Win

Published:

The Intel Core i5-1135G7 is a four-core Tiger Lake processor designed for thin-and-light laptops. Yet, somewhat unexpectedly, it supports AVX-512, a SIMD instruction-set extension that disappeared from Intel’s mainstream hybrid Core lineup with the introduction of Alder Lake. When I realized that my everyday machine could execute 512-bit vector instructions, I immediately wanted to see what they could do to the FP32 kernels in my fortran-nlp-kernels project. If AVX2 could process eight FP32 values at once, AVX-512 could process sixteen, so the wider configuration should obviously be faster. With this mental model, I approached the experiment with a brute-force theory of performance: compile the same kernel with the same compiler for scalar, AVX2, and AVX-512 execution, then watch the runtime fall as the vector registers became wider.

So here is how it went:

  • I wrote this fortran benchmark program which called the cpu native inner_product subroutine of fortran-nlp-kernels. Using gfortran flags for manual control, I then compiled the same program into 3 binaries: one restricted to scalar code, one allowed to generate AVX2 instructions with up to 256-bit vectors, and one targeting AVX-512 with 512-bit width explicitly requested.
  • I profiled each binary with Intel VTune Profiler and looked at the metrics from Hotspots and Microarchitectural Exploration results. I found that AVX2 and AVX-512 were not faster than scalar. I saw the Vector Capacity usage, which was at ~6% for scalar, rising to only ~11% for both AVX2 and AVX-512. After inspecting the assembly code in each, I saw that the reduction-heavy math of the inner product was not being optimized properly in the SIMD mode.
  • That led me to find out if a different compiler could do a better job at generating optimized SIMD instructions. I chose the ifx- Intel’s LLVM-based Fortran compiler. I ran the same 3 binaries compilation and profiled each of them. Here I saw an improvement: the Vector Capacity Usage rose from ~6% for scalar to ~49% in both AVX2 and AVX-512. But theoretically, it should have been closer to 100% for AVX-512. After going through this document, which was really interesting btw, I learnt accurately about how this “Advanced Vector Extensions” was actually made, from instructions down to the silicon.
  • Now I knew I needed to inspect the assembly of these binaries and find the exact type of registers they were using. (Heads up: XMM registers are 128-bit wide and are used for scalar operations, YMM registers are used for AVX2 and are 256-bit wide, and the ZMM registers are used for the full 512-bit wide AVX-512). It was fascinating to learn that these are not physically separate registers: my processor has 32 ZMM registers, 32 YMM registers, and 32 XMM registers, but it still only has 32 total registers. Turns out the XMM register is just the lower 128 bits of the YMM register, and the YMM register is just the lower 256 bits of the full 512-bit ZMM registers.
  • So now, inspecting the assembly of the AVX-512 binary that was supposed to use the ZMM register, it was still using only the YMM register. After a quick research I found this compiler flag -mprefer-vector-width=512 which forces the compiler to explicitly generate instructions that use the ZMM registers.
  • Now I recompiled the source program using both gfortran and ifx for AVX-512 SIMD and to explicitly use the ZMM registers using the new flags. Now the ifx compiled binary hit ~96% Vector Capacity Usage, but the gfortran-compiled binary was still at ~11%. After inspecting the assembly, both were now using ZMM.

So, the lesson so far is that having capable hardware is impressive, but without the ability to utilize it, it’s like the hardware does not even exist. After studying the generated assemblies for AVX-512(ZMM), while ifx generated optimized instructions to perform the same math: reorganizing the reductions around several independent partial sums and delaying the final horizontal reduction until after the main loop; the gfortran version, while it did widen the multiplication, performed lane extraction and scalar accumulation within every vector loop iteration. The generated loops therefore retained one persistent accumulator and added repeated shuffle, extraction, and scalar-add operations. Below is a simplified representation:

gfortran compiled binary for inner product math that uses ZMM registers:
For every 16-element vector chunk, gfortran multiplied the corresponding elements, reduced all 16 lanes into a scalar, and immediately added that scalar to the same persistent accumulator:

p_t = x_{16t+1}y_{16t+1} + x_{16t+2}y_{16t+2} + … + x_{16t+16}y_{16t+16} s_t = s_t + p_t

This created one serial dependency chain:

s0 → s1 → s2 → ··· → sd.

ifx compiled binary for inner product math that uses ZMM registers:
The ifx binary maintained 4 independent partial sums:

s(0) = x1y1 + x5y5 + x9y9 + ··· , s(1) = x2y2 + x6y6 + x10y10 + ··· (2) = x3y3 + x7y7 + x11y11 + ··· , s(3) = x4y4 + x8y8 + x12y12 + ··· .

Only after the main loop were the four partial sums combined:

s =s(0) +s(1) +s(2) +s(3)

Essentially, gfortran used 2 ZMM working registers, 0 persistent ZMM accumulators, and 1 scalar XMM accumulator, and ifx used 4 persistent ZMM registers.


Now I thought of changing the math. Of course, now it would be matrix multiplication.

To measure the performance of executing matrix multiplication directly on the SIMD hardware, I could not use any BLAS or OpenMP implementation. So I wrote a native SGEMM kernel using a three-level Fortran loop:

C = 0.0_real32
do j = 1, n
  do k = 1, n
    do i = 1, n
      C(i , j ) = C(i , j) + A(i ,k) ∗ B(k, j)
    end do
  end do
end do

Here we go again:

  • Compile the same kernel using the combination of two compilers and their flags, generate 6 assemblies, profile each, and analyse them.
  • Now, with ifx and even with gfortran, the Vector Capacity usage jumped from ~6% for scalar to 50% for AVX2, and a full 100% for AVX-512 (explicit ZMM).

Finally, I was using what I had paid for. Turns out the higher arithmetic intensity of matrix multiplication was exercising my SIMD hardware.

I started this experiment thinking AVX-512 would always win. But the results taught me that wider SIMD instructions don’t automatically translate into better performance.

The compiler’s ability to expose parallelism, the structure of the mathematical operation, and the instructions generated all matter. The inner product and matrix multiplication demonstrated this in very different ways.

Hardware provides the capability. The compiler and the workload determine how much of that capability becomes actual performance.

And that’s what made this experiment worth doing.

This experiment became the technical report .