Add ex10

parent 3241a5f6
cmake_minimum_required (VERSION 3.15)
project (ex10-matrix-multiplication)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_EXTENSIONS FALSE)
add_compile_options (-Wall -Wextra -Wpedantic -march=native)
add_executable (main main.cpp mm0.cpp)
#include <iostream>
#include <chrono>
#include <cstdlib>
#include <random>
#include "matrix_multiplication.hpp"
double benchmark(function_t f, matrix_t const & A, matrix_t const & B, matrix_t & C, std::size_t N) noexcept {
for (std::size_t i = 0; i < N * N; ++i) {
C[i] = 0;
}
auto start = std::chrono::high_resolution_clock::now();
f(A, B, C, N);
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end - start).count();
}
int main() {
int const runs = 10;
int const N = 512;
matrix_t A(N * N);
matrix_t B(N * N);
matrix_t C(N * N);
std::mt19937 gen;
std::uniform_real_distribution<double> dis(0, 1);
for (std::size_t i = 0; i < N * N; ++i) {
A[i] = dis(gen);
B[i] = dis(gen);
}
for (int i = 0; i < runs; ++i) {
auto time = benchmark(mm0, A, B, C, N);
std::cout << "mm0," << N << ',' << time << '\n';
}
}
#pragma once
#include <cstdint>
#include <vector>
using value_t = double;
using matrix_t = std::vector<value_t>;
using function_t = void (*)(matrix_t const &, matrix_t const &, matrix_t &, std::size_t N);
void mm0(matrix_t const & A, matrix_t const & B, matrix_t & C, std::size_t N) noexcept;
#include "matrix_multiplication.hpp"
// Trivial implementation
void mm0(matrix_t const & A, matrix_t const & B, matrix_t & C, std::size_t N) noexcept {
for (std::size_t i = 0; i < N; ++i) {
for (std::size_t j = 0; j < N; ++j) {
for (std::size_t k = 0; k < N; ++k) {
C[i + j * N] += A[i + k * N] * B[j + k * N];
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment