-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_hpx.cpp
151 lines (126 loc) · 4.31 KB
/
main_hpx.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
// main.cpp
#include <hpx/hpx_main.hpp>
#include <hpx/algorithm.hpp>
#include <hpx/chrono.hpp>
#include <hpx/local/init.hpp>
#include <hpx/modules/assertion.hpp>
#include <iostream>
#include <random>
#include <chrono>
using namespace std;
#define N 1000
vector<vector<int>> generateMatrix(int rows, int cols) {
// Create a random number generator
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dist(1, 100);
// Create the matrix and fill it with random values
vector<vector<int>> matrix(rows, vector<int>(cols));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
matrix[i][j] = dist(gen);
}
}
return matrix;
}
//sequential version of matrix multipication
vector<vector<int>> matmul_seq( const vector<vector<int>>& a,
const vector<vector<int>>& b,
bool flag = 1) {
auto start = std::chrono::high_resolution_clock::now();
int ra = a.size();
int ca = a[0].size();
int rb = b.size();
int cb = b[0].size();
if (ca != rb){
cerr << "Dimensions incorrect (it's A(m,n)*B(n,k))"<< endl;
return {};
}
vector<vector<int>> c(ra, vector<int>(cb,0));
for (int i = 0; i < ra; i++) {
for (int j = 0; j < cb; j++) {
for (int k = 0; k < ca; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
//printing the time taken by the sequential version
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (flag == 1){
std::cout << "Time taken by sequential version: "
<< duration.count()
<< "ms" << endl;
}
return c;
}
vector<vector<int>> matmul_par_unseq( const vector<vector<int>>& a,
const vector<vector<int>>& b,
bool flag = 1) {
auto start = std::chrono::high_resolution_clock::now();
int ra = a.size();
int ca = a[0].size();
int rb = b.size();
int cb = b[0].size();
if (ca != rb){
cerr << "Dimensions incorrect (it's A(m,n)*B(n,k))"<< endl;
return {};
}
vector<vector<int>> c(ra, vector<int>(cb,0));
//todo : to make it vectorization-safe, is to replace the mutex by an atomic variable
hpx::for_each(hpx::execution::par_unseq, a.begin(), a.end(), [&](const std::vector<int>& row_a) {
int i = &row_a - &a[0];
for (int j = 0; j < cb; j++) {
for (int k = 0; k < ca; k++) {
c[i][j] += row_a[k] * b[k][j];
}
}
});
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (flag == 1){
std::cout << "Time taken by parallel(par_unseq) version: "
<< duration.count()
<< " ms" << endl;
}
return c;
}
vector<vector<int>> matmul_par( const vector<vector<int>>& a,
const vector<vector<int>>& b,
bool flag = 1) {
auto start = std::chrono::high_resolution_clock::now();
int ra = a.size();
int ca = a[0].size();
int rb = b.size();
int cb = b[0].size();
if (ca != rb){
cerr << "Dimensions incorrect (it's A(m,n)*B(n,k))"<< endl;
return {};
}
vector<vector<int>> c(ra, vector<int>(cb,0));
//
hpx::for_each(hpx::execution::par, a.begin(), a.end(), [&](const std::vector<int>& row_a) {
int i = &row_a - &a[0];
for (int j = 0; j < cb; j++) {
for (int k = 0; k < ca; k++) {
c[i][j] += row_a[k] * b[k][j];
}
}
});
//printing the time taken
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (flag == 1){
std::cout << "Time taken by parallel(par) version: "
<< duration.count()
<< "ms" << endl;
}
return c;
}
int main() {
auto a = generateMatrix(N, N);
auto b = generateMatrix(N, N);
auto e = matmul_seq(a, b);
auto c = matmul_par_unseq(a, b);
auto d = matmul_par(a, b);
}