Floating-point numbers in computer architecture behave differently from real numbers in math. The order of operations matters, and compilers have to respect rules when it comes to optimization. Knowing how these rules work and how to guide the compiler affects performance and precision. In today’s post, we’ll break these rules down and discuss an important building block: fused multiply-add.
This post is day 1 of Stream HPC’s Advent of Kernel Optimization, posting every weekday until the 25th of December. Check out all posts so far here.
Floating-point calculations are quite complex for a processor to handle. Taking addition as an example, one must align the exponents of the operands, add the significands, normalize, round the result and handle special cases like infinities, NaNs, and denormals. To still deliver good throughput for floating-point math, processor designers employ techniques like instruction pipelining. With pipelining, the operations are broken down into stages, each stage can work in parallel with other stages.
Another way in which processors try to improve throughput of floating-point values is by using specialized circuits. Any software that deals with floating-point values generally has a lot of multiply-accumulate operations, for example a = a + b * c. When performing a multiplication followed by an addition, the intermediate result is rounded. Since the multiply-accumulate operation is so common, it is advantageous to create special hardware for it. A fused multiply-add (FMA) performs the multiplication an addition in one step, and rounding only once. This results in both reduced work and increased precision.
FMA on GPU
CPUs generally have FMA units, and so do most GPUs. Replacing a + b * c with an FMA instruction is called contraction. Whether the compiler is allowed to use expression contraction depends on the language rules and compiler settings. In some situations contraction will be applied by default, in others it needs to be explicitly enabled. An alternative is to call a direction function such as with std::fma or __fmaf_rn. Another issue is that floating-point math is generally not associative, meaning that (a + b) + c != a + (b + c). Hence, the compiler is not free to rearrange floating-point expressions to make better use of the FMA unit.
The assumptions compilers make are not uniform and understand what is enabled by default and what can be changed through options is crucial. Let’s have a look at how CUDA handles this. In the documentation we can see that FMA is actually enabled by default, and it can be disabled with --fmad=false. In Compiler Explorer we can see that when it’s enabled, an FFMA SASS instruction is generated and if it’s disabled there is an FMUL followed by an FADD.
Reordering
Being aware of these rules allows you to generate better code. Take the following example, also in Compiler Explorer.
__global__ void kernel_standard(float* out, float x, float a, float b, float c)
{
*out = a * x * x + b * x + c;
}
__global__ void kernel_nested(float* out, float x, float a, float b, float c)
{
*out = (a * x + b) * x + c;
}These kernels evaluate the same polynomial, but in a different way. However, with CUDA the nested kernel makes use of efficient FMA instructions and the standard kernel does not. While this is an isolated example, it shows that how you write instructions matters. It happens because of reordering rules: the CUDA compiler is not able to or not allowed to rewrite the expression to better use FMA (and --use_fast_math does not seem to help). This is in contrast to e.g. Clang, where -ffast-math implies -fassociative-math, which does allow to apply the distributive property. Compiling these kernels as HIP code with Clang and fast math does yield the same instructions for both kernels. Note that these settings and behavior only specify what is allowed. Whether the compiler actually changes the order of operations or contracts them depends on heuristics and target architecture.
Note that using FMA doesn’t always mean you are getting improved performance. Contracting a multiplication and an addition to an FMA instruction does remove one instruction and possibly an intermediate register. However, pipelining also exists on GPU and not all pipelines may be able to perform FMA. So what other computation is performed in the kernel matters. While it does give improved precision, this may change results and whether that is desirable or not depends on the requirements of your kernel.
As with all things, measure and benchmark. Hopefully this post made clear that how you write your floating-point operations and what options are passed to the compiler influence what instructions the compiler is allowed to generate.