[eigen] Eigen slower than simple for loop. |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: [eigen] Eigen slower than simple for loop.
- From: Janos Meny <janos.meny@xxxxxxxxxxxxxx>
- Date: Tue, 24 Sep 2019 22:23:38 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=mime-version:reply-to:from:date:message-id:subject:to; bh=XfD3xB1YMKftySxIUPQO8/HJtL5FZ+SlIvI2lApjlq4=; b=gS1/0MkduVGK8xsc0FtHKS4uEz0/8O2d11NaaRWwgY72HOMzxa2pJJct9cjdnPgnxh q1av4pN0sXFHdG/ZYWxoGnU8sRP1lY5Samb3jfUkJqZI9pe1cYEmhtfPlv4ttqagcC2i tOnSK8Aa175sPwlZBGqLt1qQVAhzWF2AiQPJHvNktfvQntq4dl1ASdphXCxsOz3KuNxj gnAUI/8RRUN/Lu2Fp4YENfEcUiPKx/PTidpKzVGSVj01T1hE8kZoLU1JXIuvBU1TUySy qkGldiOp+ohdx61inFZnVOcu6SzC2TleSYh9ySsByxT9z1IHyQ2LWP94n3vpv6oZdxDO SeFg==
Hey,
I wanted to direct your attention to the following question on stackoverflow:
I also measured this on my machine and got the same result: Eigen ist always twice as slow as the simple for loop (I've attached the code from the question and added some code for measuring).
What could be the reason for this?
Best regards
Janos
#include <chrono>
#include <Eigen/Core>
constexpr int dimension = 10;
using StaticMatrix = Eigen::Matrix<double, dimension, dimension>;
using VectorOfMatrices = std::vector<StaticMatrix, Eigen::aligned_allocator<StaticMatrix >>;
StaticMatrix CustomMultiply(const StaticMatrix& a, const StaticMatrix& b) {
StaticMatrix result = StaticMatrix::Zero(10, 10);
for (int bcol_idx = 0; bcol_idx < dimension; ++bcol_idx) {
for (int brow_idx = 0; brow_idx < dimension; ++brow_idx) {
result.col(bcol_idx).noalias() += a.col(brow_idx) * b(brow_idx, bcol_idx);
}
}
return result;
}
StaticMatrix PairwiseMultiplyEachMatrixNoAlias(int num_repetitions, const VectorOfMatrices& input) {
StaticMatrix acc = StaticMatrix::Zero(10, 10);
for (int i = 0; i < num_repetitions; ++i) {
for (const auto& matrix_a : input) {
for (const auto& matrix_b : input) {
acc.noalias() += matrix_a * matrix_b;
}
}
}
return acc;
}
StaticMatrix PairwiseMultiplyEachMatrixCustom(int num_repetitions, const VectorOfMatrices& input) {
StaticMatrix acc = StaticMatrix::Zero(10, 10);
for (int i = 0; i < num_repetitions; ++i) {
for (const auto& matrix_a : input) {
for (const auto& matrix_b : input) {
acc.noalias() += CustomMultiply(matrix_a, matrix_b);
}
}
}
return acc;
}
using my_clock = std::chrono::steady_clock;
using my_dur = std::chrono::duration<double>;
int main() {
VectorOfMatrices v1(100);
std::generate(v1.begin(), v1.end(), []{ return StaticMatrix::Random();});
auto start = my_clock::now();
auto r1 = PairwiseMultiplyEachMatrixCustom(100, v1);
auto end = my_clock::now();
my_dur d = end - start;
printf("Custom GEMM took: %f\n", d.count());
start = my_clock::now();
auto r2 = PairwiseMultiplyEachMatrixNoAlias(100, v1);
end = my_clock::now();
d = end - start;
printf("Eigen GEMM took: %f\n", d.count());
return 0;
}