AlgoPlus v0.1.0
Loading...
Searching...
No Matches
parallelized_matrix.h
1#ifndef PARALLELIZED_MATRIX_H
2#define PARALLELIZED_MATRIX_H
3
4#ifdef __cplusplus
5#include <cassert>
6#include <future>
7#include <iostream>
8#include <thread>
9#include <vector>
10#endif
11
12int TOTAL_THREADS = 2;
13
14namespace serial {
15std::vector<std::vector<int64_t>> add(std::vector<std::vector<int64_t>>& v1,
16 std::vector<std::vector<int64_t>>& v2) {
17 if (v1.empty()) {
18 return v2;
19 }
20 if (v2.empty()) {
21 return v1;
22 }
23
24 int64_t n = std::min(v1.size(), v2.size()), m = std::min(v1[0].size(), v2[0].size());
25
26 std::vector<std::vector<int64_t>> result(n, std::vector<int64_t>(m, 0));
27
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];
31 }
32 }
33
34 return result;
35}
36} // namespace serial
37
38namespace parallel {
39
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());
43
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];
47 }
48 }
49}
50
51std::vector<std::vector<int64_t>> add(std::vector<std::vector<int64_t>>& v1,
52 std::vector<std::vector<int64_t>>& v2) {
53 if (v1.empty()) {
54 return v2;
55 }
56 if (v2.empty()) {
57 return v1;
58 }
59
60 int64_t n = std::min(v1.size(), v2.size()), m = std::min(v1[0].size(), v2[0].size());
61
62 std::vector<std::vector<int64_t>> result(n, std::vector<int64_t>(m, 0));
63
64 std::vector<std::future<void>> threads;
65
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));
72 }
73
74 return result;
75}
76} // namespace parallel
77
78#endif