-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (53 loc) · 1.65 KB
/
main.cpp
File metadata and controls
66 lines (53 loc) · 1.65 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
#include <hpx/hpx_main.hpp>
#include <hpx/algorithm.hpp>
#include <hpx/execution.hpp>
#include <iostream>
#include <vector>
#include <chrono>
constexpr bool USE_2D = false;
constexpr int N = 1024;
#define IDX(i, j) ((i) * N + (j))
void multiply_1d(const std::vector<double>& A, const std::vector<double>& B, std::vector<double>& C)
{
hpx::experimental::for_loop(hpx::execution::par, 0, N, [&](int i) {
for (int j = 0; j < N; ++j) {
double sum = 0.0;
for (int k = 0; k < N; ++k)
sum += A[IDX(i, k)] * B[IDX(k, j)];
C[IDX(i, j)] = sum;
}
});
}
using Mat2D = std::vector<std::vector<double>>;
void multiply_2d(const Mat2D& A, const Mat2D& B, Mat2D& C)
{
hpx::experimental::for_loop(hpx::execution::par, 0, N, [&](int i) {
for (int j = 0; j < N; ++j) {
double sum = 0.0;
for (int k = 0; k < N; ++k)
sum += A[i][k] * B[k][j];
C[i][j] = sum;
}
});
}
int main()
{
std::cout << "HPX parallel matrix multiply (" << N << "x" << N
<< ") — " << (USE_2D ? "2D" : "1D") << " layout\n";
double sample = 0.0;
if constexpr (USE_2D) {
Mat2D A(N, std::vector<double>(N, 1.0));
Mat2D B(N, std::vector<double>(N, 2.0));
Mat2D C(N, std::vector<double>(N, 0.0));
multiply_2d(A, B, C);
sample = C[0][0];
} else {
std::vector<double> A(N * N, 1.0);
std::vector<double> B(N * N, 2.0);
std::vector<double> C(N * N, 0.0);
multiply_1d(A, B, C);
sample = C[0];
}
std::cout << sample << " (expected " << N * 2.0 << ")\n";
return 0;
}