mm2.cpp 815 Bytes
Newer Older
Ignacio Labarca Figueroa's avatar
Ignacio Labarca Figueroa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Programming Techniques for Scientific Simulations I
 * HS 2020
 * Exercise 10
 */

#include "matrix_multiplication.hpp"
#include <cmath> // for std::min

// Blocking
void mm2(matrix_t const & A, matrix_t const & B, matrix_t & C, std::size_t N) noexcept {
	constexpr std::size_t L1 = 1 << 15;
	constexpr std::size_t n = std::sqrt(L1 / (3.0 * sizeof(double)));

	for (std::size_t j = 0; j < N; j += n) {
		for (std::size_t k = 0; k < N; k += n) {
			for (std::size_t i = 0; i < N; i += n) {
				// Macro-kernel
				for (std::size_t jj = j; jj < std::min(j + n, N); ++jj) {
					for (std::size_t kk = k; kk < std::min(k + n, N); ++kk) {
						auto b = B[kk + jj * N];
						for (std::size_t ii = i; ii < std::min(i + n, N); ++ii) {
							C[ii + jj * N] += A[ii + kk * N] * b;
						}
					}
				}
			}
		}
	}
}