Repository navigation
Expand file tree
/
Copy pathtbb.cpp
More file actions
79 lines (67 loc) · 1.95 KB
/
Copy pathtbb.cpp
File metadata and controls
79 lines (67 loc) · 1.95 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
#include "matrix_multiplication.hpp"
#include <tbb/tbb.h>
#include <tbb/task_scheduler_init.h>
// matrix_multiplication_tbb
void matrix_multiplication_tbb(unsigned num_threads) {
using namespace tbb;
using namespace tbb::flow;
tbb::task_scheduler_init init(num_threads);
tbb::flow::graph G;
std::vector<std::unique_ptr<continue_node<continue_msg>>> tasks;
tasks.resize(4*N);
int i=0;
auto sync = std::make_unique<continue_node<continue_msg>>(G, [](const continue_msg&){});
for(; i<N ; i++) {
tasks[i] = std::make_unique<continue_node<continue_msg>>(G,
[=, i=i](const continue_msg&){
for(int j=0; j<N; ++j) {
a[i][j] = i + j;
}
}
);
make_edge(*tasks[i], *sync);
}
for(; i<2*N ; i++) {
tasks[i] = std::make_unique<continue_node<continue_msg>>(G,
[=, i=i-N](const continue_msg&){
for(int j=0; j<N; ++j) {
b[i][j] = i * j;
}
}
);
make_edge(*tasks[i], *sync);
}
for(; i<3*N ; i++) {
tasks[i] = std::make_unique<continue_node<continue_msg>>(G,
[=, i=i-2*N](const continue_msg&){
for(int j=0; j<N; ++j) {
c[i][j] = 0;
}
}
);
make_edge(*tasks[i], *sync);
}
for(; i<4*N ; i++) {
tasks[i] = std::make_unique<continue_node<continue_msg>>(G,
[=, i=i-3*N](const continue_msg&){
for(int j=0; j<N; ++j) {
for(int k=0; k<N; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
);
make_edge(*sync, *tasks[i]);
}
for(auto j=0; j<3*N; j++) {
tasks[j]->try_put(continue_msg());
}
G.wait_for_all();
//std::cout << reduce_sum() << std::endl;
}
std::chrono::microseconds measure_time_tbb(unsigned num_threads) {
auto beg = std::chrono::high_resolution_clock::now();
matrix_multiplication_tbb(num_threads);
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end - beg);
}