Eigen: what's going on during a matrix multiplication?

cplusplus eigen gemm

A short TIL – from a while ago, but I was reminded of it recently. Eigen has a page that describes how Eigen implements generalized matrix multiplation (GEMM) and how to write code such that it is optimal and doesn’t create extra temporaries.

I’ll copy one of their examples, the first:

m1 += m2 * m3;

Is a line that many write in C++, me included. BUT, writing in this way produces a temporary within Eigen, like

temp = m2 * m3;
m1 += temp; 

And if you want fast code, trimming all extra calls is part of that process. How can a GEMM be done better?

m1.noalias() += m2 * m3;  

with the noalias() function, which explicitly tells Eigen that the left-side variable does not occur on the right side.

However! There are ways to mess up with noalias() – the programmer is vouching for relationships between the variables. If a variable on the left is also on the right, and you use noalias(), the result will be wrong, and noalias() is only effective for GEMM statements. Sigh, I love C++.

© Amy Tabb 2018 - 2023. All rights reserved. The contents of this site reflect my personal perspectives and not those of any other entity.