From e01d32fb228ec009e3b0787fb11f2eb9ce98bb9a Mon Sep 17 00:00:00 2001 From: Orange Date: Wed, 17 Sep 2025 17:12:41 +0300 Subject: [PATCH] Improves matrix multiplication performance Optimizes matrix multiplication by specializing the algorithm based on the matrix storage type (row-major or column-major). This change significantly improves performance by leveraging memory access patterns specific to each storage order. --- include/omath/linear_algebra/mat.hpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 6679cc79..6f5dfd8a 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -157,14 +157,25 @@ namespace omath { Mat result; - for (size_t i = 0; i < Rows; ++i) + if constexpr (StoreType == MatStoreType::ROW_MAJOR) + for (size_t i = 0; i < Rows; ++i) + for (size_t k = 0; k < Columns; ++k) + { + const Type aik = at(i, k); + for (size_t j = 0; j < OtherColumns; ++j) + result.at(i, j) += aik * other.at(k, j); + } + else if constexpr (StoreType == MatStoreType::COLUMN_MAJOR) for (size_t j = 0; j < OtherColumns; ++j) - { - Type sum = 0; for (size_t k = 0; k < Columns; ++k) - sum += at(i, k) * other.at(k, j); - result.at(i, j) = sum; - } + { + const Type bkj = other.at(k, j); + for (size_t i = 0; i < Rows; ++i) + result.at(i, j) += at(i, k) * bkj; + } + else + std::unreachable(); + return result; }