forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
288 lines (222 loc) · 7.23 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// This program computes the dot products over a set of independent vectors
// and compares the runtime between baseline (sequential), OpenMP, C++ thread,
// and Taskflow implementations.
#include "taskflow.hpp"
#include <random>
#include <numeric>
#include <fstream>
using matrix_t = std::vector<std::vector<float>>;
// ----------------------------------------------------------------------------
// Utility section
// ----------------------------------------------------------------------------
// Function: random_matrix
matrix_t random_matrix(size_t N) {
thread_local std::default_random_engine gen(0);
std::normal_distribution<float> d{0.0f, 1.0f};
std::ostringstream oss;
oss << "|----> generating " << N << "x" << N << " matrix by thread "
<< std::this_thread::get_id() << "\n";
std::cout << oss.str();
matrix_t mat(N);
for(auto& r : mat) {
r.resize(N);
for(auto& c : r) {
c = d(gen);
}
}
return mat;
}
// Operator: multiplication
matrix_t operator * (const matrix_t& A, const matrix_t& B) {
if(A.empty() || B.empty() || A[0].size() != B.size()) {
std::cout << A[0].size() << " " << B.size() << std::endl;
throw std::runtime_error("Dimension mismatched in matrix multiplication\n");
}
size_t M, K, N;
N = A.size();
K = A[0].size();
M = B[0].size();
printf("A[%lux%lu] * B[%lux%lu]\n", N, K, K, M);
// Initialize the matrix
matrix_t ret(N);
for(auto& r : ret) {
r.resize(M);
for(auto& c : r) {
c = 0.0f;
}
}
// Matrix multiplication
for(size_t i=0; i<N; ++i) {
for(size_t j=0; j<M; ++j) {
for(size_t k=0; k<K; ++k) {
ret[i][j] += A[i][k] * B[k][j];
}
}
}
return ret;
}
// ----------------------------------------------------------------------------
// Task section
// ----------------------------------------------------------------------------
// Procedure: baseline
void baseline(const std::vector<size_t>& D) {
std::cout << "========== baseline ==========\n";
auto tbeg = std::chrono::steady_clock::now();
std::cout << "Generating matrix As ...\n";
std::vector<matrix_t> As(D.size());
for(size_t j=0; j<D.size(); ++j) {
As[j] = random_matrix(D[j]);
}
std::cout << "Generating matrix Bs ...\n";
std::vector<matrix_t> Bs(D.size());
for(size_t j=0; j<D.size(); ++j) {
Bs[j] = random_matrix(D[j]);
}
std::cout << "Computing matrix product values Cs ...\n";
std::vector<matrix_t> Cs(D.size());
for(size_t j=0; j<D.size(); ++j) {
Cs[j] = As[j] * Bs[j];
}
auto tend = std::chrono::steady_clock::now();
std::cout << "Baseline takes "
<< std::chrono::duration_cast<std::chrono::milliseconds>(tend-tbeg).count()
<< " ms\n";
}
// Procedure: openmp
void openmp(const std::vector<size_t>& D) {
std::cout << "========== OpenMP ==========\n";
auto tbeg = std::chrono::steady_clock::now();
std::cout << "Generating matrix As ...\n";
std::vector<matrix_t> As(D.size());
#pragma omp parallel for
for(size_t j=0; j<D.size(); ++j) {
As[j] = random_matrix(D[j]);
}
std::cout << "Generating matrix Bs ...\n";
std::vector<matrix_t> Bs(D.size());
#pragma omp parallel for
for(size_t j=0; j<D.size(); ++j) {
Bs[j] = random_matrix(D[j]);
}
std::cout << "Computing matrix product values Cs ...\n";
std::vector<matrix_t> Cs(D.size());
#pragma omp parallel for
for(size_t j=0; j<D.size(); ++j) {
Cs[j] = As[j] * Bs[j];
}
auto tend = std::chrono::steady_clock::now();
std::cout << "OpenMP takes "
<< std::chrono::duration_cast<std::chrono::milliseconds>(tend-tbeg).count()
<< " ms\n";
}
// Procedure: cppthread
void cppthread(const std::vector<size_t>& D) {
std::cout << "========== CppThread ==========\n";
auto tbeg = std::chrono::steady_clock::now();
tf::Threadpool tpl(std::thread::hardware_concurrency());
std::cout << "Generating matrix As ...\n";
std::vector<matrix_t> As(D.size());
std::vector<std::future<void>> futures;
for(size_t j=0; j<D.size(); ++j) {
futures.push_back(tpl.async([&, j] () { As[j] = random_matrix(D[j]); }));
}
std::cout << "Generating matrix Bs ...\n";
std::vector<matrix_t> Bs(D.size());
for(size_t j=0; j<D.size(); ++j) {
futures.push_back(tpl.async([&, j] () { Bs[j] = random_matrix(D[j]); }));
}
std::cout << "Synchronizing As and Bs ...\n";
for(auto& fu : futures) {
fu.get();
}
futures.clear();
std::cout << "Computing matrix product values Cs ...\n";
std::vector<matrix_t> Cs(D.size());
for(size_t j=0; j<D.size(); ++j) {
futures.push_back(tpl.async([&, j] () { Cs[j] = As[j] * Bs[j]; }));
}
std::cout << "Synchronizing Cs ...\n";
for(auto& fu : futures) {
fu.get();
}
auto tend = std::chrono::steady_clock::now();
std::cout << "CppThread takes "
<< std::chrono::duration_cast<std::chrono::milliseconds>(tend-tbeg).count()
<< " ms\n";
}
// Procedure: taskflow
void taskflow(const std::vector<size_t>& D) {
auto tbeg = std::chrono::steady_clock::now();
using builder_t = typename tf::Taskflow::Task;
tf::Taskflow tf;
std::cout << "Generating task As ...\n";
std::vector<matrix_t> As(D.size());
std::vector<builder_t> TaskAs;
for(size_t j=0; j<D.size(); ++j) {
TaskAs.push_back(tf.silent_emplace([&, j] () {
As[j] = random_matrix(D[j]);
}));
}
std::cout << "Generating task Bs ...\n";
std::vector<matrix_t> Bs(D.size());
std::vector<builder_t> TaskBs;
for(size_t j=0; j<D.size(); ++j) {
TaskBs.push_back(tf.silent_emplace([&, j] () {
Bs[j] = random_matrix(D[j]);
}));
}
std::cout << "Generating task Cs ...\n";
std::vector<matrix_t> Cs(D.size());
std::vector<builder_t> TaskCs;
for(size_t j=0; j<D.size(); ++j) {
TaskCs.push_back(tf.silent_emplace([&, j] () {
Cs[j] = As[j] * Bs[j];
}));
}
// Build task dependency
for(size_t j=0; j<D.size(); ++j) {
TaskCs[j].gather({TaskAs[j], TaskBs[j]});
}
tf.wait_for_all();
auto tend = std::chrono::steady_clock::now();
std::cout << "Taskflow takes "
<< std::chrono::duration_cast<std::chrono::milliseconds>(tend-tbeg).count()
<< " ms\n";
}
// ------------------------------------------------------------------------------------------------
// Function: main
int main(int argc, char* argv[]) {
if(argc != 3) {
std::cerr << "usage: ./matrix [baseline|openmp|cppthread|taskflow] N\n";
std::exit(EXIT_FAILURE);
}
// Create a unbalanced dimension for vector products.
const auto N = std::stoul(argv[2]);
std::vector<size_t> dimensions(N);
std::default_random_engine engine(0);
std::uniform_int_distribution dis(1, 1000);
std::cout << "matrix sizes = [";
for(size_t i=0; i<dimensions.size(); ++i) {
dimensions[i] = dis(engine);
if(i) std::cout << ' ';
std::cout << dimensions[i];
}
std::cout << "]\n";
// Run methods
if(std::string_view method(argv[1]); method == "baseline") {
baseline(dimensions);
}
else if(method == "openmp") {
openmp(dimensions);
}
else if(method == "cppthread") {
cppthread(dimensions);
}
else if(method == "taskflow") {
taskflow(dimensions);
}
else {
std::cerr << "wrong method, shoud be [baseline|openmp|cppthread|taskflow]\n";
}
return 0;
}