1#ifndef PARALLELIZED_MATRIX_H
2#define PARALLELIZED_MATRIX_H
15std::vector<std::vector<int64_t>> add(std::vector<std::vector<int64_t>>& v1,
16 std::vector<std::vector<int64_t>>& v2) {
24 int64_t n = std::min(v1.size(), v2.size()), m = std::min(v1[0].size(), v2[0].size());
26 std::vector<std::vector<int64_t>> result(n, std::vector<int64_t>(m, 0));
28 for (
int i = 0; i < n; i++) {
29 for (
int j = 0; j < m; j++) {
30 result[i][j] = v1[i][j] + v2[i][j];
40void compute(std::vector<std::vector<int64_t>>& result, std::vector<std::vector<int64_t>>& v1,
41 std::vector<std::vector<int64_t>>& v2,
const int64_t i,
const int64_t j) {
42 int64_t m = std::min(v1[0].size(), v2[0].size());
44 for (
int k = i; k < j; k++) {
45 for (
int w = 0; w < m; w++) {
46 result[k][w] = v1[k][w] + v2[k][w];
51std::vector<std::vector<int64_t>> add(std::vector<std::vector<int64_t>>& v1,
52 std::vector<std::vector<int64_t>>& v2) {
60 int64_t n = std::min(v1.size(), v2.size()), m = std::min(v1[0].size(), v2[0].size());
62 std::vector<std::vector<int64_t>> result(n, std::vector<int64_t>(m, 0));
64 std::vector<std::future<void>> threads;
66 int batch_size = n / TOTAL_THREADS;
67 for (
int i = 0; i < TOTAL_THREADS; i++) {
68 int start = i * batch_size;
69 int end = (i == TOTAL_THREADS - 1) ? n : start + batch_size;
70 threads.push_back(std::async(std::launch::async, compute, std::ref(result), std::ref(v1),
71 std::ref(v2), start, end));