Eigen is a very convenient library that can express the mathematic formula in a concise and human-intuitive way. I know that Eigen has lazy evaluation concepts that can represent a chain of operations in expression classes and efficiently evaluate it when necessary. I also know that Eigen can be used with MKL. However, I'm curious about what kind of expressions can be transferred into MKL cblas calls. Under what condition, they are not transferable? Are there any general rules that can help me figure out what is transferable?
Typically, I'm curious about the following cases:
MatrixXd A, B, C;
VectorXd a, b, c;
double w1, w2, w3;
b += w1 * A * a; // can be done through dgemv
b += w1 * A.transpose() * a; // can be done through dgemv
C += w2 * A * B; // can be done through dgemm
C += w2 * A.transpose() * B; // can be done through dgemm
C += w2 * A. topRows(5).transpose() * B; // can be done through dgemm
D = A * B * C; // cannot be done in one func call through cblas
Note: comments are not Eigen transfer result. Instead, it is the ideal result. I'm not sure whether Eigen can transfer them.
There is also another question along with it: when will Eigen allocate temporary memory in those chain of operations? Are there any general rules that can help me figure out whether there is any allocation happened?
To complete chtz answer, all your hypothesis are true (provided you add the .noalias()
decorator), and more generally any expression or subexpression that looks like a gemv/gemm will be turned to a single call, including transpose, blocks, conjugate, etc. You can look for examples in this unit test counting the number of temporary, so 0
means a single blas-like call, 1
means that a temporary will be created before or after the blas-like call, etc.