Skip to content

Commit

Permalink
Optimize the matrix multiplication in NeuralNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
HanatoK committed Sep 19, 2023
1 parent 0292804 commit 72c0ea3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/colvar_neuralnetworkcompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,17 @@ std::vector<std::vector<double>> neuralNetworkCompute::multiply_matrix(const std
const size_t n = B.size();
if (A[0].size() != n) {
std::cerr << "Error on multiplying matrices!\n";
// std::cerr << fmt::format("Error on multiplying matrices ({}, {}) and ({}, {})!\n", m, A[0].size(), n, B[0].size());
}
const size_t t = B[0].size();
std::vector<std::vector<double>> C(m, std::vector<double>(t, 0.0));
for (size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < t; ++j) {
for (size_t k = 0; k < n; ++k) {
C[i][j] += A[i][k] * B[k][j];
for (size_t k = 0; k < n; ++k) {
const auto tmp = A[i][k];
auto& C_i = C[i];
auto& B_k = B[k];
for (size_t j = 0; j < t; ++j) {
C_i[j] += tmp * B_k[j];
}
}
}
Expand Down

0 comments on commit 72c0ea3

Please sign in to comment.