-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
173 lines (119 loc) · 3.5 KB
/
matrix.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <vector>
#include <algorithm>
#include <future>
#include <atomic>
#include <execution>
#include <chrono>
#include <random>
#include <ratio>
#include <numeric>
#include <functional>
#include <list>
#include "common.h"
using namespace std;
class Matrix {
int* data;
int rows;
int columns;
public:
Matrix(int r, int c) {
rows = r;
columns = c;
data = new int[rows * columns];
fill(data, data + rows * columns, 0);
}
void set_values(int i, int j, int value) {
data[i * columns + j] = value;
}
void set_all(int value) {
fill(data, data + columns * rows, value);
}
static void multiply(Matrix* x, Matrix* y, Matrix* results) {
if (!(x->columns == y->rows) || !(x->rows == results->rows) && (y->columns == results->columns))
{
cout << "Error: Invalid dimensions of matrix for multiplication" << endl;
}
int r = results->rows * results->columns;
for (size_t i = 0; i < r; i++) {
for (size_t j = 0; j < x->columns; j++)
{
for (size_t j = 0; j < x->columns; j++)
{
results->data[i] += x->data[(i / results->columns) * x->columns + j] * y->data[i % results->rows + j * y->columns];
}
}
}
}
static void parallel_multiply(Matrix* x, Matrix* y, Matrix* results) {
struct process_data_chunk {
void operator()(Matrix* x, Matrix* y, Matrix* results, int start_index, int end_index)
{
for (size_t i = start_index; i < end_index; i++) {
for (size_t j = 0; j < x->columns; j++)
{
for (size_t j = 0; j < x->columns; j++)
{
results->data[i] += x->data[(i / results->columns) * x->columns + j] * y->data[i % results->rows + j * y->columns];
}
}
}
}
};
int length = results->rows * results->columns;
if (!length)
return;
int min_per_thread = 10000;
int max_threads = (length + min_per_thread + 1) / min_per_thread;
int hardware_threads = thread::hardware_concurrency();
int num_threads = min(hardware_threads != 0 ? hardware_threads : 2, max_threads);
int block_size = length / num_threads;
vector<thread> threads(num_threads - 1);
int block_start = 0;
int block_end = 0;
{
join_threads joiners(threads);
for (unsigned i = 0; i < (num_threads - 1); i++) {
block_start = i * block_size;
block_end = block_start + block_size;
threads[i] = thread(process_data_chunk(), results, x, y, block_start, block_end);
}
process_data_chunk()(results, x, y, block_end, length);
}
}
void print()
{
if (rows < 50 && columns < 50)
{
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < columns; j++)
{
std::cout << data[j + i * columns] << " ";
}
std::cout << "\n";
}
std::cout << std::endl;
}
}
};
int main() {
Matrix A(200, 200);
Matrix B(200, 200);
Matrix results(200, 200);
A.set_all(3);
B.set_all(4);
auto startTime = chrono::high_resolution_clock::now();
Matrix::multiply(&A, &B, &results);
auto endTime = chrono::high_resolution_clock::now();
cout << "SEQUENTIAL MATRIX: " << chrono::duration_cast<chrono::microseconds>(endTime - startTime).count() << endl;
Matrix C(200, 200);
Matrix D(200, 200);
Matrix results2(200, 200);
C.set_all(5);
D.set_all(6);
chrono::steady_clock::time_point parStartTime = chrono::high_resolution_clock::now();
Matrix::parallel_multiply(&C, &D, &results2);
chrono::steady_clock::time_point parEndTime = chrono::high_resolution_clock::now();
cout << "PARALLEL MATRIX: " << chrono::duration_cast<chrono::microseconds>(parEndTime - parStartTime).count() << endl;
}