forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
198 lines (153 loc) · 4.86 KB
/
matrix.cpp
File metadata and controls
198 lines (153 loc) · 4.86 KB
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
// 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/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[%zux%zu] * B[%zux%zu]\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: taskflow
void taskflow(const std::vector<size_t>& D) {
auto tbeg = std::chrono::steady_clock::now();
tf::Taskflow tf;
std::cout << "Generating task As ...\n";
std::vector<matrix_t> As(D.size());
std::vector<tf::Task> 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<tf::Task> 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<tf::Task> 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 == "taskflow") {
taskflow(dimensions);
}
else {
std::cerr << "wrong method, shoud be [baseline|taskflow]\n";
}
return 0;
}